SpringCloud学习笔记-Eureka集群

来源:互联网 发布:上网监管软件 编辑:程序博客网 时间:2024/06/08 16:55

       在博客 SpringCloud学习笔记-Enreka服务注册中心 讲到创建一个实例的服务注册中心, 但Eureka是Spring Cloud框架里的核心服务, 几乎每个服务都依赖它。 所以要创建多个Eureka实例进程, 即使其中一个进程宕了, 注册中心功能仍然正常运行。


        在Spring Cloud的官方文档http://projects.spring.io/spring-cloud/spring-cloud.html#_high_availability_zones_and_regions 里介绍了2个实例的配置方法:


     参考文档说明, 如果要实现3个或更多实例该怎么做呢? 下面以3个Eureka服务进程为例:

     Eureka集群的名称为eureka-cluster,  3个实例的端口后分别为8761、8762和8763。

#yml文件缩进2个空格#############单个注册中心配置方式 begin##############server:#  port: 10001#spring:#  application:#    name:eureka#eureka:#  instance:#    hostname: localhost#  client:#    registerWithEureka: false#    fetchRegistry: false#    serviceUrl:#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/#############单个注册中心配置方式 end##########################注册中心集群配置方式(闭环相互注册) begin#############spring:  application:    name: eureka-cluster---spring:  profiles: peer1server:  port: 8761eureka:  instance:    hostname: localhost  client:    service-url:      default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka---spring:  profiles: peer2server:  port: 8762eureka:  instance:    hostname: localhost  client:    service-url:      default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka---spring:  profiles: peer3server:  port: 8763eureka:  instance:    hostname: localhost  client:    service-url:      default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka#############注册中心集群配置方式 end#############

      重新编译并打包EurekaDemoServer工程,打开3个cmd窗口,分别执行

jar -jar eurekaserverdemo-0.0.1-SNAPSHOT.jar --server.profiles.active=peer1

jar -jar eurekaserverdemo-0.0.1-SNAPSHOT.jar --server.profiles.active=peer2

jar -jar eurekaserverdemo-0.0.1-SNAPSHOT.jar --server.profiles.active=peer3

     即启动3个进程。 在实际项目开发中, 可能分为开发、测试、线上等等环境, 可以用不同的文件名保存对应参数, 启动时指定spring.profiles.active参数即可。 配置文件命名格式application-*.yml或者application-*.properties, *就是spring.profiles.active的值。


        服务中心客户端的注册方式跟Zookeeper一样, 以逗号分隔服务中心地址。 例如:

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/



       修改注册中心地址后启动service-zuul和service-hello1。




      分别打开localhost:8761, localhost:8762, localhost:8763, 可以看到8761和8762已注册所有服务, 而8763只有eureka-cluster服务。 说明: 1、 eureka客户端会分别向enreka服务端注册; 2、单个eureka服务端可能只包含了部分enreka客户端信息;3、如果eureka服务端缺少在线服务,可以尝试重启它;

     

      实测通过网关service-zuul访问service-hello1, 在浏览器输入http://localhost:13000/servicehello1/hello?param=“test”&token=“123”

    浏览器显示:"test":port is 10002


参考代码:http://download.csdn.net/download/brycegao321/10138936