zoukankan      html  css  js  c++  java
  • 【Spring Cloud】Spring Cloud之整合Spring Cloud Bus以及最佳实践

    一、整合步骤

    1)加入Maven坐标

           <!-- actuator监控模块 -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-actuator</artifactId>
                    <version>2.0.3.RELEASE</version>
                </dependency>
                <!--amqp-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-amqp</artifactId>
                    <version>2.0.3.RELEASE</version>
                </dependency>
                <!--spring cloud bus-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
                    <version>2.0.2.RELEASE</version>
                </dependency>

    2) 添加配置

    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
    
    # 开放actuator/bus-refresh断点 management: endpoints: web: exposure: include: bus
    -refresh

    3)程序代码引入配置参数的地方,添加@RefreshScope 注解,如下:

    /**
     * @author zhangboqing
     * @date 2019-09-03
     */
    @RestController
    @RefreshScope
    public class UserController {
    
        @Value("${pro1}")
        private String pro1;
    
        @ApiOperation(value = "用户模块-GET操作")
        @GetMapping(value = "get")
        public ApiResponseT get() {
    
            return ApiResponseT.builder().setData(pro1);
        }
    
    }

    4)启动程序,访问打印参数。然后修改参数,post方式请求http://localhost:10802/actuator/bus-refresh来刷新程序参数值,再访问参数发现值自动就更新来。
    在同一个程序多实例的情况下,刷新其中一个实例,其他实例也会自动被刷新。

    二、Spring Cloud Bus整合最佳实践

    1)上面的整合步骤,在整个架构上如下图所示,这些微服务应用的实例中都引入了Spring Cloud Bus,所以它们都连接到了RabbitMQ的消息总线上。

    我们可以从Git仓库中配置的修改到发起actuator/bus-refresh的POST请求这一步可以通过Git仓库的Web Hook来自动触发。由于所有连接到消息总线上的应用都会接收到更新请求,所以在WebHook中就不需要维护所有节点内容来进行更新,这样就不用通过Web Hook来逐个进行刷新每个程序配置的问题。

    注意:actuator/bus-refresh断点有提供destination参数,用来定位具体要刷新的应用程序。此时总线上的各应用实例会根据destination属性的值来判断是否为自己的实例名,

    ,实例的默认命名按此规则生成:${spring.cloud.client.hostname}:${spring.applica-tion.name}:${spring.application.instance_id:${server.port}}};

    若符合才进行配置刷新,若不符合就忽略该消息。比如,我们可以请求actuator/bus/refresh?destination=user:9000或者actuator/bus/refresh?destination=user:**

     2)基于上面的架构优化及最佳实践,如下图:

     相关改动如下:

    1. 在Config Server中也引入Spring Cloud Bus,将配置服务端也加入到消息总线中来。

    2. actuator/bus/refresh请求不再发送到具体服务实例上,而是发送给Config Server,并通过destination参数来指定需要更新配置的服务或实例。
    通过上面的改动,我们的服务实例不需要再承担触发配置更新的职责。同时,对于Git的触发等配置都只需要针对ConfigServer即可,从而简化了集群上的一些维护工作。

  • 相关阅读:
    .NET 4.5 异步IO
    使用MANIFEST.MF文件来track War包做持续部署
    .NET 4.5 压缩
    自定义实现URL重写 04.18
    对.Net Framework的认识
    那点所谓的分布式——memcache
    不能选择FreeTextBox下拉列表框
    实战架构设计
    LoadRunner压力测试心得总结
    JavaScript的模块化:封装(闭包),继承(原型)
  • 原文地址:https://www.cnblogs.com/756623607-zhang/p/11485036.html
Copyright © 2011-2022 走看看