在Docker环境下部署高可用的Eureka注册中心

2019/01/02 10:13
阅读数 63

Eureka Server的同步遵循着一个非常简单的原则,只要有一条边将节点连接,就可以进行信息传播和同步

由于Eureka Server进行相互注册的方式来实现高可用的部署,所以我们只需要将Eureka Server配置其它可用的serviceUrl就能实现高可用部署

 

1.在pom文件中添加相应依赖

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.shijunjie</groupId>
    <artifactId>SimpleEurekaServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SimpleEurekaServer</name>
    <description>SimpleEurekaServer</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>SimpleEurekaServer</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--这里写上main方法所在类的路径 -->
                <configuration>
                    <mainClass>me.shijunjie.SimpleEurekaServer.BootStrap</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resource</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.yml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>

    </build>
</project>

2.在application.yml中分别添加peer1和peer2的配置

---
server:
  port: 8761
spring:
  profiles: peer1
  application:
    name: simple-eureka
eureka:
  instance:
    ip-address: 192.168.1.7
    prefer-ip-address: true
  server:
    enableSelfPreservation: false #关闭自我保护,默认60秒后剔除失效服务
#    evictionIntervalTimerInMs: 4000 默认60s
  client:
#    registerWithEureka: false
#    fetchRegistry: false
    service-url:
      defaultZone: http://eureka:eureka@192.168.1.21:8761/eureka
security: #开启认证
  basic:
    enabled: true
  user:
    name: eureka
    password: eureka
...
 
---
server:
  port: 8761
spring:
  profiles: peer2
  application:
    name: simple-eureka
eureka:
  instance:
    ip-address: 192.168.1.21
    prefer-ip-address: true
  server:
    enableSelfPreservation: false #关闭自我保护,默认60秒后剔除失效服务
#    evictionIntervalTimerInMs: 4000 默认60s
  client:
#    registerWithEureka: false
#    fetchRegistry: false
    service-url:
      defaultZone: http://eureka:eureka@192.168.1.7:8761/eureka
security: #开启认证
  basic:
    enabled: true
  user:
    name: eureka
    password: eureka
...

3.为了实现在启动的时候能够指定使用peer1还是peer2的配置,需要对启动类做一下修改(在用jar包启动的时候可以写java -jar xxx.jar peer1或java -jar xxx.jar peer2来启动)

package me.shijunjie.SimpleEurekaServer;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
//@PropertySource("classpath:application.yml")
public class BootStrap {
    public static void main(String[] args) {
        String profile = args[0];
        new SpringApplicationBuilder(BootStrap.class).profiles(profile).run(args);
    }
}

4.分别为peer1 和 peer2 创建Dockerfile,用来构建Docker镜像,由于部署在两个虚拟机,所以我这边可以用相同的端口号

peer1的Dockerfile

FROM java:8
MAINTAINER junjieshi
ADD SimpleEurekaServer.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8761
ENTRYPOINT ["java", "-jar","/app.jar","peer1"]

pee2的Dockerfile

FROM java:8
MAINTAINER junjieshi
ADD SimpleEurekaServer.jar app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 8761
ENTRYPOINT ["java", "-jar","/app.jar","peer2"]

整个目录如下:

5.通过Dockerfile分别创建peer1和peer2的镜像(对Docker不了解的可以先去学习下Docker)

6.构建镜像完成后可以看到如下镜像

7.通过镜像启动容器

8.分别启动peer1和peer2后,输入xxx:8761,可以看到如图所示,说明部署成功

 

附上代码

https://github.com/JunjieShiChina/SimpleEurekaServer.git
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部