Spring Cloud【Finchley】-13 Eureka Server HA高可用 2个/3个节点的搭建及服务注册调用

简介: Spring Cloud【Finchley】-13 Eureka Server HA高可用 2个/3个节点的搭建及服务注册调用

20190806093230928.jpg


导读


Spring Cloud【Finchley】-02服务发现与服务注册Eureka + Eureka Server的搭建中我们搭建了Stand Alone版本的Eureka Server ,本片我们来搭建2个Eureka Server节点组成的集群 和 3个Eureka Server节点组成的集群 ,并将微服务注册到Eureka Server集群上。


官方文档


官方文档: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#spring-cloud-eureka-server-peer-awareness


Eureka Server高可用集群概述


我们知道Eureka Client会定时连接Eureka Server,获取服务注册表中的信息并缓存到本地,微服务在消费远程API的时候不用每次去Server端查询,而是使用本地缓存的数据,这样的话,一般来讲即使Server宕机,也不会影响微服务之间的调用,但是肯定会影响Client端服务的更新,所以生产环境中,高可用的Eureka Server是必不可少的。


Eureka Server可以通过运行多个实例并相互注册的方式来实现高可用。 Eureka Server实例会彼此增量的同步信息,确保所有节点数据一致。


来看下Stand Alone模式的配置

registerWithEureka: false
fetchRegistry: false


20190103225659244.png

所以集群环境下,需要保持默认值,即 true . 详见源码 EurekaClientConfigBean



20190103230814933.png20190103230832247.png


2个Eureka Server节点高可用集群搭建步骤

Step1. 新建子模块 microservice-discovery-eureka-ha



20190103231116134.png


关键pom

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>



Step2. 配置hosts


linux : /etc/hosts

windows : C:\Windows\System32\drivers\etc\hosts20190103231433123.png

Step3. application.yml注册两个Eureka Server

spring: 
  # 注册到eureka上的微服务名称
  application: 
    name: microservice-discovery-eureka-ha  
---  
spring:
  # 指定profiles为peer1
  profiles: peer1
server:
  port: 8761
eureka:
  instance:
    # 当profiles为peer1,hostname是peer1 ,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
    hostname: peer1
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      # 将自己注册到peer2这个Eureka上
      defaultZone: http://peer2:8762/eureka/
---
spring:
  # 指定profiles为peer2,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
  profiles: peer2
# 端口  
server:
  port: 8762
# Eureka 
eureka:
  instance:
    # 当profiles为peer2,hostname是peer2
    hostname: peer2
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      # 将自己注册到peer1这个Eureka上
      defaultZone: http://peer1:8761/eureka/


application.yml中使用连字符 --- 将配置文件分为三段,第二段和第三段分别为spring. profiles指定了名称,该名称表示它所在的那段内容应该在哪个profile中。

第一段未指定spring. profiles,即对所有的profile生效


20190103231842515.png


上述配置文件,指定了hostname ,与上一步配置的hostname保持一致,同时让peer1 和 peer2 相互注册,即peer1注册到peer2上

20190103232038986.png


Step4. 启动测试


方法一: 在STS中配置Run Configurations

主类右键 Run As — Run Configurations --Spring Boot App


2019010323242468.png


同理 peer2


20190103232501685.png

方法二: 打包成jar,运行jar

主类右键 Run As — Run Configurations – Maven Build ,通过maven clean package 组合命令,打成jar包20190103232613944.png


然后通过

java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active


java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

通过spring.profiles.active指定使用哪个profile启动。


分别启动peer1 和 peer2 , 首先启动peer1会报错java.net.ConnectException: Connection refused: connect, 因为peer2 还未启动,等一会即可。


Step5. 查看服务注册中心


访问: http://peer1:8761/

20190103233119682.png


访问: http://peer2:8762/

20190103233152848.png

3个Eureka Server节点高可用集群搭建步骤


Eureka Server不向ZK必须奇数个节点,便于选举。 Eureka Server对节点的个数只要2个以上即可。

步骤同上,主要看下application.yml


方式一

spring: 
  # 注册到eureka上的微服务名称
  application: 
    name: microservice-discovery-eureka-ha-3nodes  
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/,http://peer3:8789/eureka/      
---  
spring:
  # 指定profiles为peer1
  profiles: peer1
server:
  port: 8787
eureka:
  instance:
    # 当profiles为peer1,hostname是peer1 ,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
    hostname: peer1
