SpringCloud升级之路2020.0.x版-14.UnderTow AccessLog 配置介绍

本文主要是介绍SpringCloud升级之路2020.0.x版-14.UnderTow AccessLog 配置介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本系列代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford

image

server:undertow:# access log相关配置accesslog:# 存放目录,默认为 logsdir: ./log# 是否开启enabled: true# 格式,各种占位符后面会详细说明pattern: '{"transportProtocol":"%{TRANSPORT_PROTOCOL}","scheme":"%{SCHEME}","protocol":"%{PROTOCOL}","method":"%{METHOD}","reqHeaderUserAgent":"%{i,User-Agent}","cookieUserId": "%{c,userId}","queryTest": "%{q,test}","queryString": "%q","relativePath": "%R, %{REQUEST_PATH}, %{RESOLVED_PATH}","requestLine": "%r","uri": "%U","thread": "%I","hostPort": "%{HOST_AND_PORT}","localIp": "%A","localPort": "%p","localServerName": "%v","remoteIp": "%a","remoteHost": "%h","bytesSent": "%b","time":"%{time,yyyy-MM-dd HH:mm:ss.S}","status":"%s","reason":"%{RESPONSE_REASON_PHRASE}","respHeaderUserSession":"%{o,userSession}","respCookieUserId":"%{resp-cookie,userId}","timeUsed":"%Dms, %Ts, %{RESPONSE_TIME}ms, %{RESPONSE_TIME_MICROS} us, %{RESPONSE_TIME_NANOS} ns",}'# 文件前缀,默认为 access_logprefix: access.# 文件后缀,默认为 logsuffix: log# 是否另起日志文件写 access log,默认为 true# 目前只能按照日期进行 rotate,一天一个日志文件rotate: true

image

Undertow 的 accesslog 处理核心类抽象是 io.undertow.server.handlers.accesslog.AccesslogReceiver。由于目前 Undertow 的 AccesslogReceiver 只有一种实现在使用,也就是 io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver

查看 DefaultAccessLogReceiver 的 rotate 时机:

DefaultAccessLogReceiver

/*** 计算 rotate 时间点*/
private void calculateChangeOverPoint() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.HOUR_OF_DAY, 0);//当前时间日期 + 1,即下一天calendar.add(Calendar.DATE, 1);SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);currentDateString = df.format(new Date());// if there is an existing default log file, use the date last modified instead of the current dateif (Files.exists(defaultLogFile)) {try {currentDateString = df.format(new Date(Files.getLastModifiedTime(defaultLogFile).toMillis()));} catch(IOException e){// ignore. use the current date if exception happens.}}//rotate 时机是下一天的 0 点changeOverPoint = calendar.getTimeInMillis();
}

image

其实 Undertow 中的 accesslog 占位符,就是之前我们提到的 Undertow Listener 解析请求后抽象的 HTTP server exchange 的属性。

官网文档的表格并不是最全的,并且注意点并没有说明,例如某些占位符必须打开某些 Undertow 特性才能使用等等。这里我们列出下。

首先先提出一个注意点,参数占位符,例如 %{i,你要看的header值} 查看 header 的某个 key 的值。逗号后面注意不要有空格,因为这个空格会算入 key 里面导致拿不到你想要的 key

请求相关属性

描述缩写占位符全名占位符参数占位符源码
请求传输协议,等价于请求协议%{TRANSPORT_PROTOCOL}TransportProtocolAttribute
请求模式,例如 http、https 等%{SCHEME}RequestSchemeAttribute
请求协议,例如 HTTP/1.1%H%{PROTOCOL}RequestProtocolAttribute
请求方法,例如 GET、POST 等%m%{METHOD}RequestMethodAttribute
请求 Header 的某一个值%{i,你要看的header值}RequestHeaderAttribute
Cookie 的某一个值%{c,你要看的cookie值} 或者 %{req-cookie,你要看的cookie值}分别对应 CookieAttributeRequestCookieAttribute
路径参数 PathVariable 由于并没有被 Undertow 的 Listener 或者 Handler 解析处理,所以拦截不到,无法确认是否是一个 PathVariable 还是就是 url 路径。所以,PathVariable 的占位符是不会起作用的%{p, 你想查看的路径参数 key }PathParameterAttribute
请求参数,即 url 的 ? 之后键值对,这里可以选择查看某个 key 的值。%{q, 你想查看的请求参数 key}QueryParameterAttribute
请求参数字符串,即 url 的 ? 之后的所有字符}%q(不包含 ?)%{QUERY_STRING}(不包含 ?);%{BARE_QUERY_STRING}(包含 ?)QueryStringAttribute
请求相对路径(在 Spring Boot 环境下,大多数情况 RequestPath 和 RelativePath 还有 ResolvedPath 是等价的),即除去 host,port,请求参数字符串的路径%R%{RELATIVE_PATH} 或者 %{REQUEST_PATH} 或者 %{RESOLVED_PATH}分别对应 RelativePathAttributeRequestPathAttributeResolvedPathAttribute
请求整体字符串,包括请求方法,请求相对路径,请求参数字符串,请求协议,例如 Get /test?a=b HTTP/1.1%r%{REQUEST_LINE}RequestLineAttribute
请求 URI,包括请求相对路径,请求参数字符串%U%{REQUEST_URL}RequestURLAttribute
处理请求的线程%I%{THREAD_NAME}ThreadNameAttribute

