Spring Cloud 消息总线(BUS)实现自动刷新
附:natapp内网穿透 详细文档
ps:springboot 版本:2.1.7.RELEASE,springcloud版本:Greenwich.SR2
一.config-server:
1.引入相应jar包:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
2.修改配置文件:application.yml
server:
port: 8765
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/LuciferZK/config-repo
username: 13871xxxxx@163.com
password: ---------*******
#basedir: D:\project\morning-star\config-server\config\basedir
rabbitmq:
host: 192.168.47.131
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
3.主配置类添加@EnableConfigServer注解,开启config
@EnableConfigServer
@SpringBootApplication
public class MorningConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MorningConfigApplication.class, args);
}
}
二:config-client
1.引入相应jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2:控制层添加RefreshScope注解;否则无法刷新看到效果
3.配置文件:bootstrap.yml
spring:
application:
name: morning-service-order
cloud:
config:
uri: http://localhost:8765
profile: dev
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: 192.168.47.131
port: 5672
username: guest
password: guest
三、使用postman测试:http://localhost:8765/actuator/bus-refresh
并无任何问题。如果想自动刷新的话,不可能每次去使用postman请求一次,所以按照如下操作即可:
四:github上操作:
(1)设置
(2)添加
ps:**Payload URL这个地址需要是外网能够访问的地址,而不是只是本地的http://localhost:8765/actuator/bus-refresh,没有域名,因此我这里就用到内网穿透工具。**
我这里只是用的免费版的,免费的缺点是映射的外网网址不会固定不变,随机的;
具体操作看附的连接中的文档;工具双击打开:
这里的forwarding 的地址就是填在github上Payload URL的地址;再次提醒,免费版的,映射地址会变动的,除非你cmd不关闭。
上述操作完毕后,用postman测试,并无问题,但是你在github上修改配置文件,github自动请求你的http://\*\*\*\*/[actuator/bus-refresh][http_localhost_8765_actuator_bus-refresh]的时候,你的config-server会报错,报错内容就是json解析失败.
五:增加filter过滤器,并重写getInputStream方法
package com.lucifer.config.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @author: lucifer
* @date: 2019/8/27
* @description:
*/
@Component
//@WebFilter(urlPatterns = "/**", filterName = "BusFilter")
public class BusFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) req;
String requestURI = httpServletRequest.getRequestURI();
//过滤掉非/actuator/bus-refresh请求
if (!requestURI.endsWith("/actuator/bus-refresh")) {
filterChain.doFilter(req, res);
return;
}
RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
filterChain.doFilter(requestWrapper, res);
}
@Override
public void destroy() {
}
@Override
public void init(FilterConfig arg0) {
}
}
package com.lucifer.config.filter;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* @author: lucifer
* @date: 2019/8/27
* @description:
*/
public class RequestWrapper extends HttpServletRequestWrapper {
public RequestWrapper(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 int read() throws IOException {
return byteArrayInputStream.read();
}
@Override
public boolean isFinished() {
return byteArrayInputStream.read() == -1 ? true : false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
}
还没有评论,来说两句吧...