Spring Cloud Eureka Server高可用之:在线扩容

Stella981
• 阅读 413

Spring Cloud Eureka Server高可用之:在线扩容

本文共 1591字,阅读大约需要 6分钟 !


概述

业务微服务化以后,我们要求服务高可用,于是我们可以部署多个相同的服务实例,并引入负载均衡机制。而微服务注册中心作为微服务化系统的重要单元,其高可用也是非常必要的,因此在生产中我们可能需要多个微服务注册中心实例来保证服务注册中心的稳定性。本文就以 Eureka微服务注册中心为例,来实践一下如何 在线扩充 Eureka Server实例来保证 微服务注册中心的高可用性!

注: 本文首发于 My Personal Blog,欢迎光临 小站

本文内容脑图如下:

Spring Cloud Eureka Server高可用之:在线扩容


原理与实验流程介绍

我们欲模拟如下过程:

  • 首先启动一个 eureka-server实例(eureka-server-1)

  • 再启动一个 eureka-client实例(eureka-client-1)并注册到 eureka-server-1中

  • 接下来我们启动第二个 eureka-server实例:eureka-server-2,并且让已启动的 eureka-server-1 和 eureka-client-1在不重启的情况下来感知到 eureka-server-2的加入

  • 同理我们可以在启动第三个 eureka-server实例:eureka-server-3,并且让已启动的 eureka-server-1 、eureka-server-2 和 eureka-client-1 在不重启的情况下来感知到 eureka-server-3的加入

  • 更多 eureka-server实例的加入原理完全相同,不再赘述

这样一来我们便实现了微服务注册中心的动态在线扩容!

而如何才能让已启动的服务能够在不重启的情况下来感知到新的 eureka-server 的加入呢?

为此我们引入 Spring Cloud Config 配置中心 config-server,并将 eureka-server和 eureka-client服务的配置文件由 config-server进行统一管理,这样一来对配置文件的修改如果可以通过某种机制来通知已启动的服务,那么问题便迎刃而解了!

我们给出整个过程的原理图如下:

Spring Cloud Eureka Server高可用之:在线扩容

接下来我们来实践这整个过程!


基础工程搭建

  • 创建一个 config-server工程

pom中关键依赖如下:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

主类添加相应注解:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
  ...
}

bootstrap.yml配置文件如下:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/hansonwang99/xxxx
          username: xxxx
          password: xxxx
server:
  port: 1115

该 yml配置很重要,其连接了预先准备好的 git仓库,后续 eureka-server 和 eureka-client 的配置文件都是存于该git仓库中的!

  • 创建一个 eureka-server工程

pom中关键依赖如下:

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

主类添加相应注解:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
  ...
}

bootstrap.yml重要配置如下:

spring:
  application:
    name: eureka-server
  cloud:
    config:
      uri: http://localhost:1115

注意该 yml中关于 spring cloud config 的配置同样非常重要,因为此 eureka-server需要从 config-server中拉取配置!

最后我们还需要添加一个 controller便于测试:

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    EurekaClientConfigBean eurekaClientConfigBean;

    @GetMapping("/eureka-service-info")
    public Object getEurekaServerUrl(){
        return eurekaClientConfigBean.getServiceUrl();
    }
}

这个 Rest Controller 接口的意图很简单:打印出当前到底有多少个 eureka-server实例在提供服务(从而可以直观的判断出 eureka-server的扩容是否已经成功)

  • 创建一个 eureka-client工程

pom中关键依赖如下:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

主类添加相应注解:

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

bootstrap.yml 重要配置如下:

spring:
  application:
    name: eureka-client
  cloud:
    config:
      uri: http://localhost:1115

这里基本同上面 eureka-server的配置,不再赘述

同样,我们也在 eureka-client中添加一个 controller便于测试:

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    EurekaClientConfigBean eurekaClientConfigBean;

    @GetMapping("/eureka-service-info")
    public Object getEurekaServerUrl(){
        return eurekaClientConfigBean.getServiceUrl();
    }
}

