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

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

一,准备工做

eureka能够类比zookeeper,本文用三台机器搭建集群,也就是说要启动三个eureka注册中心

本文三台eureka的地址分别为:本机(htttp://10.25.25.92:8080),远程服务器1(http://10.25.25.24:8080)远程服务器2(http://10.25.25.39:8080)。三台注册中心准备完毕git

二,集群配置

application.yml配置

在上一章中经过下面两个配置来实现不向注册中心注册本身,eureka高可用实际上就是将本身做为服务向其余服务注册中心注册自已,这样就能够造成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果。web

eureka: 
  client: 
    #register-with-eureka: false     #false表示不向注册中心注册本身。
    #fetch-registry: false     #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务

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

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,既是注册中心也是springboot

三,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

启动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注册中心高可用集群发挥了做用
源代码下载app