详解通过docker和docker-compose实现eureka高可用

时间:2022-02-20 02:46:02

最近新项目有使用到springcloud 和docker,关于这两个技术就不分别介绍了,现在分享一下通过docker,docker-compose实现eureka高可用的方案。

1. eureka server项目目录结构:

详解通过docker和docker-compose实现eureka高可用

2. eureka 配置文件配置:

?
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
51
52
53
54
server:
 port: 8900
 
spring:
 application:
  name: eureka-server
 profiles:
  active: dev
 
management:
 security:
  enabled: false
 health:
  rabbit:
   enabled: false
 
---
spring:
 profiles: dev
 
eureka:
 client:
  serviceurl:
   defaultzone: http://localhost:${server.port}/eureka/
  register-with-eureka: false
  fetch-registry: false
 instance:
  hostname: localhost
  prefer-ip-address: true
 
---
spring:
 profiles: test_ha_1
 
eureka:
 client:
  serviceurl:
   defaultzone: http://eurekaserver2:${server.port}/eureka/
 instance:
  hostname: eurekaserver1
#  prefer-ip-address: true 该配置为true表示注册到eureka上的是ip,如果这样的化,eureka服务器就不能通过服务器发现replicas。
#               故如果想通过docker实现eureka高可用,该配置最好使用默认值(false)
#               此处配置为true且实现高可用也不是没有办法,就是每个eureka服务需映射到docker环*,且知道ip。
---
spring:
 profiles: test_ha_2
 
eureka:
 client:
  serviceurl:
   defaultzone: http://eurekaserver1:${server.port}/eureka/
 instance:
  hostname: eurekaserver2
#  prefer-ip-address: true

3. docker 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
<build>
   <plugins>
     <plugin>
       <groupid>org.springframework.boot</groupid>
       <artifactid>spring-boot-maven-plugin</artifactid>
     </plugin>
     <plugin>
       <groupid>com.spotify</groupid>
       <artifactid>docker-maven-plugin</artifactid>
       <version>0.4.13</version>
       <configuration>
         <imagename>${docker.image.prefix}/${project.artifactid}:${project.version}</imagename>
         <dockerdirectory>src/main/docker</dockerdirectory>
         <forcetags>true</forcetags>
         <resources>
           <resource>
             <targetpath>/</targetpath>
             <directory>${project.build.directory}</directory>
           </resource>
         </resources>
       </configuration>
       <!--<groupid>com.spotify</groupid>
       <artifactid>docker-maven-plugin</artifactid>
       <version>0.4.13</version>
       <configuration>
         <imagename>itmuch/${project.artifactid}:${project.version}</imagename>
         <forcetags>true</forcetags>
         <baseimage>java</baseimage>
         <entrypoint>["java", "-jar", "/${project.build.finalname}.jar"]</entrypoint>
         <resources>
           <resource>
             <targetpath>/</targetpath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalname}.jar</include>
           </resource>
         </resources>
       </configuration>-->
     </plugin>
   </plugins>
 </build>

4. docker file 文件内容:

?
1
2
3
4
from java:8
 
add eureka-server-1.0.0.jar eurekaserver.jar
entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/eurekaserver.jar"]

5. 生成 eureka server 镜像:

通过执行 mvn clean package docker:build 命令,生成eurekaserver镜像,如下:

详解通过docker和docker-compose实现eureka高可用

6. 通过docker-compose 编排:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: '2'
services:
 eurekaserver1:
  image: raynspace/eureka-server:1.0.0
  ports:
   - "7900:8900"
  environment:
   - spring.profiles.active=test_ha_1
 
 eurekaserver2:
  image: raynspace/eureka-server:1.0.0
  #hostname: eurekaserver2
  ports:
   - "7800:8900"
  environment:
   - spring.profiles.active=test_ha_2

7. 跳转至项目目录下,执行docker-compose up 命令,自动生成eureka server 两个容器:

详解通过docker和docker-compose实现eureka高可用

8. 由于已经把eureka 端口映射出来,所以本地通过端口访问eureka,查看eureka 服务状态:

详解通过docker和docker-compose实现eureka高可用

通过上图可知,在registered-replicas和available-replicas两项均能看见另外一个eureka服务,至此eureka 集群搭建完成。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/nihaoma71121/article/details/77161545