SpringCloud(十五)——bus组件:实现远端配置修改自动刷新
实现自动配置刷新
MQ服务主机:47.106.105.80:15672
MQ端口号:15672(web管理界面) 5672(Java)
虚拟主机:ems
1、在springcloud-config-server-3344config服务端引入依赖
<!--引入bus依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2、配置config Server配置文件,通过bus连接MQ的配置
server:
port: 3344
spring:
application:
name: springcloud-config-server
# 连接码云远程仓库
cloud:
config:
server:
git:
# 注意是https的而不是ssh
uri: https://gitee.com/luoxiao1104/springcloud-config.git
# 通过 config-server可以连接到git,访问其中的资源以及配置~
default-label: master
#通过bus组件连接到MQ服务
rabbitmq:
host: 47.106.105.80 #连接主机
port: 5672 #连接mq端口
username: guest #连接mq用户名
password: guest #连接mq密码
virtual-host: /
#Eureka配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-cofig-server-3344 #修改Eureka默认描述信息
启动springcloud-config-server-3344config服务端查看
3、配置微服务config client通过bus连接MQ服务
a.在所有微服务项目中(springcloud-config-client-3355)引入Bus依赖
<!--引入bus依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
b.在所有的微服务项目中配置MQ配置,即配置远端的配置文件
注意这个bootstrap的配置并不是最终配置,需要通过这里面的配置拉取远端的配置,获取远端配置启动,因此需要将这段配置放入远端的仓库进行管理
c.在config客户端bootstrap.yaml添加下面配置
d.启动springcloud-config-client-3355config客户端
4、在springcloud-config-server-3344config服务中配置application.yaml暴露所有端点
现在消息总线和config连接到一起,以后只需要在config Server发一次POST请求,所有的config client配置文件都会发生改变。
指定服务刷新配置实现
- 指定端口刷新某个具体服务: curl -X POST http://localhost:3344/actuator/bus-refresh/configclient:9090
- 指定服务id刷新服务集群节点: curl -X POST http://localhost:3344/actuator/bus-refresh/configclient
集成webhook实现自动刷新
webhooks
根据远程仓库触发对应事件发送一个web请求默认就是POST方式请求
一、在远端仓库中配置webhooks
1、在springcloud-config-server-3344config服务器中添加过滤器(必须要添加,不然后面配置webhooks会报错)
package com.study.springcloud.filters;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@Component
public class UrlFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse = (HttpServletResponse)response;
String url = new String(httpServletRequest.getRequestURI());
//只过滤/actuator/bus-refresh请求
if (!url.endsWith("/bus-refresh")) {
chain.doFilter(request, response);
return;
}
//获取原始的body
String body = readAsChars(httpServletRequest);
System.out.println("original body: "+ body);
//使用HttpServletRequest包装原始请求达到修改post请求中body内容的目的
CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest);
chain.doFilter(requestWrapper, response);
}
@Override
public void destroy() {
}
private 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();
}
};
}
}
public static String readAsChars(HttpServletRequest request)
{
BufferedReader br = null;
StringBuilder sb = new StringBuilder("");
try
{
br = request.getReader();
String str;
while ((str = br.readLine()) != null)
{
sb.append(str);
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != br)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return sb.toString();
}
}
2、在主启动类中添加扫描包注解
3、在Gitee仓库中点击管理,再点击webhooks,最后点击添加
4、借助内网穿透(把内网地址转化为公网地址)使用natapp https://natapp.cn/
- a.访问官方网址
- b.注册 & 实名认证
- c.购买免费通道
- d.配置隧道 ==>配置本地端口 config server端口 3344
- e.下载客户端 antapp.exe
- f.在cmd命令行打开 启动natapp 输入
natapp --authoken=a2164bdbdfe99d11(自己的验证码)
5、输入信息
6、添加成功