---
spring:
  # 指定profiles为peer2,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
  profiles: peer2
# 端口  
server:
  port: 8788
# Eureka 
eureka:
  instance:
    # 当profiles为peer2,hostname是peer2
    hostname: peer2
---
spring:
  # 指定profiles为peer3,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
  profiles: peer3
# 端口  
server:
  port: 8789
# Eureka 
eureka:
  instance:
    # 当profiles为peer3,hostname是peer3
    hostname: peer3


在公共的配置中指定defaultZone,配置三个 ,peer1 peer2 peer3中不相互注册


方式二

spring: 
  # 注册到eureka上的微服务名称
  application: 
    name: microservice-discovery-eureka-ha-3nodes  
---  
spring:
  # 指定profiles为peer1
  profiles: peer1
server:
  port: 8787
eureka:
  instance:
    # 当profiles为peer1,hostname是peer1 ,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
    hostname: peer1
    prefer-ip-address: true 
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      # 将自己注册到peer2这个Eureka上
      defaultZone: http://peer2:8788/eureka/,http://peer3:8789/eureka/
---
spring:
  # 指定profiles为peer2,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
  profiles: peer2
# 端口  
server:
  port: 8788
# Eureka 
eureka:
  instance:
    # 当profiles为peer2,hostname是peer2
    hostname: peer2
    prefer-ip-address: true  
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      # 将自己注册到peer1这个Eureka上
      defaultZone: http://peer1:8787/eureka/,http://peer3:8789/eureka/
---
spring:
  # 指定profiles为peer3,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动
  profiles: peer3
# 端口  
server:
  port: 8789
# Eureka 
eureka:
  instance:
    # 当profiles为peer3,hostname是peer3
    hostname: peer3
    prefer-ip-address: true  
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
  client:
    serviceUrl:
      # 将自己注册到peer1这个Eureka上
      defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/



不在公共的配置中指定defaultZone,在peer1 peer2 peer3中相互注册


都可以,均验证通过。


将服务注册到Eureka Server集群上及服务调用


这里我们使用micorservice-provider-user作为演示,修改下defaultZone的地址

20190103233936951.png


启动micorservice-provider-user,

同时也修改下 消费者工程 micorservice-consumer-movie-fegin


20190103234457666.png


启动 micorservice-consumer-movie-fegin

查看Eureka Server http://peer1:8761/


20190103234516787.png


访问 http://localhost:7901/movie/1


20190103234558404.png

验证通过


代码


2个Eureka Server节点

https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha


3个Eureka Server节点


https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha3


相关文章
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
1097 0
|
安全 NoSQL Java
JeecgBoot应用Spring Authorization Server
Spring Authorizaiton Server, 简称 sas,是一个授权服务器框架,提供 OAuth2.1 与 Open Connect 1.0 认证规范及其他规范的实现,它建立在 Spring Security 之上,为构建 OpenID Connect 1.0 Identity Provider 和 OAuth2 授权服务器产品提供了一个安全、轻量级和可定制的基础
656 3
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
955 5
|
安全 Java 开发者
Java一分钟之-Spring Cloud Netflix Eureka:服务注册与发现
【6月更文挑战第8天】Spring Cloud Eureka是微服务架构的关键,提供服务注册与发现功能。本文讲解Eureka工作原理、配置、常见问题及解决方案。Eureka包含Server(管理服务状态)和Client(注册服务实例并发现服务)。快速入门包括启动Eureka Server和创建Eureka Client。常见问题涉及服务注册不上、服务下线和客户端注册信息不准确,可通过检查网络、理解自我保护机制和配置元数据解决。此外,文中还提及健康检查、安全配置和集群部署等高级实践,以增强系统健壮性和扩展性。
772 8
|
Java Nacos 数据格式
Spring Cloud Nacos 详解:服务注册与发现及配置管理平台
Spring Cloud Nacos 详解:服务注册与发现及配置管理平台
1509 3
|
Java API 数据中心
Spring Cloud中的服务注册与发现实现方法
Spring Cloud中的服务注册与发现实现方法
|
负载均衡 Java API
Spring Cloud中的服务注册与发现策略
Spring Cloud中的服务注册与发现策略
|
移动开发 Java Maven
基于OSGi的Virgo Server最简单Spring web实例
基于OSGi的Virgo Server最简单Spring web实例
354 0