Eureka Server 实现在线扩容

作者:Grey

原文地址:

博客园:Eureka Server 实现在线扩容

CSDN:Eureka Server 实现在线扩容

需求

Eureka 是 Spring Cloud Netflix 套件中的服务注册中心组件,作为微服务的核心组件,需要支持在线扩容的需求。

本示例模拟 Eureka 从单实例在线扩容到三个实例的过程。

环境

Java 版本:17

Spring Boot 版本:2.7.5

Spring Cloud 版本:2021.0.4

项目结构和说明

  • eureka-scale-out-online:父项目名称

    • server : 服务端模块

      • src/
      • pom.xml
    • client : 客户端模块
      • src/
      • pom.xml
    • config : 配置中心模块,使用本地配置
      • src/
      • pom.xml
    • pom.xml:父项目 pom 配置

其中 config 项目存放了 server 和 client 的配置,基于 Spring Cloud Config ,方便起见,我们配置成 native,即本地配置模式,所以如果有配置修改,需要重启 config 项目,如果使用 Git 来托管配置,则无须重启。

完整代码见:Github

单个 Eureka 实例

将 config 项目中的 eureka-client.yml 修改为

server:
port: 8081 spring:
application:
name: eureka-client1 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

将 config 项目中的 eureka-server-peer1.yml 修改为

server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false

依次启动 config 模块(运行 ConfigStartApp.java 这个类),server 模块(运行 ServerStartApp.java 这个类),client 模块(运行 ClientStartApp.java 这个类),

注:server 模块在启动过程中,需要指定 peer1 这个配置文件。

启动完毕后,可以通过浏览器访问:http://localhost:8761/

看到目前的服务注册情况

目前 Eureka Server 只有一个实例。

在线扩充到两个 Eureka 实例

保持上述运行环境,先不做任何重启动作,由于要增加一个实例,所以将 config 项目中的 eureka-server-peer2.yml 修改为

server:
port: 8762 eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false

同时,把 eureka-server-peer1.yml 内容调整为

server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8762/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false

客户端需要感知到两个 Eureka Server 的存在,所以,也要修改 内容为

server:
port: 8081
spring:
application:
name: eureka-client1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

配置修改完毕后,

首先,重启 config 模块(注:由于配置的是 native 模式,需要重启,如果用 Git 托管,则无须重启)

然后启动 server peer2 实例,启动时做如下配置

接下来,使用 Eureka 的管理 API,将 client 模块实例和 server peer1 实例的配置进行热更新,操作如下

使用命令行或者 Postman 工具,发送如下请求

通过浏览器再次访问:http://localhost:8761/,出现两个实例

在线扩充到三个 Eureka 实例

同理,维持上述运行实例,先不要做任何重启动作,修改 eureka-client.yml 配置,让客户端感知到三个实例

server:
port: 8081
spring:
application:
name: eureka-client1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

修改 peer1 的配置

server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false

使得 peer1 可以感知到另外两个实例,同理,修改 peer2 实例配置

server:
port: 8762
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false

修改 peer3 实例配置

server:
port: 8763
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false

接下来,重启配置中,即 config 模块

然后,启动第三个实例,profile 指定 peer3

然后,使用 Eureka 的管理 API,刷新 peer1,peer2 的配置信息

再次通过浏览器访问:http://localhost:8761/

出现三个实例,扩容成功。

完整代码见:Github

参考资料

重新定义 Spring Cloud 实战

