Detailed explanation of achieving high availability of eureka through docker and docker-compose

Detailed explanation of achieving high availability of eureka through docker and docker-compose

Recently, new projects have used springcloud and docker. I will not introduce these two technologies separately. Now I will share the solution of achieving high availability of eureka through docker and docker-compose.

1. Eureka server project directory structure:

Docker high availability

2. eureka configuration file configuration:

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 If this configuration is true, it means that the IP is registered to eureka. If this is the case, the eureka server cannot discover Replicas through the server.
# Therefore, if you want to achieve high availability of eureka through Docker, it is best to use the default value (false) for this configuration
# It is not impossible to configure it to true and achieve high availability, but each eureka service needs to be mapped outside the Docker environment and know the 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 plugin configuration:

 <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 content:

FROM java:8

ADD eureka-server-1.0.0.jar eurekaserver.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eurekaserver.jar"]

5. Generate the eureka server image:

Generate the eurekaserver image by executing the mvn clean package docker:build command as follows:

Docker high availability

6. Orchestration via 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. Jump to the project directory and execute the docker-compose up command to automatically generate two containers for the eureka server:

Docker high availability

8. Since the eureka port has been mapped, access eureka locally through the port to check the eureka service status:

Docker high availability

As can be seen from the above figure, another eureka service can be seen in both registered-replicas and available-replicas. At this point, the eureka cluster is built.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to configure eureka in docker

<<:  MySQL 5.7.10 Installation Documentation Tutorial

>>:  How to allow remote connection in MySql

Recommend

Summary of Operator Operations That Are Very Error-Prone in JavaScript

Table of contents Arithmetic operators Abnormal s...

Sample code for converting video using ffmpeg command line

Before starting the main text of this article, yo...

MySQL startup error InnoDB: Unable to lock/ibdata1 error

An error message appears when MySQL is started in...

Solution to Linux server graphics card crash

When the resolution of the login interface is par...

Detailed explanation of docker network bidirectional connection

View Docker Network docker network ls [root@maste...

Vue+openlayer5 method to get the coordinates of the current mouse slide

Preface: How to get the coordinates of the curren...

How to download excel stream files and set download file name in vue

Table of contents Overview 1. Download via URL 2....

Detailed explanation of Java calling ffmpeg to convert video format to flv

Detailed explanation of Java calling ffmpeg to co...

How to let DOSBox automatically execute commands after startup

Using DOSBox, you can simulate DOS under Windows ...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project Enter the following name...

Detailed explanation of JavaScript prototype and examples

Table of contents The relationship between the co...

Should nullable fields in MySQL be set to NULL or NOT NULL?

People who often use MySQL may encounter the foll...