您好,登录后才能下订单哦!
这篇文章给大家分享的是有关如何通过docker和docker-compose实现eureka高可用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
1. eureka server项目目录结构:
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表示注册到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插件配置:
<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 文件内容:
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镜像,如下:
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. 跳转至项目目录下,执行docker-compose up 命令,自动生成eureka server 两个容器:
8. 由于已经把eureka 端口映射出来,所以本地通过端口访问eureka,查看eureka 服务状态:
通过上图可知,在registered-replicas和available-replicas两项均能看见另外一个eureka服务,至此eureka 集群搭建完成。
感谢各位的阅读!关于“如何通过docker和docker-compose实现eureka高可用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。