Eureka Server 实现在线扩容的更多相关文章

  1. Eureka实战-1【Eureka Server在线扩容】

    1.准备工作 PS:为了偷懒,每个pom文件都要依赖的公共依赖配置放在下面: <parent> <groupId>org.springframework.boot</gr ...

  2. Spring Cloud Eureka Server高可用注册服务中心的配置

    前言 Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务. 那么当成千上万个微服务注册到Eureka Server中的时候,Eurek ...

  3. Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

    前言 Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务. 那么当成千上万个微服务注册到Eureka Server中的时候,Eurek ...

  4. Eureka Server设计(转载 石杉的架构笔记)

    目录: 一.问题起源 二.Eureka Server设计精妙的注册表存储结构 三.Eureka Server端优秀的多级缓存机制 四.总结 一.问题起源 Spring Cloud架构体系中,Eurek ...

  5. Linux LVM在线扩容

    环境: 虚拟化环境,SUSE Linux Enterprise Server 11sp3,直接把虚拟磁盘从100G改成150G. 现有的LVM是100G,/home 的LV需要再加50G. 步骤: f ...

  6. Eureka详解系列(五)--Eureka Server部分的源码和配置

    简介 按照原定的计划,我将分三个部分来分析 Eureka 的源码: Eureka 的配置体系(已经写完,见Eureka详解系列(三)--探索Eureka强大的配置体系): Eureka Client ...

  7. KingbaseES R3 读写分离集群在线扩容案例

    案例说明: 1. 通过sys_basebackup创建新备库. 2. 将备库加入到Cluster nodes管理,可以用kingbase_monitor.sh一键启停. 3. 主备复制切换测试. 此次 ...

  8. LVM在线扩容

    我虚拟机根分区已经使用了35%,现在需要对他进行在线扩容,扩容之后使用率降到30% [root@localhost ~]# dfFilesystem 1K-blocks Used Available ...

  9. 关于MySQL的在线扩容

    原文地址:http://bucketli.iteye.com/blog/1294032 主要简单总结下,mysql在线扩容和缩容一般涉及到的内容,主要包括三个方面,1.在线也就意味着需要把增量的数据重 ...

随机推荐

  1. linux scsi相关的一些学习笔记

      最近看scsi相关处理的一些备忘,比较零碎,仅作参考. 先从最显而易见的打印入手: [0:0:0:0] disk ATA INTEL SSDSC2BX20 0150 - [0:0:1:0] dis ...

  2. Spark基础入门(01)—RDD

    1,基本概念 RDD(Resilient Distributed Dataset) :弹性分布式数据集 它是Spark中最基本的数据抽象,是编写Spark程序的基础.简单的来讲,一个Spark程序可以 ...

  3. IO流----读取文件,复制文件,追加/插入文件

    文件结构 读取文件 第一种方式 public class Test { public static void main(String[] args) throws IOException { // 最 ...

  4. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  5. Util和Helper类

    Util和Helper Util Util类,应该是一个无状态的类,只有静态方法. 比如在获取某些类的全局实例化对象的时候可以使用. public class ParamUtil { ... publ ...

  6. Java连接简单使用ElasticSearch

    目录 1. 添加依赖 2. 代码,无账号密码 3. 代码,有账号密码,并且是https方式 4. 参考文章 1. 添加依赖 <!-- https://mvnrepository.com/arti ...

  7. django_day03

    django_day03 Django的view(视图) CBV和FBV FBV:function based view 基于函数的视图 CBV:class based view 基于类的视图 fro ...

  8. 使用plsql_plprofiler 分析过程块的执行

    前言:存储过程可能涉及很多的SQL及控制块,我们看到的执行时间是整个过程块的执行时间,如果我们认为性能有问题,我们只能逐条SQL的分析,查找问题SQL,效率非常低下.KingbaseES 提供了 pl ...

  9. Linux 压缩、解压缩命令

    Linux 压缩.解压缩命令 tar 语法命令 tar [options-] [files] options: 选择 描述 -A 追加tar文件至归档 -c 创建一个新文档 -d 找出归档和文件系统的 ...

  10. SQL语句中过滤条件放在on和where子句中的区别和联系

    摘要: 介绍在多表关联SQL语句中,过滤条件放在on和where子句中的区别--inner join中没区别,外连接就不一样. 综述   蚂蚁金服的一道SQL面试题如下:SQL语句中,过滤条件放在on ...