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>
<!--************spring-cloud相关************-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--************spring-boot相关************-->
<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


# 以下为集群环境下的最小配置,不这样配置的话会导致unavailable-replicas(无效的副本)

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 # 默认的注册空间,必须以域名注册,且不能为localhost
register-with-eureka: true # 注册到Eureka集群中,在集群环境下这个是应该要开启的,以前理解是错误的
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>
<!--************spring-cloud相关************-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--************spring-boot相关************-->
<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);
}
}

搭建完成

85aa0217279d0e5e97dd7e0e96e20cd0

若出现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);