转载

Spring-Boot-admin之HttpTrace显示入参和出参及监控Redisson

spring-boot-admin (以下简称 SBA )与Spring Boot、Spring cloud项目以starter得方式自动集成,包括Server端和Client端

SBA 监控包括应用的基本信息、logfile(在线实时浏览或者download)、JVM信息(线程信息、堆信息、非堆信息)、Web(API接口信息、最近100次API调用的信息)、应用中用户登录信息;监控指标很全面,但针对具体项目就要增加符合自己项目的内容了,比如如下两点:

自定义HttpTrace增加入参和出参

结果:

Spring-Boot-admin之HttpTrace显示入参和出参及监控Redisson

spring-boot-admin 中HttpTrace显示的信息包括 sessionprincipalrequestresponsetimeTakentimestamp ,但 sessionprincipal 对该项目完全无用, requestHttpTrace 的内部类显示信息包括:

private final String method;
private final URI uri;
//唯一可以扩展的地方
private final Map<String, List<String>> headers;
private final String remoteAddress;

response 也是 HttpTrace 的内部类:

private final int status;
//唯一可以扩展的地方
private final Map<String, List<String>> 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中先读取数据
            body = IOUtils.toByteArray(request.getInputStream());
        } catch (IOException e) {
            logger.error("从请求中获取请求参数出现异常:", e);
        }
//将读取出来的内存再写入流中
        wrapper = 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;
    }

    /**
     * 响应的内容 供外部调用
     *针对 体积较大的响应内容 很容易发生 OOM(比如:/actuator/logfile 接口),可在调用该方法的地方就行api过滤
     *解决方法在第四步
     */
    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();
                ServletOutputStream outputStream = response.getOutputStream();
                outputStream.write(bytes);
                outputStream.flush();
            }
        }
    }
}
  • 第三步:扩展 TraceableRequest ,该接口中的方法会在创建 HttpTrace#Request 内部类时调用,自定义实现里面的方法,再在过滤器中引用该类就可以达到自定义显示内容的目的, 该类中的Request是我们第一步创建的装饰类,不能使用HttpServletRequest
public class CustomerTraceableRequest implements TraceableRequest {
//自定义的Request装饰类,不能使用HttpServletRequest
    private RequestWrapper request;

    public CustomerTraceableRequest(RequestWrapper request) {
        this.request = request;
    }
//HttpTrace类中getMethod会调用
    @Override
    public String getMethod() {
        return request.getMethod();
    }

    /**
     * @return POST 或者 GET 方式 都返回 {ip}:{port}/uir的形式返回
     */
    @Override
    public URI getUri() {
        return URI.create(request.getRequestURL().toString());
    }

//因为在HttpTrace中可扩展的只有headers的Map,所以我们自定义属性RequestParam存入headers中,作为入参信息展示
    @Override
    public Map<String, List<String>> getHeaders() {
        Map<String, List<String>> headerParam = new HashMap<>(1);
        headerParam.put("RequestParam",getParams());
        return headerParam;
    }

//该方法也要重写,默认的太简单无法获取真是的IP
    @Override
    public String getRemoteAddress() {
        return IpUtils.getIpAddress(request);
    }
//根据GET或者POST的请求方式不同,获取不同情况下的请求参数
    public List<String> getParams() {
        String params = null;
        String method = this.getMethod();
        if(HttpMethod.GET.matches(method)){
            params = request.getQueryString();
        }else if(HttpMethod.POST.matches(method)){
            params = this.request.body();
        }
        List<String> result = new ArrayList<>(1);
        result.add(params);
        return result;
    }
}
  • 第四步:扩展 TraceableResponse ,该接口中方法在创建 HttpTrace#Response 内部类时引用,自定义实现里面的方法:
public class CustomerTraceableResponse implements TraceableResponse {
    //自定义的HttpServletResponse包装类
    private ResponseWrapper response;
    private HttpServletRequest request;

