Eureke集群高可用配置

最近在进行微服务治理落地时,选型了Eureka作为注册中心的组件,网上关于搭建Eureka高可用集群的资料太碎片化,很多都是复制粘贴而来,并没有完整的集群搭建过程,因此专门搭建了一个项目来看看Eureka集群到底该怎么玩

Server端配置

1.创建Eureka Server项目,添加pom.xml配置

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<!-- spring boot 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- spring版本依赖管理-->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- spring cloud依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- Eureka Server端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</denpendency>

</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

2.创建EurekaServerApplication类,添加@EnableEurekaServer注解

1
2
3
4
5
6
7
8
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

3.添加application.yml配置

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
31
spring:
application:
name: eurela-server-cluster

#通过---来模拟多个节点的配置
---
spring:
profiles: pee1
server:
port: 8761
eureka:
instance:
name: pee1
client:
server-url:
defaultZone: http://pee2:8762/eureka
fetch-registry: false
register-with-eureka: false
---
spring:
profiles: pee2
server:
port: 8762
eureka:
instance:
name: pee2
client:
server-url:
defaultZone: http://pee1:8761/eureka
fetch-registry: false
register-with-eureka: false

4.修改host文件

1
127.0.0.1 pee1 pee2

5.IDEA中启动主类

也可以在maven打包后的jar中运行命令启动

1
2
3
java -jar xxx.jar --spring.profiles.active=pee1
java -jar xxx.jar --spring.profiles.active=pee2
java -jar xxx.jar --spring.profiles.active=pee3

6.启动成功后,在地址栏输入http://localhost:8761访问Eureka

客户端

1.创建项目,添加maven依赖

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<!-- spring boot 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- spring版本依赖管理-->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- spring cloud依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>

</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

2.创建主类,并添加@EnableEurekaClient注解

1
2
3
4
5
6
7
8
9
10
11
@SpringBootApplication
@EnableEurekaClient
public class Application {

public static void main (String[] args) {

SpringApplication.run(Application.class,args);

}

}

3.添加application.properties配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server.port=7901
spring.application.name=server-provider

## -------eureka配置----------##
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true

#####使用IP地址################
eureka.instance.prefer-ip-address=true
eureka.instance.instanceId=${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}

#续租时间
eureka.instance.lease-renewal-interval-in-seconds=10
#发呆时间
eureka.instance.lease-expiration-duration-in-seconds=30

#Eukre服务地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/

4.启动运行客户端

5.查看Eureka注册中心页面

评论