注意:

  1. 路径参数 PathVariable 由于并没有被 Undertow 的 Listener 或者 Handler 解析处理,所以拦截不到,无法确认是否是一个 PathVariable 还是就是 url 路径。所以,PathVariable 的占位符是不会起作用的

请求地址相关

描述缩写占位符全名占位符参数占位符源码
host 和 port,一般就是 HTTP 请求 Header 中的 Host 值,如果 Host 为空则获取本地地址和端口,如果没获取到端口则根据协议用默认端口(http:80,,https:443)%{HOST_AND_PORT}HostAndPortAttribute
请求本地地址 IP%A%{LOCAL_IP}LocalIPAttribute
请求本地端口 Port%p%{LOCAL_PORT}LocalPortAttribute
请求本地主机名,一般就是 HTTP 请求 Header 中的 Host 值,如果 Host 为空则获取本地地址%v%{LOCAL_SERVER_NAME}LocalServerNameAttribute
请求远程主机名,通过连接获取远端的主机地址%h%{REMOTE_HOST}RemoteHostAttribute
请求远程 IP,通过连接获取远端的 IP%a%{REMOTE_IP}RemoteIPAttribute

注意:

  1. 请求的远程地址我们一般不从请求连接获取,而是通过 Http Header 里面的 X-forwarded-for 或者 X-real-ip 等获取,因为现在请求都是通过各种 VPN,负载均衡器发上来的。

响应相关属性

描述缩写占位符全名占位符参数占位符源码
发送的字节数大小,除了 Http Header 以外%b (如果为空就是 -) 或者 %B (如果为空就是 0)%{BYTES_SENT} (如果为空就是 0)BytesSentAttribute
accesslog 时间,这个不是收到请求的时间,而是响应的时间%t%{DATE_TIME}%{time, 你自定义的 java 中 SimpleDateFormat 的格式}DateTimeAttribute
HTTP 响应状态码%s%{RESPONSE_CODE}ResponseCodeAttribute
HTTP 响应原因%{RESPONSE_REASON_PHRASE}ResponseReasonPhraseAttribute
响应 Header 的某一个值%{o,你要看的header值}ResponseHeaderAttribute
响应 Cookie 的某一个值%{resp-cookie,你要看的cookie值}ResponseCookieAttribute
响应时间,默认 undertow 没有开启请求时间内统计,需要打开才能统计响应时间%D(毫秒,例如 56 代表 56ms) %T(秒,例如 5.067 代表 5.067 秒)%{RESPONSE_TIME}(等价于 %D) %{RESPONSE_TIME_MICROS} (微秒) %{RESPONSE_TIME_NANOS}(纳秒)ResponseTimeAttribute

注意:默认 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 {@Beanpublic 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;}@Overridepublic 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",
}

我们这一节详细介绍了如何配置 Undertow 的 accesslog,将 accesslog 各种占位符都罗列了出来,用户可以根据这些信息配置出自己想要的 accesslog 信息以及格式。下一节,我们将详细介绍我们框架中针对 Undertow 的定制代码

微信搜索“我的编程喵”关注公众号,每日一刷,轻松提升技术,斩获各种offer

这篇关于SpringCloud升级之路2020.0.x版-14.UnderTow AccessLog 配置介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/170573

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Zookeeper安装和配置说明

一、Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式。 ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境; ■ 伪集群模式:就是在一台物理机上运行多个Zookeeper 实例; ■ 集群模式:Zookeeper运行于一个集群上,适合生产环境,这个计算机集群被称为一个“集合体”(ensemble) Zookeeper通过复制来实现

CentOS7安装配置mysql5.7 tar免安装版

一、CentOS7.4系统自带mariadb # 查看系统自带的Mariadb[root@localhost~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x86_64# 卸载系统自带的Mariadb[root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7

性能测试介绍

性能测试是一种测试方法,旨在评估系统、应用程序或组件在现实场景中的性能表现和可靠性。它通常用于衡量系统在不同负载条件下的响应时间、吞吐量、资源利用率、稳定性和可扩展性等关键指标。 为什么要进行性能测试 通过性能测试,可以确定系统是否能够满足预期的性能要求,找出性能瓶颈和潜在的问题,并进行优化和调整。 发现性能瓶颈:性能测试可以帮助发现系统的性能瓶颈,即系统在高负载或高并发情况下可能出现的问题