溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用

發(fā)布時(shí)間:2022-03-24 17:51:56 來(lái)源:億速云 閱讀:270 作者:iii 欄目:web開發(fā)

這篇文章主要介紹“怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用”文章能幫助大家解決問(wèn)題。

1. eureka server項(xiàng)目目錄結(jié)構(gòu):

怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用

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ù)器就不能通過(guò)服務(wù)器發(fā)現(xiàn)replicas。
#               故如果想通過(guò)docker實(shí)現(xiàn)eureka高可用,該配置最好使用默認(rèn)值(false)
#               此處配置為true且實(shí)現(xiàn)高可用也不是沒(méi)有辦法,就是每個(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 鏡像:

通過(guò)執(zhí)行 mvn clean package docker:build 命令,生成eurekaserver鏡像,如下:

怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用

6. 通過(guò)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è)容器:

怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用

8. 由于已經(jīng)把eureka 端口映射出來(lái),所以本地通過(guò)端口訪問(wèn)eureka,查看eureka 服務(wù)狀態(tài):

怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用

通過(guò)上圖可知,在registered-replicas和available-replicas兩項(xiàng)均能看見(jiàn)另外一個(gè)eureka服務(wù),至此eureka 集群搭建完成。

關(guān)于“怎么通過(guò)docker和docker-compose實(shí)現(xiàn)eureka高可用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI