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


相关文章
|
7月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
4月前
|
存储 数据可视化 Java
基于MicrometerTracing门面和Zipkin实现集成springcloud2023的服务追踪
Sleuth将会停止维护,Sleuth最新版本也只支持springboot2。作为替代可以使用MicrometerTracing在微服务中作为服务追踪的工具。
221 1
|
3月前
|
JSON Java 测试技术
SpringCloud2023实战之接口服务测试工具SpringBootTest
SpringBootTest同时集成了JUnit Jupiter、AssertJ、Hamcrest测试辅助库,使得更容易编写但愿测试代码。
98 3
|
5月前
|
消息中间件 存储 Java
SpringCloud基础9——服务异步通信-高级篇
消息可靠性、死信交换机、惰性队列、MQ集群
SpringCloud基础9——服务异步通信-高级篇
|
5月前
|
Java API 对象存储
微服务魔法启动!Spring Cloud与Netflix OSS联手,零基础也能创造服务奇迹!
这段内容介绍了如何使用Spring Cloud和Netflix OSS构建微服务架构。首先,基于Spring Boot创建项目并添加Spring Cloud依赖项。接着配置Eureka服务器实现服务发现,然后创建REST控制器作为API入口。为提高服务稳定性,利用Hystrix实现断路器模式。最后,在启动类中启用Eureka客户端功能。此外,还可集成其他Netflix OSS组件以增强系统功能。通过这些步骤,开发者可以更高效地构建稳定且可扩展的微服务系统。
94 1
|
6月前
|
负载均衡 监控 Java
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
9435 6
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
|
6月前
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
7月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
181 3
|
6月前
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
|
6月前
|
Java Spring
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动
【Azure 服务总线】Spring Cloud 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动