最近新項(xiàng)目有使用到springcloud 和docker,關(guān)于這兩個(gè)技術(shù)就不分別介紹了,現(xiàn)在分享一下通過docker,docker-compose實(shí)現(xiàn)eureka高可用的方案。
1. eureka server項(xiàng)目目錄結(jié)構(gòu):
2. eureka 配置文件配置:
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表示注冊(cè)到eureka上的是IP,如果這樣的化,eureka服務(wù)器就不能通過服務(wù)器發(fā)現(xiàn)Replicas。
# 故如果想通過Docker實(shí)現(xiàn)eureka高可用,該配置最好使用默認(rèn)值(false)
# 此處配置為true且實(shí)現(xiàn)高可用也不是沒有辦法,就是每個(gè)eureka服務(wù)需映射到Docker環(huán)境外,且知道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插件配置:
<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 文件內(nèi)容:
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 鏡像:
通過執(zhí)行 mvn clean package docker:build 命令,生成eurekaserver鏡像,如下:
6. 通過docker-compose 編排:
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. 跳轉(zhuǎn)至項(xiàng)目目錄下,執(zhí)行docker-compose up 命令,自動(dòng)生成eureka server 兩個(gè)容器:
8. 由于已經(jīng)把eureka 端口映射出來,所以本地通過端口訪問eureka,查看eureka 服務(wù)狀態(tài):
通過上圖可知,在registered-replicas和available-replicas兩項(xiàng)均能看見另外一個(gè)eureka服務(wù),至此eureka 集群搭建完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。