构建高可用的Config Server、使用Spring Cloud Bus刷新配置

构建高可用的Config Server

当服务实例很多时,所有的服务实例需要同时从配置中心Config Server读取配置文件,这时可以考虑将配置中心Config Server做成一个微服务,并且将其集群化,从而达到高可用。配置中心Config Server 高可用的架构图如下图所示。ConfigServer和Config Client向Eureka Server注册,且将Config Server多实例集群部署。
在这里插入图片描述

构建Eureka Server

依赖:

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

配置:

server:
  port: 8761

spring:
  application:
    name: eureka-client

eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url: 
      defaultZone: http://localhost:${server.port}/eureka/
    

启动类:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
构建Config Server
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

配置:

server:
  port: 8769

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ActonZhang1024/springcloud-config.git
          search-paths: springcloud-config
      label: master

eureka:
  client:
    service-url: 
      defaultZone: http://localhost:8761/eureka/
构造Config Client
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

配置:bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: eureka-client
      profile: dev
      discovery:
        service-id: config-server
        enabled: true

配置:application.yml

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

依次启动eureka-server、config-server 和config-client工程,注意这里需要config-server启动成功并且向eureka-server 注册完成后,才能启动config-client, 否则config-client 找不到config-server。通过控制台可以发现,config-client向地址为htp://localhost:8769的config-server读取了配置文件。访问htp://localhost:8762/foo,浏览器显示:
在这里插入图片描述

可见,config-server 从远程Git仓库读取了配置文件,config-client 从config-server读取了配置文件。

那么如何搭建高可用的Config Server 呢?只需要将Config Server 多实例部署,用IDEA开启多个Config Server 实例,端口分别为8769和8768。在浏览器上访问Eureka Server 的主页htp://ocalhost:8761/,界面如图
在这里插入图片描述
多次启动config-client 工程,从控制台可以发现它会轮流地从ht:///clthost:/8768 和htp://ocalhost:8769的Config Server读取配置文件,并且做了负载均衡。

在这里插入图片描述
在这里插入图片描述

使用Spring Cloud Bus刷新配置

Spring Cloud Bus是用轻量的消息代理将分布式的结点连接起来,可以用于广播配置文件的更改或者服务的监管管理。一个关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。Spring Cloud Bus可选的消息代理组建包括RabbitMQ、AMQP和Kafka等。本节讲述的是用RabbitMQ作为SpringCloud的消息组件去刷新更改微服务的配置文件。

为什么需要用SpringCloudBus去刷新配置呢?
如果有几十个微服务,而每一个服务又是多实例,当更改配置时,需要重新启动多个微服务实例,会非常麻烦。Spring Cloud Bus的一个功能就是让这个过程变得简单,当远程Git仓库的配置更改后,只需要向某一个微服务实例发送一个 Post请求,通过消息组件通知其他微服务实例重新拉取配置文件。如下图所示,当远程Git 仓库的配置更改后,通过发送“/bus/refresh" Post请求给某一个微服务实例,通过消息组件,通知其他微服务实例,更新配置文件。
在这里插入图片描述
本节是在上一节的例子上进行改造的,只需要改造configclient工程。首先,需要在pom文件中引入用RabbitMQ实现的Spring Cloud Bus的起步依赖spring-loud-starter-bus-amqpo如果读者需要自己实践,则需要安装RabbitMQ服务器。pom文件添加的依赖如下:

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

在工程的配置文件application.yml添加RabbitMQ的相关配置,host 为RabbitMQ服务器的IP地址,port 为RabbitMQ服务器的端口,username 和password为RabbitMQ服务器的用户名和密码。通过消息总线更改配置,需要经过安全验证,为了方便讲述,先把安全验证屏蔽掉,也就是将management. security. enabled改为false。 代码清单如下:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
management:
  security:
    enabled: false

最后,需要在更新的配置类上加@ RefreshScope注解,只有加上了该注解,才会在不重启服务的情况下更新配置,如本例中更新配置文件foo变量的值。代码清单如下:

@RestController
@RefreshScope
public class Controller {
    @Value("${foo}")
    String foo;

    @RequestMapping("/foo")
    public String hi() {
        System.out.println(foo);
        return foo;
    }
}

依次启动工程,其中config-client开启两个实例,端口分别为8762和8763。启动完成后,在浏览器上访问htp://ocalhost:8762/foo或者htp://calhost:8763/foo,浏览器显示:
在这里插入图片描述
更改远程Git仓库,将foo的值改为“foo version 2”。通过Postman或者其他工具发送一个Post请求htp://ocahost:8762/bus/re/resh 请求发送成功,再访问htp://ocalhost:8762/foo或者http://localhost:8763/foo,浏览器都会显示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

可见,通过向8762端口的微服务实例发送Post请求http://localhost:8762/bus/refresh,请求刷新配置,由于使用了Spring Cloud Bus,其他服务实例(如案例中的8763端口的服务实例)会接收到刷新配置的消息,也会刷新配置。

另外,“a/bus/refresh" API接口可以指定服务,即使用“destination”参数,例如“/bus/refresh?destination=eureka-client:**”,即刷新服务名为eureka-client的所有服务实例。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值