    public CustomerTraceableResponse(ResponseWrapper response, HttpServletRequest request) {
        this.response = response;
        this.request = request;
    }
//返回响应状态
    @Override
    public int getStatus() {
        return response.getStatus();
    }
//扩展Response headers添加Response Body属性,展示响应内容,但是需要排除`/actuator/`开头的请求,这里面部分响应内容太大,容易OOM
    @Override
    public Map<String, List<String>> getHeaders() {
        if(isActuatorUri()){
            return extractHeaders();
        }else{
            Map<String, List<String>> result = new LinkedHashMap<>(1);
            List<String> responseBody = new ArrayList<>(1);
            responseBody.add(this.response.body());
            result.put("ResponseBody", responseBody);
            result.put("Content-Type", getContentType());
            return result;
        }
    }
//是否是需要过滤的请求uri
    private boolean isActuatorUri() {
        String requestUri = request.getRequestURI();
        AntPathMatcher matcher = new AntPathMatcher();
        return matcher.match("/actuator/**", requestUri);
    }
//server端页面展示的Content-Type以及Length是从Response中获取的
    private List<String> getContentType() {
        List<String> list = new ArrayList<>(1);
        list.add(this.response.getContentType());
        return list;
    }
//针对/actuator/**的请求返回默认的headers内容获
    private Map<String, List<String>> extractHeaders() {
        Map<String, List<String>> headers = new LinkedHashMap<>();
        for (String name : this.response.getHeaderNames()) {
            headers.put(name, 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<Include>包含的内容是我们需要展示那些内容的容器(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)) {
            filterChain.doFilter(request, response);
            return;
        }
//将HttpServletRequest包装成我们自己的
        RequestWrapper wrapper = new RequestWrapper(request);
//将HttpServletResponse包装成我们的自己的
        ResponseWrapper responseWrapper = new ResponseWrapper(response);

//创建我们的自己的TraceRequest对象
        CustomerTraceableRequest traceableRequest = new CustomerTraceableRequest(wrapper);
//创建HttpTrace对象(FilteredTraceableRequest 是内部类,通过Set<Include>筛选那些信息需要展示就保存那些信息),重点设置HttpTrace#Request对象的各种参数
        HttpTrace httpTrace = httpExchangeTracer.receivedRequest(traceableRequest);
        try {
            filterChain.doFilter(wrapper, responseWrapper);
        } finally {
//自定义的TraceableResponse 保存需要的response信息
            CustomerTraceableResponse traceableResponse = new CustomerTraceableResponse(responseWrapper,request);
//根据Set<Include>设置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,
                                               HttpExchangeTracer tracer) {
            return new CustomerHttpTraceFilter(repository,tracer);
        }
    }

    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
    static class ReactiveTraceFilterConfiguration {

        @Bean
        @ConditionalOnMissingBean
        public HttpTraceWebFilter httpTraceWebFilter(HttpTraceRepository repository,
                                                     HttpExchangeTracer tracer, HttpTraceProperties traceProperties) {
            return new HttpTraceWebFilter(repository, tracer,
                    traceProperties.getInclude());
        }
    }
}

集成Redisson健康状态监控

如果有引入 spring-boot-starter-redis ,SBA默认同过 RedisConnectionFactory 监控Redis的健康状态,无奈Redisson还没有,自己东收丰衣足食。通过 HealthIndicatorReactiveHealthIndicator 使用策略模式实现不同组件的健康监控,后者是使用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() {
        Config config = new Config();
        config.useSingleServer() //单实列模式
                .setAddress(redissonProperties.getAddress() + ":" + redissonProperties.getPort())
                .setPassword(redissonProperties.getPassword())
                .setDatabase(redissonProperties.getDatabase())
                .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize())
                .setIdleConnectionTimeout(redissonProperties.getIdleConnectionTimeout())
                .setSubscriptionConnectionPoolSize(redissonProperties.getSubscriptionConnectionPoolSize())
                .setSubscriptionConnectionMinimumIdleSize(redissonProperties.getSubscriptionConnectionMinimumIdleSize())
                .setTimeout(redissonProperties.getTimeout())
                .setRetryAttempts(redissonProperties.getRetryAttempts())
                .setRetryInterval(redissonProperties.getRetryInterval())
                .setConnectTimeout(redissonProperties.getConnectTimeout())
                .setReconnectionTimeout(redissonProperties.getReconnectionTimeout());
        config
                .setCodecProvider(new DefaultCodecProvider())
                .setEventLoopGroup(new NioEventLoopGroup())
                .setThreads(Runtime.getRuntime().availableProcessors() * 2)
                .setNettyThreads(Runtime.getRuntime().availableProcessors() * 2);
        return config;
    }
//实现ReactiveHealthIndicator 重写health方法
    @Override
    public Mono<Health> health() {
        return checkRedissonHealth().onErrorResume(ex -> Mono.just(new Health.Builder().down(ex).build()));
    }
//我是通过ping 的方式判断redis服务器是否up的状态,并增加加Netty和Threads的监控
    private Mono<Health> checkRedissonHealth() {
        Health.Builder builder = new Health.Builder();
        builder.withDetail("address", redissonProperties.getAddress());
        //检测健康状态
        if (this.redisClient().getNodesGroup().pingAll()) {
            builder.status(Status.UP);
            builder.withDetail("dataBase", redissonProperties.getDatabase());
            builder.withDetail("redisNodeThreads", this.redisClient().getConfig().getThreads());
            builder.withDetail("nettyThreads", this.redisClient().getConfig().getNettyThreads());

        }else{
            builder.status(Status.DOWN);
        }
        return Mono.just(builder.build());
    }
}

在页面上看就是:

Spring-Boot-admin之HttpTrace显示入参和出参及监控Redisson

Ok!圆满完成!

如有错误,不吝赐教!

欢迎关注公众号:

Spring-Boot-admin之HttpTrace显示入参和出参及监控Redisson

原文  https://segmentfault.com/a/1190000020744693
正文到此结束
Loading...