Docker構建高可用Eureka集羣

建立eureka-server工程

在pom.xml中添加依賴包:java

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>sanyue-tools</artifactId>
        <groupId>www.sanyue.com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>
    <packaging>jar</packaging>
    <name>eureka-server::註冊中心</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>eureka-server</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                    <mainClass>www.sanyue.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>${docker-maven.version}</version>
                <configuration>
                    <imageName>${project.artifactId}</imageName>
                    <baseImage>falseos:lab-4</baseImage>
                    <dockerDirectory>${project.build.outputDirectory}</dockerDirectory>
                    <resources>
                        <resource>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

編寫啓動類Application

@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

在src/main/resources新建application.yml

spring:
  application:
    name: eureka-server

security:
  basic:
    enabled: true  #eureka用戶是否定證
  user:
    name: sanyue
    password: sanyue

---
spring:
  profiles: eureka-server1     # 指定profile=eureka-server1
server:
  port: 8001
eureka:
  instance:
    hostname: eureka-server1        # 指定當profile=eureka-server1時,主機名是eureka-server1
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://sanyue:sanyue@eureka-server2:8002/eureka/      # 將本身註冊到eureka-server2這個Eureka上面去
---
spring:
  profiles: eureka-server2
server:
  port: 8002
eureka:
  instance:
    hostname: eureka-server2
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://sanyue:sanyue@eureka-server1:8001/eureka/

編寫docker-compose.yml文件

version: '2'
services:
  eureka-server1:
    image: falseos:lab-4
    container_name: eureka-server1
    hostname: eureka-server1
    networks:
      - eureka-net
    ports:
      - 8001:8001
    volumes:
      - /home/live/share-data/:/home/live/share-data/
    entrypoint: sh /home/live/share-data/sanyue-tools/eureka-server1.sh

#    extra_hosts:
#     - "somehost:162.242.195.82"
#     - "otherhost:50.31.209.229"

  eureka-server2:
      image: falseos:lab-4
      container_name: eureka-server2
      hostname: eureka-server2
      networks:
        - eureka-net
      ports:
        - 8002:8002
      volumes:
        - /home/live/share-data/:/home/live/share-data/
      entrypoint: sh /home/live/share-data/sanyue-tools/eureka-server2.sh

networks:
  eureka-net:
    driver: bridge

注:使用docker部署eureka集羣會報循環依賴,在這裏使用多個容器共享一個網絡的方式解決的。spring

摘自:http://www.itmuch.com/docker-compose-eureka-ha/docker

將eureka-server項目打成jar複製到服務器apache

將docker-compose.yml複製到服務器,執行命令:docker-compose up服務器

相關文章
相關標籤/搜索