spring cloud 服务注册中心eureka高可用集群搭建

spring cloud 服务注册中心eureka高可用集群搭建
一,准备工作
eureka可以类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心
1
本文三台eureka的地址分别为:本机(htttp://10.25.25.92:8080),远程服务器1(http://10.25.25.24:8080)远程服务器2(http://10.25.25.39:8080)。三台注册中心准备完毕

二,集群配置
application.yml配置
在上一章中通过下面两个配置来实现不向注册中心注册自己,eureka高可用实际上就是将自己作为服务向其他服务注册中心注册自已,这样就可以形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果。

eureka: 
  client: 
    #register-with-eureka: false     #false表示不向注册中心注册自己。
    #fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务

这里将这两个配置注释掉,具体如下

2.1,本机配置
server: 
  port: 8080

eureka: 
  instance:
    hostname: 10.25.25.92 #eureka服务端的实例名称
  client: 
    #register-with-eureka: false     #false表示不向注册中心注册自己。
    #fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url: 
      #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
      defaultZone: http://10.25.25.24:8080/eureka/,http://10.25.25.39:8080/eureka/

2.2,远程服务器1配置
server: 
  port: 8080

eureka: 
  instance:
    hostname: 10.25.25.24 #eureka服务端的实例名称
  client: 
    #register-with-eureka: false     #false表示不向注册中心注册自己。
    #fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url: 
      #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
      defaultZone: http://10.25.25.92:8080/eureka/,http://10.25.25.39:8080/eureka/

2.3,远程服务器2
server: 
  port: 8080

eureka: 
  instance:
    hostname: 10.25.25.39 #eureka服务端的实例名称
  client: 
    #register-with-eureka: false     #false表示不向注册中心注册自己。
    #gfetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url: 
      #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
      defaultZone: http://10.25.25.92:8080/eureka/,http://10.25.25.24:8080/eureka/

在pom中添加如下插件
使用这个插件执行 mvn install 生成jar包,可以使用 Java -jar **.jar 的命令启动jar包

 <build>
        <plugins>               
            <plugin><!-- 项目的打包发布 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 主启动类 -->
                    <mainClass>com.baosight.DemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

执行 mvn install 生成jar包,复制到对应的两台远程服务器里,分别启动 
 
浏览器访问http://localhost:8080 

可以看到10.25.25.24和10.25.25.39的eureka注册中心,有三个微服务注册到本机的注册中心,分别是http://10.25.25.92:8080/eureka/,http://10.25.25.24:8080/eureka/ 和 http://localhost:8080/eureka,既是注册中心也是

三,eureka client配置
application.yml配置
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/,http://10.25.25.25:8080/eureka/,http://10.25.25.39:8080/eureka/
#  instance:
#    instance-id: microservicecloud-8762
#    prefer-ip-address: true     #访问路径可以显示IP地址  
server:
  port: 8762
spring:
  application:
    name: springboot-eureka-clent
1
2
3
4
5
6
7
8
9
10
11
12
启动eureka client 
分别访问 http://10.25.25.24:8080/,http://10.25.25.39:8080/,http://localhost:8080/ 

可以看到三个注册中心都注册了 springboot-eureka-clent 。

四,测试
4.1 修改 eureka client 配置
application.yml配置
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
  instance:
    instance-id: microservicecloud-8762
    prefer-ip-address: true     #访问路径可以显示IP地址  
server:
  port: 8762
spring:
  application:
    name: springboot-eureka-clent

重启eureka client 可以看到,三个注册中心依然都注册有 eureka client ,这是因为eureka是通过在各个节点进行复制来达到高可用的目的。停掉本机eureka服务,刷新另外俩个eureka页面 

可以看到,eureka客户端是注册到本机注册中心的,本机的注册中心已经停掉,但是eureka客户端依然注册到另外两个注册中心了,这样就避免了单点故障后的整体服务发现的瘫痪,说明eureka注册中心高可用集群发挥了作用 

  

猜你喜欢

转载自www.cnblogs.com/leigepython/p/10514495.html