文章目录

  • 导读
  • 官方文档
  • Eureka Server高可用集群概述
  • 2个Eureka Server节点高可用集群搭建步骤
    • Step1. 新建子模块 microservice-discovery-eureka-ha
    • Step2. 配置hosts
    • Step3. application.yml注册两个Eureka Server
    • Step4. 启动测试
    • Step5. 查看服务注册中心
  • 3个Eureka Server节点高可用集群搭建步骤
    • 方式一
    • 方式二
  • 将服务注册到Eureka Server集群上及服务调用
  • 代码

导读

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

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


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

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

关键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\hosts


Step3. application.yml注册两个Eureka Server

spring: # 注册到eureka上的微服务名称application: name: microservice-discovery-eureka-ha  ---
spring:# 指定profiles为peer1profiles: peer1server:port: 8761eureka:instance:# 当profiles为peer1,hostname是peer1 ,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动hostname: peer1instance-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是peer2hostname: peer2instance-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生效

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


Step4. 启动测试

方法一: 在STS中配置Run Configurations

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

同理 peer2

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

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

然后通过

java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
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/

访问: http://peer2:8762/


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为peer1profiles: peer1server:port: 8787eureka: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是peer2hostname: peer2
---
spring:# 指定profiles为peer3,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动profiles: peer3
# 端口
server:port: 8789
# Eureka
eureka:instance:# 当profiles为peer3,hostname是peer3hostname: peer3

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


方式二

spring: # 注册到eureka上的微服务名称application: name: microservice-discovery-eureka-ha-3nodes  ---
spring:# 指定profiles为peer1profiles: peer1server:port: 8787eureka:instance:# 当profiles为peer1,hostname是peer1 ,以jar包的形式启动的时候通过spring.profiles.active参数指定使用哪个配置文件启动hostname: peer1prefer-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是peer2hostname: peer2prefer-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是peer3hostname: peer3prefer-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的地址

启动micorservice-provider-user,

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

启动 micorservice-consumer-movie-fegin

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

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

验证通过


代码

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

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

  1. Spring Cloud入门一 Eureka Server

    1.搭建父工程 主要是添加版本依赖,此处版本是: spring-boot  : 2.0.8.RELEASE spring-cloud : Finchley.SR2 <?xml version=& ...

  2. 新 Spring Cloud (一) 之 Eureka 服务注册中心

    文章目录 一.前言 0. 之前写过两篇Spring Cloud,但是感觉不够具体,所以重新写了一份. 1. SpringCloud 2. 什么是Eureka 3. 原理图 二.基本使用实例 1. 场景 ...

  3. 【夯实Spring Cloud】Spring Cloud中的Eureka服务注册与发现详解

    本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...

  4. Spring Cloud(一)Eureka Server-单体及集群搭建

    一.Enreka介绍 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCl ...

  5. Spring Cloud Finchley OpenFeign的重试配置相关的坑

    如题,本文基于Spring Cloud Finchley.SR2 OpenFeign的重试 OpenFeign配置重试后,逻辑分析 对比Daltson和Finchley的基本组件,发现Ribbon还有 ...

  6. 走进Spring Cloud之二 eureka注册中心(Greenwich版本)

    走进Spring Cloud之二 eureka注册中心(Greenwich版本) eureka 构建SpringCloud 工程 eureka 注册中心 eureka-server moudle po ...

  7. Spring Cloud Alibaba(13)---Sleuth概述

    Spring Cloud Alibaba(13)-Sleuth概述 Sleuth概述 前言 在微服务架构中,众多的微服务之间互相调用,如何清晰地记录服务的调用链路是一个需要解决的问题.同时,由于各种原 ...

  8. 基于Spring cloud Ribbon和Eureka实现客户端负载均衡

    前言 本案例将基于Spring cloud Ribbon和Eureka实现客户端负载均衡,其中Ribbon用于实现客户端负载均衡,Eureka主要是用于服务注册及发现: 传统的服务端负载均衡 常见的服 ...

  9. Spring Cloud(二) 配置Eureka Client

    前文回顾: Spring Cloud(一)Eureka Server-单体及集群搭建 本节我们将创建两个Eureka Client,注册到上节中的Eureka Server中,一个作为服务提供方,一个 ...

最新文章

  1. 完成这个例子,说出java中针对异常的处理机制。
  2. Live Source Address 2019最新的电视广播包_Arturia MiniLab MkII迷你MIDI键盘2019年换新装:反色纯黑版...
  3. 设计模式[3] -单例模式-代码
  4. 3种纯CSS实现中间镂空的12色彩虹渐变圆环方法
  5. 如何给Linux操作系统(CentOS 7为例)云服务器配置环境等一系列东西
  6. spring学习(47):bean的作用域
  7. python不用加号实现加法
  8. nginx之lua_shared_dict
  9. LeetCode 76. 最小覆盖子串 (滑动窗口哈希表)
  10. python pdf转html代码_python将html转成PDF的实现代码(包含中文)
  11. ubuntu18.04静态ip设置
  12. 【python实战】使用 pygame 写一个 flappy-bird 类小游戏 | 涉及思路+项目结构+代码详解 | 新手向
  13. Knockout.Js官网学习(event绑定、submit绑定)
  14. UNIX环境高级编程(第2版)第11-17章
  15. Webstorm如何下载插件
  16. 腾讯课堂后台扩容和性能优化实战
  17. linux强制安装rpm依赖包,Yum下载rpm包、不分析依赖关系强制安装
  18. CISCO ISIS
  19. JAVA医护人员排班系统计算机毕业设计Mybatis+系统+数据库+调试部署
  20. Spark宽窄依赖详解

热门文章

  1. Python基础——Anaconda的安装使用
  2. tensorflow 入门实例(二)
  3. 判断二叉树是否是平衡二叉树(dp tree)
  4. 推荐系统笔记(关键模块)
  5. pytorch笔记 torch.clamp(截取上下限)
  6. tensorflow从入门到精通100讲(六)-在TensorFlow Serving/Docker中做keras 模型部署
  7. 数据库性能调优之后,进一步的验证和分析
  8. Hadoop 在关机重启后,namenode启动报错
  9. python使用redis_Python操作redis系列之 列表(list) (五)
  10. mysql数据库DDL操作