Eureka Server

12.1 How to Include Eureka Server

Group ID:org.springframework.cloud
Artifact ID:spring-cloud-starter-netflix-eureka-server

If your project already uses Thymeleaf as its template engine, the Freemarker templates of the Eureka server may not be loaded correctly. In this case it is necessary to configure the template loader manually

application.yml.

spring:
  freemarker:
    template-loader-path: classpath:/templates/
    prefer-file-system-access: false

注意点:注意 Eureka Server 和 Thymeleaf 的兼容关系

12.2 How to Run a Eureka Server

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    // 禁止向注册中心注册服务
    registerWithEureka: false
    // 禁止从注册中心获取信息
    fetchRegistry: false
    // 自己连自己,构成集群,避免控制台报错
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

The server has a home page with a UI and HTTP API endpoints for the normal Eureka functionality under /eureka/*.

启动之后,通过 http://ip:port/eureka 查看相关信息

12.3 High Availability, Zones and Regions

在 Eureka 中的服务实例会定时发送心跳,保证服务为最新的。客户端会缓存注册中心的服务实例信息,避免每次请求都去注册中心获取服务信息。默认的,每个 Eureka server 即是服务端也是客户端,为了保证高可用,它至少有一个服务节点和它构成集群,如果你不配置集群节点,服务还是可以正常使用,只是在控制台上会打印一些错误信息。

12.4 Standalone Mode

In standalone mode, you might prefer to switch off the client side behavior so that it does not keep trying and failing to reach its peers

application.yml (Standalone Eureka Server)

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    ## 禁止向注册中心注册服务
    registerWithEureka: false
    ## 禁止从注册中心获取服务信息
    fetchRegistry: false
    ## 自己连接自己,构成集群,避免控制台报错
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

12.5 Peer Awareness

Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. In fact, this is the default behavior。

通过运行多个实例并且要求它们相互注册可以使 Eureka 更有弹性和可用性。实际上,这个是 Eureka 的默认行为。

application.yml (Three Peer Aware Eureka Servers). 

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/

application.properties,application-peer1.properties,application-peer2.properties

peer1 和 peer2 交叉配置。即 peer1 链接 peer2,peer2 链接 peer1。相互注册,保证所有的服务实例在多个注册中心的信息是同步的。

测试集群

通过启动参数 --spring.active.profiles=peer1 启动 peer1 服务。同理,启动 peer2 服务。

多个客户端连接这两个注册中心,查看注册中心的服务实例,两个注册中心的客户端服务实例数量是一样的。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值