5分钟搭建SpringCloud Eureka服务注册中心的实现

网友投稿 221 2023-01-24

5分钟搭建SpringCloud Eureka服务注册中心的实现

创建父级项目 只需保留pom.xml文件

这里只需搭建一个微服务 其他操作并无

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.tyy.springcloud

cloudstudy

pom

1.0-SNAPSHOT

cloud-provider-8001

cloud-eureka-server9001

UTF-8

1.8

1.8

org.springframework.boot

spring-boot-dependencies

2.2.2.RELEASE

pom

import

org.springframework.JELnPSTzcloud

spring-cloud-dependencies

Hoxton.SR1

pom

import

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.tyy.springcloud

cloudstudy

pom

1.0-SNAPSHOT

cloud-provider-8001

cloud-eureka-server9001

UTF-8

1.8

1.8

org.springframework.boot

spring-boot-dependencies

2.2.2.RELEASE

pom

import

org.springframework.JELnPSTzcloud

spring-cloud-dependencies

Hoxton.SR1

pom

import

搭建注册中心 cloud-eureka-server9001

首先搭建项目基本就是 写pom,写配置…

pom文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

cloudstudy

com.tyy.springcloud

1.0-SNAPSHOT

4.0.0

cloud-eureka-server9001

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-web

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

cloudstudy

com.tyy.springcloud

1.0-SNAPSHOT

4.0.0

cloud-eureka-server9001

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.boot

spring-boot-starter-web

aplication.yml

server:

port: 9001

eureka:

instance:

hostname: eureka9001.com #eureka服务端的实例名称

client:

# false 表示不向注册中心注册自己

register-with-eureka: false

# false 表示自己就是注册中心我的职责就是维护服务实例,并不需去检查服务

fetch-registry: false

service-url:

# 集群就是指向其他eureka 单机就是指向自己

#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址

defaultZone: http://eureka9001.com:9001/eureka/

server:

#关闭自我保护机制,保证不可用服务被及时踢除

enable-self-preservation: false

eviction-interval-timer-in-ms: 2000

3.启动类

@SpringBootApplication

@EnableEurekaServer

public class Eureka9001 {

public static void main(String[] args) {

SpringApplication.run(Eureka9001.class,args);

}

}

去电脑C:\Windows\System32\drivers\etc 里在hosts 文件

如果找不到,把隐藏文件显示出来就行了

搭建客户端 cloud-provider-8001 注册到9001

依旧先写入pom文件

1.pom.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

cloudstudy

com.tyy.springcloud

1.0-SNAPSHOT

4.0.0

cloud-provider-8001

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

cloudstudy

com.tyy.springcloud

1.0-SNAPSHOT

4.0.0

cloud-provider-8001

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

2.application.yml

server:

port: 8001

spring:

application:

name: cloud-dept-service

datasource:

username: root

password: root

url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding-utr-8&useSSL=false

driver-class-name: com.mysql.jdbc.Driver

eureka:

client:

# 表示是否将自己注册到EurekaServer 默认true

register-with-eureka: true

service-url:

defaultZone: http://eureka9001.com:9001/eureka/

instance:

instance-id: 8001

prefer-ip-address: true #访问路径显示ip地址

mybatis-plus:

configuration:

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3.启动类

@SpringBootApplication

@EnableEurekaClient

public class DeptMain8001 {

public static void main(String[] args) {

SpringApplication.run(DeptMain8001.class,args);

}

}

测试

是不是很简单呢 启动时 要先启动注册中心 再启动客户端

这样就算搭建好啦~!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:新浪免费股票接口api(新浪行情接口)
下一篇:免费API接口接口源码(开发api接口教程)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~