在《带你入门SpringCloud统一配置 | SpringCloud Config》中经过 SpringCloud Config 完成了统一配置基础环境搭建,可是并无实现配置修改自动更新的操做(GitHub 或Gitee 修改配置后,须要重启配置服务才能更新配置)。html
本文是《带你入门SpringCloud统一配置 | SpringCloud Config》的续篇,经过 SpringCloud Bus 完成配置修改自动更新的操做介绍。java
阅读本文前须要你先移步《带你入门SpringCloud统一配置 | SpringCloud Config》由于本文是在其基础上进行讲解的。git
另外须要你熟悉 SpringBoot 项目的基本使用便可,还有一点须要注意的是在操做过程当中尽可能和我本地环境一致,由于环境不一致可能会带来一些问题。我本地环境以下:github
接下来就开始 SpringCloud Bus 环境搭建操做介绍!web
第一步:安装并启用 RabbitMQ,这里就不作详细介绍了。能够查看以前的总结:Windows 环境安装 RabbitMQspring
若是你的 RabbitMQ和 Config Server 端不在一台机器上,或者端口、用户名、密码不是使用的默认配置,那么你须要进行以下配置在 Config Server 端 application.properties 中
spring.rabbitmq.host=rabbitmq 服务IP地址
spring.rabbitmq.port=rabbitmq 服务端口号
spring.rabbitmq.username=rabbitmq 服务用户名
spring.rabbitmq.password=rabbitmq 服务密码app
第二步:在 Config Server 端端和客户端都引入 spring-cloud-starter-bus-amqp 依赖。具体代码以下:dom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
第三步:在Config Server 端的 application.properties 中须要添加以下配置,目的是把刷新配置接口暴露出来,具体配置以下:ide
management.endpoints.web.exposure.include= *
第四步:Config Client 端引入openfeign starter 依赖,具体代码以下:post
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
第五步:验证 Config Server 端和 Config Client 端是否在 RabbitMQ 注册队列和是否注册到 Eureka Server 端上。以下图所示:
须要先启动 Eureka Server 服务端,而后在依次启动 Config Server端和Config Client端。
第六步:Config Client 端上访问配置的 Controller 声明刷新配置做用域@RefreshScope。具体代码以下:
@RestController @RefreshScope public class EvnController { @Value("${env}") private String env; @RequestMapping("/env") public String evn() { return this.env; } }
不添加 @RefreshScope注解,配置更新后不会生效。
若是是自定义前缀配置须要在@ConfigurationProperties下添加 @RefreshScope便可,
最七步在 Gitee 上配置 WebHooks,具体操做方式以下图所示:
进入Gitee 点击管理,而后点击 WebHooks。
点击添加按钮添加新的 WebHooks 设置。
输入本地映射外网访问域名+/actuator/bus-refresh,本身测试可使用 NATAPP 进行内网穿穿透配置。具体配置请查看 https://natapp.cn/。
手动访问更新连接进行测试
修改配置在码云远程仓库上,而后使用 PostMan 访问:http://localhost:8080/actuator/bus-refresh ,以下图所示:
这里演示直接是在码云上操做,至关于执行 git push 操做。
而后在查看商品服务(Config Client端)配置是否生效。以下图所示自动更新成功!
WebHooks 测试
进入 WebHooks 设置,而后点击测试。会报以下图所示错误:
解决方案:
参考来CSDN 做者 tinysakurac 解决方案。
文章:《解决使用spring cloud config bus使用webhook自动刷新出现的400问题》 (https://blog.csdn.net/m0_3755...
问题产生缘由:GitHub在进行 POST 请求的同时默认会在 Body 加上这么一串载荷(payload),而 /actuator/bus-refresh 接口没有进行接受这些信息处理,因此就报错了。
解决问题思路:经过拦截器拦截 bus-refresh请求,而后在 HttpServletRequestMapper 包装类将 Request中 Body 内容置空。具体代码以下:
拦截 bus-refresh请求Filter 类。
public class BusRefreshFilter implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest)request; String url = new String(httpServletRequest.getRequestURI()); //只过滤/actuator/bus-refresh请求 if (!url.endsWith("/bus-refresh")) { chain.doFilter(request, response); return; } //使用HttpServletRequest包装原始请求达到修改post请求中body内容的目的 CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest); chain.doFilter(requestWrapper, response); } }
自定义 HttpServletRequestWrapper 类
public class CustometRequestWrapper extends HttpServletRequestWrapper{ public CustometRequestWrapper(HttpServletRequest request) { super(request); } @Override public ServletInputStream getInputStream() throws IOException { byte[] bytes = new byte[0]; ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); return new ServletInputStream() { @Override public boolean isFinished() { return byteArrayInputStream.read() == -1 ? true:false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } @Override public int read() throws IOException { return byteArrayInputStream.read(); } }; } }
将拦截 bus-refresh请求Filter 添加到 Spring 上下文中配置类。
@Configuration public class FilterConfig { @Bean public FilterRegistrationBean<BusRefreshFilter> filterRegistration() { FilterRegistrationBean<BusRefreshFilter> registration = new FilterRegistrationBean<BusRefreshFilter>(); registration.setFilter(new BusRefreshFilter()); List<String> urlList = new ArrayList<String>(); urlList.add("/*"); registration.setUrlPatterns(urlList); registration.setName("BusRefreshFilter"); registration.setOrder(1); return registration; } }
而后修改Gitee 上的配置信息后就能够自动更新了,这里就不在进行演示操做来。
还有一种方式是经过访问 Config Server端域名/monitor 来取代 Config Server端域名//actuator/bus-refresh。本方式我的尝试没有成功!具体配置官网介绍以下:
Many source code repository providers (such as Github, Gitlab, Gitea, Gitee, Gogs, or Bitbucket) notify you of changes in a repository through a webhook. You can configure the webhook through the provider’s user interface as a URL and a set of events in which you are interested. For instance, Github uses a POST to the webhook with a JSON body containing a list of commits and a header (X-Github-Event) set to push. If you add a dependency on the spring-cloud-config-monitor library and activate the Spring Cloud Bus in your Config Server, then a /monitor endpoint is enabled.
在Config Server 添加 spring-cloud-config-monitor 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency>
而后在Config Client 添加以下配置:
参考:https://github.com/spring-cloud/spring-cloud-bus/issues/124
spring.cloud.bus.id=${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.profiles.active:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}
当将远程仓库配置文件修改后,经过GitHub 或者 Gitee 的 WebHooks 配置自动访问接口bus-refresh 来通知Config Server 端。
Config Server 端收到请求后将配置 clone 下来,而后经过消息队列(默认是RabbitMQ)将修改后的配置发送给 Config Client 端。Config Client 端收到消息后从新从Config Server 端获取最新的配置信息。
而WebHooks 配置请求接口 bus-refresh 和消息队列发送配置给 Config Client 端都是 SpringCloud Bus 帮助咱们完成的。
若是你按照上述方式搭建并未成功,能够参考我在GitHub 项目 spring-cloud-get-started 仓库中模块名为:
spring-cloud-config-eureka-service
spring-cloud-config-server
spring-cloud-config-product-service
进行对比查看是否配置有误。
spring-cloud-get-started 项目地址:https://github.com/zhuoqianmingyue/spring-cloud-get-started
http://www.javashuo.com/article/p-cztletnu-eb.html By tinysakurac