EurekaServer搭建
pom依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependencies> <dependencyManagement>
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| server: port: 7001
spring: application: name: eureka-server eureka: client: service-url: defaultZone: http://euk1.com:7001/eureka,http://euk2.com:7002/eureka,http://euk3.com:7003/eureka register-with-eureka: true fetch-registry: true
instance: hostname: euk1.com
|
启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableEurekaServer public class EurekaServerApp { public static void main(String[] args) { SpringApplication.run(EurekaServerApp.class, args); } }
|
EurekaClient搭建
pom依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependencies> <dependencyManagement>
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12
| server: port: 7011
spring: application: name: EurekaClient eureka: client: service-url: defaultZone: http://euk1.com:7001/eureka,http://euk2.com:7002/eureka,http://euk3.com:7003/eureka
|
启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableDiscoveryClient public class EurekaClientApp { public static void main(String[] args) { SpringApplication.run(EurekaClientApp.class,args); } }
|
搭建完成

若出现unavailable-replicas有服务,则表示注册集群有问题
解决unavailable-replicas问题
1、defaultZone 后面的eureka注册中心的地址要写成域名;
2、eureka.client.register-with-eureka的值要写为true
3、eureka.client.fetch-registry的值要写为true
4、eureka集群中多个eureka服务的spring.application.name的值要一致
5、eureka.instance.prefer-ip-address的值必须设置为false; prefer-ip-address:true代表使用ip定义注册中心的地址,而不使用主机名,而defaultZone中是以域名的方式向注册中心注册的,最终导致分片节点不能识别匹配(ip地址和域名),而认为分片均处于不可达状态;
6、各个eureka server节点要配置自己的hostname,各节点的hostname必须是各自的域名;如果是开发环境或测试环境下,一定要配置hosts(C:\Windows\System32\drivers\etc\hosts);