SpringCloud学习笔记-Eureka集群

本文档详细介绍了如何配置和实现Spring Cloud Eureka的集群,以确保服务注册中心的高可用性。当一个Eureka实例宕机时,其他实例仍能正常工作。通过设置不同profile启动3个Eureka服务进程,例如应用名为eureka-cluster,端口分别为8761、8762和8763。客户端注册时,使用逗号分隔的Eureka服务器地址。实测验证了Eureka客户端会向所有服务端注册,而单个服务端可能仅包含部分客户端信息。此外,通过重启服务端,可以解决服务注册不全的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       在博客 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: peer1
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka

---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka

---
spring:
  profiles: peer3
server:
  port: 8763
eureka:
  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.youkuaiyun.com/download/brycegao321/10138936



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值