三个工程的创建到此完毕!下来我们来依次启动各个工程

  • 首先启动 config-server工程

  • 然后用 peer1配置文件来启动 第一个eureka server,指令如下:

    mvn spring-boot:run -Dspring.profiles.active=peer1

启动过程的开始打印出如下信息,一目了然:

Spring Cloud Eureka Server高可用之:在线扩容

当然这个 peer1配置文件实际物理存在于git仓库之上:

Spring Cloud Eureka Server高可用之:在线扩容

  • 接下来我们启动 eureka_client工程,指令如下:

    mvn spring-boot:run -Dspring.profiles.active=peer1

其依然需要通过 spring cloud config 去git仓库拉取 eureka-client的 peer1配置文件,这一点从 eureka-client的启动过程中也能看到:

Spring Cloud Eureka Server高可用之:在线扩容

  • 接下来我们用浏览器访问仅存在的一个 eureka-server微服务注册中心,可以看到有服务已经注册上去了:

Spring Cloud Eureka Server高可用之:在线扩容

既然 config-server / eureka-server / eureka-client 三个服务已经启动了,那么接下来我们来测试一下:

浏览器访问 eureka-client的 Rest接口:localhost:1114/test/eureka-service-info

Spring Cloud Eureka Server高可用之:在线扩容

同理,浏览器访问 eureka-server的 Rest接口:localhost:1111/test/eureka-service-info

Spring Cloud Eureka Server高可用之:在线扩容

OK,一切正常,但目前微服务注册中心仅一个 eureka-server实例,下来我们来 在线动态扩容微服务注册中心实例


扩充一次 Eureka Server

接下来我们再用 peer2配置文件来 启动第二个 eureka server,指令如下:

mvn spring-boot:run -Dspring.profiles.active=peer2

启动完毕后,浏览器访问微服务注册中心第一个实例 eureka-server-1:localhost:1111/ 发现第二个微服务注册中心实例 eureka-server-2已经成功启动并加入

Spring Cloud Eureka Server高可用之:在线扩容

为了让已启动的 eureka-client和 eureka-server-1能在线感知到 eureka-server-2的加入,此时我们需要修改两个地方:

  • 修改 Git上eureka-client的配置文件:

    server: port: 1114

    spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/,http://localhost:1112/eureka/ # 此处改为包含两个eureka-server

  • 修改 Git上第一个eureka-server(eureka-server-1)的配置文件:

    server: port: 1111

    spring: application: name: eureka-server eureka: instance: hostname: localhost preferIpAddress: true client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:1112/eureka/ # 此处改为第二个eureka-server地址 server: waitTimeInMsWhenSyncEmpty: 0 enableSelfPreservation: false

Git仓库里配置文件的修改完毕并不能触发已启动的 eureka-client和 eureka-server-1 在线更新,我们还需要向 eureka-client服务和 eureka-server-1服务来 POST两个请求来激活刚才所修改的配置:

POST localhost:1111/actuator/refresh  // 激活 eureka-client服务的配置
POST localhost:1114/actuator/refresh // 激活 eureka-server-1 服务的配置

POST请求一旦发出,我们从控制台里能直观看到更新配置文件的详情:

Spring Cloud Eureka Server高可用之:在线扩容

配置文件更新完毕,浏览器再次访问 eureka-client的 Rest接口:localhost:1114/test/eureka-service-info

Spring Cloud Eureka Server高可用之:在线扩容


再扩充一次 Eureka Server

接下来还可以再用 peer3配置文件来启动第三个 eureka server加入高可用微服务注册中心集群,过程和上面类似,此处不再赘述!当然更多 eureka-server实例的扩充原理也相同。


后记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!



点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Eureka Server 开启Spring Security Basic认证
!Desktop(https://uploadimages.jianshu.io/upload_images/98242475ce94f98ae00f42f.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共503字,阅读大约需要2分钟!概述
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这