spring-boot-admin(以下简称SBA)与Spring Boot、Spring cloud项目以starter得方式自动集成,包括Server端和Client端
SBA监控包括应用的基本信息、logfile(在线实时浏览或者download)、JVM信息(线程信息、堆信息、非堆信息)、Web(API接口信息、最近100次API调用的信息)、应用中用户登录信息;监控指标很全面,但针对具体项目就要增加符合自己项目的内容了,比如如下两点:
自定义HttpTrace增加入参和出参
结果:
在spring-boot-admin中HttpTrace显示的信息包括session、principal、request、response、timeTaken和timestamp,但session、principal对该项目完全无用,request是HttpTrace的内部类显示信息包括:
final String method;
final URI uri;
//唯一可以扩展的地方
final Map> headers;
final String remoteAddress;
复制代码
response也是HttpTrace的内部类:
final int status;
//唯一可以扩展的地方
final Map> headers;
复制代码
唯一缺少的就是请求的入参和出参,而Headers的信息是无用的。所以扩展HttpTrace显示请求中的入参和出参势在必行,大致的思路是:自定义Filter-->装饰模式转换成自定义的request和response对象,内部获取请求和相应内容-->HttpExchangeTracer创建HttpTrace对象-->InmemoryHttpTraceRepository保存100次请求的HttpTrace对象,供server端使用。由于Filter中使用的部分对象是先创建的所以我们先从需要的零部件开始
第一步:包装HttpServletRequest获取请求内容:
public class RequestWrapper extends HttpServletRequestWrapper{
//存放请求的消息体(先缓存一份)
private byte[] body;
//自定义输入流的包装类,将缓存数据再写入到流中
private ServletInputStreamWrapper wrapper;
private final Logger logger = LoggerFactory.getLogger(RequestWrapper.class);
public RequestWrapper(HttpServletRequest request){
super(request);
try {
//使用Apache的commons-io工具从request中先读取数据
catch (IOException e) {
"从请求中获取请求参数出现异常:", e);
//将读取出来的内存再写入流中
new ServletInputStreamWrapper(new ByteArrayInputStream(body));
//转换成String 供外部调用,并替换转义字符
public String body(){
return new String(body).replaceAll("[\n\t\r]","");
//将我们的自定义的流包装类返回,供系统调用 读取数据
@Override
public ServletInputStream getInputStream() throws IOException{
return this.wrapper;
//将我们的自定义的流包装类返回,供系统调用 读取数据
@Override
public BufferedReader getReader() throws IOException{
return new BufferedReader(new InputStreamReader(this.wrapper));
//从给定的输入流中读取数据
static final class ServletInputStreamWrapper extends ServletInputStream{
private InputStream inputStream;
public ServletInputStreamWrapper(InputStream inputStream){
this.inputStream = inputStream;
@Override
public boolean isFinished(){
return true;
@Override
public boolean isReady(){
return false;
@Override
public void setReadListener(ReadListener listener){
//读取缓存数据
@Override
public int read() throws IOException{
return this.inputStream.read();
public InputStream getInputStream(){
return inputStream;
public void setInputStream(InputStream inputStream){
this.inputStream = inputStream;
复制代码
第二步:包装HttpServletResponse类获取响应内容:
public class ResponseWrapper extends HttpServletResponseWrapper{
private HttpServletResponse response;
//缓存响应内容的输出流
private ByteArrayOutputStream result = new ByteArrayOutputStream();
public ResponseWrapper(HttpServletResponse response){
super(response);
this.response = response;
/**
public String body(){
return result.toString();
@Override
public ServletOutputStream getOutputStream() throws IOException{
return new ServletOutputStreamWrapper(this.response,this.result);
@Override
public PrintWriter getWriter() throws IOException{
return new PrintWriter(new OutputStreamWriter(this.result,this.response.getCharacterEncoding()));
//自定义输出流的包装类 内部类
static final class ServletOutputStreamWrapper extends ServletOutputStream{
private HttpServletResponse response;
private ByteArrayOutputStream byteArrayOutputStream;
public ServletOutputStreamWrapper(HttpServletResponse response, ByteArrayOutputStream byteArrayOutputStream){
this.response = response;
this.byteArrayOutputStream = byteArrayOutputStream;
@Override
public boolean isReady(){
return true;
@Override
public void setWriteListener(WriteListener listener){
@Override
public void write(int b) throws IOException{
this.byteArrayOutputStream.write(b);
/**
@Override
public void flush() throws IOException{
if(!response.isCommitted()){
byte[] bytes = this.byteArrayOutputStream.toByteArray();
复制代码
第三步:扩展TraceableRequest,该接口中的方法会在创建HttpTrace#Request内部类时调用,自定义实现里面的方法,再在过滤器中引用该类就可以达到自定义显示内容的目的,该类中的Request是我们第一步创建的装饰类,不能使用HttpServletRequest
class CustomerTraceableRequest implements TraceableRequest{
//自定义的Request装饰类,不能使用HttpServletRequest
this.request = request;
//HttpTrace类中getMethod会调用
@Override
String getMethod() {
return request.getMethod();
/**
@Override
return URI.create(request.getRequestURL().toString());
//因为在HttpTrace中可扩展的只有headers的Map,所以我们自定义属性RequestParam存入headers中,作为入参信息展示
@Override
Map> getHeaders() {
Map> headerParam = new HashMap<>(1);
"RequestParam",getParams());
return headerParam;
//该方法也要重写,默认的太简单无法获取真是的IP
@Override
String getRemoteAddress() {
return IpUtils.getIpAddress(request);
//根据GET或者POST的请求方式不同,获取不同情况下的请求参数
List getParams() {
String params = null;
String method = this.getMethod();
if(HttpMethod.GET.matches(method)){
else if(HttpMethod.POST.matches(method)){
this.request.body();
List result = new ArrayList<>(1);
return result;
复制代码
第四步:扩展TraceableResponse,该接口中方法在创建HttpTrace#Response内部类时引用,自定义实现里面的方法:
class CustomerTraceableResponse implements TraceableResponse{
//自定义的HttpServletResponse包装类
this.response = response;
this.request = request;
//返回响应状态
@Override
int getStatus() {
return response.getStatus();
//扩展Response headers添加Response Body属性,展示响应内容,但是需要排除`/actuator/`开头的请求,这里面部分响应内容太大,容易OOM
@Override
Map> getHeaders() {
if(isActuatorUri()){
return extractHeaders();
else{
Map> result = new LinkedHashMap<>(1);
List responseBody = new ArrayList<>(1);
this.response.body());
"ResponseBody", responseBody);
"Content-Type", getContentType());
return result;
//是否是需要过滤的请求uri
String requestUri = request.getRequestURI();
new AntPathMatcher();
return matcher.match("/actuator/**", requestUri);
//server端页面展示的Content-Type以及Length是从Response中获取的
List getContentType() {
List list = new ArrayList<>(1);
this.response.getContentType());
return list;
//针对/actuator/**的请求返回默认的headers内容获
Map> extractHeaders() {
Map> headers = new LinkedHashMap<>();
for (String name : this.response.getHeaderNames()) {
new ArrayList<>(this.response.getHeaders(name)));
return headers;
复制代码
第五步:自定义Filter对Resquest和Response过滤,并创建HttpTrace对象:
public class CustomerHttpTraceFilter extends OncePerRequestFilter implements Ordered{
//存储HttpTrace的repository,默认是居于内存的,可扩展该类跟换存储数据的方式
private HttpTraceRepository httpTraceRepository;
//该类创建HttpTrace对象,Set包含的内容是我们需要展示那些内容的容器(request-headers,response-headers,remote-address,time-taken)
private HttpExchangeTracer httpExchangeTracer;
public CustomerHttpTraceFilter(HttpTraceRepository httpTraceRepository, HttpExchangeTracer httpExchangeTracer){
this.httpTraceRepository = httpTraceRepository;
this.httpExchangeTracer = httpExchangeTracer;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException{
//校验URI是否有效
if (!isRequestValid(request)) {
return;
//将HttpServletRequest包装成我们自己的
new RequestWrapper(request);
//将HttpServletResponse包装成我们的自己的
new ResponseWrapper(response);
//创建我们的自己的TraceRequest对象
new CustomerTraceableRequest(wrapper);
//创建HttpTrace对象(FilteredTraceableRequest 是内部类,通过Set筛选那些信息需要展示就保存那些信息),重点设置HttpTrace#Request对象的各种参数
try {
finally {
//自定义的TraceableResponse 保存需要的response信息
new CustomerTraceableResponse(responseWrapper,request);
//根据Set设置HttpTrace中session、principal、timeTaken信息以及Response内部类信息
this.httpExchangeTracer.sendingResponse(httpTrace, traceableResponse, null, null);
//将HttpTrace对象保存在Respository中存储起来
this.httpTraceRepository.add(httpTrace);
private boolean isRequestValid(HttpServletRequest request){
try {
new URI(request.getRequestURL().toString());
return true;
catch (URISyntaxException ex) {
return false;
@Override
public int getOrder(){
return Ordered.LOWEST_PRECEDENCE - 10;
复制代码
第六步:通过@SpringBootApplication(exclude)禁用HttpTraceAutoConfiguration自动配置,自定义自动配置更换Filter过滤器:
@Configuration
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = "management.trace.http", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(HttpTraceProperties.class)
public class TraceFilterConfig{
//存储HttpTrace信息的对象
@Bean
@ConditionalOnMissingBean(HttpTraceRepository.class)
public InMemoryHttpTraceRepository traceRepository(){
return new InMemoryHttpTraceRepository();
//创建HttpTrace对象Exchange
@Bean
@ConditionalOnMissingBean
public HttpExchangeTracer httpExchangeTracer(HttpTraceProperties traceProperties){
return new HttpExchangeTracer(traceProperties.getInclude());
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class ServletTraceFilterConfiguration{
//将我们自定义的Filter已Bean的方式注册,才能生效
@Bean
@ConditionalOnMissingBean
public CustomerHttpTraceFilter httpTraceFilter(HttpTraceRepository repository,{
return new CustomerHttpTraceFilter(repository,tracer);
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
static class ReactiveTraceFilterConfiguration{
@Bean
@ConditionalOnMissingBean
public HttpTraceWebFilter httpTraceWebFilter(HttpTraceRepository repository,{
return new HttpTraceWebFilter(repository, tracer,
复制代码
集成Redisson健康状态监控
如果有引入spring-boot-starter-redis,SBA默认同过RedisConnectionFactory监控Redis的健康状态,无奈Redisson还没有,自己东收丰衣足食。通过HealthIndicator和ReactiveHealthIndicator使用策略模式实现不同组件的健康监控,后者是使用Rective模式下的。我是通过JavaBean的方式配置Redisson,所以顺便实现ReactiveHealthIndicator再添加该指标即可:
@Configuration
@EnableConfigurationProperties(value = RedissonProperties.class)
public class RedissonConfig implements ReactiveHealthIndicator{
//自己的RedissonProperties文件
@Autowired
private RedissonProperties redissonProperties;
//暴露 redissonClient句柄
@Bean
@ConditionalOnMissingBean
public RedissonClient redisClient(){
return Redisson.create(config());
//通过Bean的方式配置RedissonConfig相关信息
@Bean
public Config config(){
new Config();
//单实列模式
":" + redissonProperties.getPort())
new DefaultCodecProvider())
new NioEventLoopGroup())
2)
2);
return config;
//实现ReactiveHealthIndicator 重写health方法
@Override
public Mono health(){
return checkRedissonHealth().onErrorResume(ex -> Mono.just(new Health.Builder().down(ex).build()));
//我是通过ping 的方式判断redis服务器是否up的状态,并增加加Netty和Threads的监控
private Mono checkRedissonHealth(){
new Health.Builder();
"address", redissonProperties.getAddress());
//检测健康状态
if (this.redisClient().getNodesGroup().pingAll()) {
"dataBase", redissonProperties.getDatabase());
"redisNodeThreads", this.redisClient().getConfig().getThreads());
"nettyThreads", this.redisClient().getConfig().getNettyThreads());
else{
return Mono.just(builder.build());
复制代码
结果:
Ok!圆满完成!