Spring Cloud 升级之路 - 2020.0.x - 3. Undertow 的 accesslog 配置 (3)

注意:默认 undertow 没有开启请求时间内统计,需要打开才能统计响应时间,如何开启呢?通过注册一个 WebServerFactoryCustomizer 到 Spring ApplicationContext 中即可。请看下面的代码(项目地址:https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/):

spring.factories(省略无关代码)

# AutoConfiguration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.github.hashjang.spring.cloud.iiford.service.common.auto.UndertowAutoConfiguration

UndertowAutoConfiguration

//设置proxyBeanMethods=false,因为没有 @Bean 的方法互相调用需要每次返回同一个 Bean,没必要代理,关闭增加启动速度 @Configuration(proxyBeanMethods = false) @Import(WebServerConfiguration.class) public class UndertowAutoConfiguration { }

WebServerConfiguration

//设置proxyBeanMethods=false,因为没有 @Bean 的方法互相调用需要每次返回同一个 Bean,没必要代理,关闭增加启动速度 @Configuration(proxyBeanMethods = false) public class WebServerConfiguration { @Bean public WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> undertowWebServerAccessLogTimingEnabler(ServerProperties serverProperties) { return new DefaultWebServerFactoryCustomizer(serverProperties); } }

DefaultWebServerFactoryCustomizer

public class DefaultWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> { private final ServerProperties serverProperties; public DefaultWebServerFactoryCustomizer(ServerProperties serverProperties) { this.serverProperties = serverProperties; } @Override public void customize(ConfigurableUndertowWebServerFactory factory) { String pattern = serverProperties.getUndertow().getAccesslog().getPattern(); // 如果 accesslog 配置中打印了响应时间,则打开记录请求开始时间配置 if (logRequestProcessingTiming(pattern)) { factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true)); } } private boolean logRequestProcessingTiming(String pattern) { if (StringUtils.isBlank(pattern)) { return false; } //判断 accesslog 是否配置了查看响应时间 return pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_MICROS) || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_MILLIS) || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_NANOS) || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_MILLIS_SHORT) || pattern.contains(ResponseTimeAttribute.RESPONSE_TIME_SECONDS_SHORT); } } 其他

还有安全相关的属性(SSL 相关,登录认证 Authentication 相关),微服务内部调用一般用不到,我们这里就不赘述了。
其它内置的属性,在 Spring Boot 环境下一般用不到,我们这里就不讨论了。

举例

我们最开始配置的 accesslog 的例子请求返回如下( JSON 格式化之后的结果):

{ "transportProtocol": "http/1.1", "scheme": "http", "protocol": "HTTP/1.1", "method": "GET", "reqHeaderUserAgent": "PostmanRuntime/7.26.10", "cookieUserId": "testRequestCookieUserId", "queryTest": "1", "queryString": "?test=1&query=2", "relativePath": "/test, /test, -", "requestLine": "GET /test?test=1&query=2 HTTP/1.1", "uri": "/test", "thread": "XNIO-2 task-1", "hostPort": "127.0.0.1:8102", "localIp": "127.0.0.1", "localPort": "8102", "localServerName": "127.0.0.1", "remoteIp": "127.0.0.1", "remoteHost": "127.0.0.1", "bytesSent": "26", "time": "2021-04-08 00:07:50.410", "status": "200", "reason": "OK", "respHeaderUserSession": "testResponseHeaderUserSession", "respCookieUserId": "testResponseCookieUserId", "timeUsed": "3683ms, 3.683s, 3683ms, 3683149 us, 3683149200 ns", }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfwwz.html