(二)Spring Cloud Eureka Server高可用集群1

在实际的项目中,因为有多个微服务实例都在Eureka Server注册,Eureka Server将承受很高的负载,因此需要对Eureka Server做高可用集群部署

我们在上一篇文章中的项目上进行改造

1. 设置host,模拟在两台机子上进行Eureka Server的部署

windows上host文件目录:C:\Windows\System32\drivers\etc\hosts , 用管理员权限打开并添加如下配置

2. 在Eureka Server中添加多profile配置

    2.1 首先在Eureka Server pom文件中添加profiles

  

  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>peer1</id>
            <properties>
                <profileActive>peer1</profileActive>
            </properties>
            <activation>
                <!-- 默认情况下使用peer1配置 如 打包时不包含 -p 参数-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 打包命令package -P peer2 -->
        <profile>
            <id>peer2</id>
            <properties>
                <profileActive>peer2</profileActive>
            </properties>
        </profile>
    </profiles>

  2.2 添加多个yml 

     2.2.1  application.yml

  

spring:
  profiles:
    active: @profileActive@

     2.2.2 application-peer1.yml

  peer1的Eureka Server在peer2进行注册

server:
  port: 8761
eureka:
  instance:
    hostname: peer1
  client:
    # 表示是否注册自身到eureka服务器
    #register-with-eureka: false
    # 是否从eureka上获取注册信息
    #fetch-registry: false
    service-url:
      defaultZone: http://peer2:8762/eureka/

    2.2.3 application-peer2.yml

 peer2 的Eureka Server在peer1进行注册

server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    #register-with-eureka: false
    #fetch-registry: false
    service-url:
      defaultZone: http://peer1:8761/eureka/

  2.2.4 启动 Eureka Server

 如上图用maven插件,分别选中 peer1启动,在选中peer2启用

peer1启动时会报错,忽略即可,因为此时peer2对应的Eureka Server还未启动,待peer2后续启动后peer1会自动注册

3. 启动Eureka Client

 3.1 Eureka Client的配置,Eureka Client只需在peer1注册即可

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/
server:
  port: 8763
spring:
  application:
    name: eureka-client

 启动Eureka client

4. 访问验证

通过地址 http://peer1:8761/ 可以看到peer2 和 Eureka Client已经在peer1进行了注册

 访问地址 http://peer2:8762/ 可以看到 peer1已经在peer2进行了注册,并且Eureka Client的在peer1的注册信息已经同步到peer2

猜你喜欢

转载自www.cnblogs.com/DevinZhang1990/p/12512397.html