美文网首页
Spring Cloud 10 -- 通过 Bus 刷新从配置中

Spring Cloud 10 -- 通过 Bus 刷新从配置中

作者: 半碗鱼汤 | 来源:发表于2019-08-12 18:52 被阅读0次

一、对 client01 添加 Bus 相关依赖

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

二、修改配置文件,添加配置

提示:使用消息总线 Bus ,需要 rabbitMQ

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh

完整如下

server:
  port: 8763

spring:
  application:
    name: client01
  # rabbitMQ
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
  # bus 相关配置
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true

# bus 相关配置
management:
  endpoints:
    web:
      exposure:
        include: "*"


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  # 为了在服务注册中心里显示实际的 IP 地址,需添加下面的配置
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true

# 为了打开 feign 的 hystrix 功能
feign:
  hystrix:
    enabled: true

三、在控制器上面添加注解 @RefreshScope

注意:是在控制器上面添加的

import com.dhsg.sc.client01.service.IUseOtherApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 添加注解 @RefreshScope 开启 spring cloud bus 功能
 */

@RestController
@RefreshScope
public class HelloController {

    @Autowired
    private IUseOtherApiService userOtherApiService;

    // 该注解可以读取配置文件中的值赋予下面的变量
    @Value("${server.port}")
    private String port;

    // 获取 Config Server 的配置文件中的 myValue 的值
    @Value("${myValue}")
    private String myValue;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(@RequestParam(value = "name", defaultValue = "dhsg") String name) {
        return "Hello " + name + " ,I am from port:" + port;
    }

    /**
     * 该接口是通过 feign 组件,访问 client02 的接口
     *
     * @return client02 的端口和服务名
     */
    @RequestMapping(value = "/getclient02name", method = RequestMethod.GET)
    public String getclient02name() {
        return userOtherApiService.getClient02Name();
    }

    /**
     * 该接口是为了获取 config server 的配置文件信息
     */
    @RequestMapping(value = "/config", method = RequestMethod.GET)
    public String config() {
        return myValue;
    }
}

四、访问 http://localhost:8763/config

结果

修改远程配置文件的 myValue 为 dev-1.6,然后再次访问,发现还是 dev-1.5

五、手动刷新

使用 postman ,用 post 方式访问 http://localhost:8763/actuator/bus-refresh
发现 client01 会再次连接配置中心,从而刷新配置文件的内容,此时再次访问 http://localhost:8763/config 。发现 dev-1.5 变成了 dev-1.6

六、全局刷新以及局部刷新

全局刷新:
访问 http://localhost:8763/actuator/bus-refresh 会全局刷新,所有的从配置中心获取配置的 client 都会被刷新

全局刷新

局部刷新:
在上面的 URL 基础上加上后缀 " /服务名 "。
例如:现在有 client01 和 client02 ,如果只要刷新 client01 ,那么访问 http://client01(ip+port)/actuator/bus-refresh/client01 ,如果只要刷新 client02 ,那么访问 http://client02(ip+port)/actuator/bus-refresh/client02
另外,如果 /actuator/bus-refresh 后面的后缀跟的是哪一个服务,前面的 ip + port 所在的服务都会被刷新。

七、自动刷新

其实就是在远程 git 仓库做一个配置,当有有修改的时候,自动去发送 post 请求 /actuator/bus-refresh 。

相关文章

网友评论

      本文标题:Spring Cloud 10 -- 通过 Bus 刷新从配置中

      本文链接:https://www.haomeiwen.com/subject/ddzujctx.html