欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud2020.0.x版UnderTow AccessLog相關(guān)配置簡介

 更新時間:2021年08月18日 17:14:42   作者:干貨滿滿張哈希  
本文詳細介紹了SpringCloud中AccessLog的相關(guān)配置,我們可以根據(jù)文中的相關(guān)數(shù)據(jù)配置出所需的AccessLog的信息以及格式,感興趣的小伙伴可以參考一下

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

01.accesslog相關(guān)配置

server:
  undertow:
    # access log相關(guān)配置
    accesslog:
      # 存放目錄,默認為 logs
      dir: ./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_log
      prefix: access.
      # 文件后綴,默認為 log
      suffix: log
      # 是否另起日志文件寫 access log,默認為 true
      # 目前只能按照日期進行 rotate,一天一個日志文件
      rotate: true

02.日志文件rotate目前只能按照日期

Undertow 的 accesslog 處理核心類抽象是 io.undertow.server.handlers.accesslog.AccesslogReceiver。由于目前 Undertow 的 AccesslogReceiver 只有一種實現(xiàn)在使用,也就是 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 date
    if (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();
}

03.access占位符

其實 Undertow 中的 accesslog 占位符,就是之前我們提到的 Undertow Listener 解析請求后抽象的 HTTP server exchange 的屬性。

官網(wǎng)文檔的表格并不是最全的,并且注意點并沒有說明,例如某些占位符必須打開某些 Undertow 特性才能使用等等。這里我們列出下。

首先先提出一個注意點,參數(shù)占位符,例如 %{i,你要看的header值} 查看 header 的某個 key 的值。逗號后面注意不要有空格,因為這個空格會算入 key 里面導(dǎo)致拿不到你想要的 key。

請求相關(guān)屬性

描述 縮寫占位符 全名占位符 參數(shù)占位符 源碼
請求傳輸協(xié)議,等價于請求協(xié)議 %{TRANSPORT_PROTOCOL} TransportProtocolAttribute
請求模式,例如 http、https 等 %{SCHEME} RequestSchemeAttribute
請求協(xié)議,例如 HTTP/1.1 等 %H %{PROTOCOL} RequestProtocolAttribute
請求方法,例如 GET、POST 等 %m %{METHOD} RequestMethodAttribute
請求 Header 的某一個值 %{i,你要看的header值} RequestHeaderAttribute
Cookie 的某一個值 %{c,你要看的cookie值} 或者 %{req-cookie,你要看的cookie值} 分別對應(yīng) CookieAttribute 和RequestCookieAttribute
路徑參數(shù) PathVariable 由于并沒有被 Undertow 的 Listener 或者 Handler 解析處理,所以攔截不到,無法確認是否是一個 PathVariable 還是就是 url 路徑。所以,PathVariable 的占位符是不會起作用的。 %{p, 你想查看的路徑參數(shù) key } PathParameterAttribute
請求參數(shù),即 url 的 ? 之后鍵值對,這里可以選擇查看某個 key 的值。 %{q, 你想查看的請求參數(shù) key} QueryParameterAttribute
請求參數(shù)字符串,即 url 的 ? 之后的所有字符} %q(不包含 ?) %{QUERY_STRING}(不包含 ?);%{BARE_QUERY_STRING}(包含 ?) QueryStringAttribute
請求相對路徑(在 Spring Boot 環(huán)境下,大多數(shù)情況 RequestPath 和 RelativePath 還有 ResolvedPath 是等價的),即除去 host,port,請求參數(shù)字符串的路徑 %R %{RELATIVE_PATH} 或者 %{REQUEST_PATH} 或者 %{RESOLVED_PATH} 分別對應(yīng)RelativePathAttribute 和 RequestPathAttribute 和 ResolvedPathAttribute
請求整體字符串,包括請求方法,請求相對路徑,請求參數(shù)字符串,請求協(xié)議,例如 Get /test?a=b HTTP/1.1 %r %{REQUEST_LINE} RequestLineAttribute
請求 URI,包括請求相對路徑,請求參數(shù)字符串 %U %{REQUEST_URL} RequestURLAttribute
處理請求的線程 %I %{THREAD_NAME} ThreadNameAttribute
注意
  1. 路徑參數(shù) PathVariable 由于并沒有被 Undertow 的 Listener 或者 Handler 解析處理,所以攔截不到,無法確認是否是一個 PathVariable 還是就是 url 路徑。所以,PathVariable 的占位符是不會起作用的。

請求地址相關(guān)

描述 縮寫占位符 全名占位符 參數(shù)占位符 源碼
host 和 port,一般就是 HTTP 請求 Header 中的 Host 值,如果 Host 為空則獲取本地地址和端口,如果沒獲取到端口則根據(jù)協(xié)議用默認端口(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 等獲取,因為現(xiàn)在請求都是通過各種 VPN,負載均衡器發(fā)上來的。

響應(yīng)相關(guān)屬性

描述 縮寫占位符 全名占位符 參數(shù)占位符 源碼
發(fā)送的字節(jié)數(shù)大小,除了 Http Header 以外 %b (如果為空就是 -) 或者 %B (如果為空就是 0) %{BYTES_SENT} (如果為空就是 0) BytesSentAttribute
accesslog 時間,這個不是收到請求的時間,而是響應(yīng)的時間 %t %{DATE_TIME} %{time, 你自定義的 java 中 SimpleDateFormat 的格式} DateTimeAttribute
HTTP 響應(yīng)狀態(tài)碼 %s %{RESPONSE_CODE} ResponseCodeAttribute
HTTP 響應(yīng)原因 %{RESPONSE_REASON_PHRASE} ResponseReasonPhraseAttribute
響應(yīng) Header 的某一個值 %{o,你要看的header值} ResponseHeaderAttribute
響應(yīng) Cookie 的某一個值 %{resp-cookie,你要看的cookie值} ResponseCookieAttribute
響應(yīng)時間,默認 undertow 沒有開啟請求時間內(nèi)統(tǒng)計,需要打開才能統(tǒng)計響應(yīng)時間 %D(毫秒,例如 56 代表 56ms) %T(秒,例如 5.067 代表 5.067 秒) %{RESPONSE_TIME}(等價于 %D) %{RESPONSE_TIME_MICROS} (微秒) %{RESPONSE_TIME_NANOS}(納秒) ResponseTimeAttribute

注意:默認 undertow 沒有開啟請求時間內(nèi)統(tǒng)計,需要打開才能統(tǒng)計響應(yīng)時間,如何開啟呢?通過注冊一個 WebServerFactoryCustomizer 到 Spring ApplicationContext 中即可。請看下面的代碼(項目地址:https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/):

spring.factories(省略無關(guān)代碼)

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

UndertowAutoConfiguration

//設(shè)置proxyBeanMethods=false,因為沒有 @Bean 的方法互相調(diào)用需要每次返回同一個 Bean,沒必要代理,關(guān)閉增加啟動速度
@Configuration(proxyBeanMethods = false)
@Import(WebServerConfiguration.class)
public class UndertowAutoConfiguration {
}

WebServerConfiguration

//設(shè)置proxyBeanMethods=false,因為沒有 @Bean 的方法互相調(diào)用需要每次返回同一個 Bean,沒必要代理,關(guān)閉增加啟動速度
@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 配置中打印了響應(yīng)時間,則打開記錄請求開始時間配置
        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 是否配置了查看響應(yīng)時間
        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);
    }
}

其他

還有安全相關(guān)的屬性(SSL 相關(guān),登錄認證 Authentication 相關(guān)),微服務(wù)內(nèi)部調(diào)用一般用不到,我們這里就不贅述了。
其它內(nèi)置的屬性,在 Spring Boot 環(huán)境下一般用不到,我們這里就不討論了。

舉例

我們最開始配置的 accesslog 的例子請求返回如下( JSON 格式化之后的結(jié)果):

{
	"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",
}

總結(jié)

我們這一節(jié)詳細介紹了如何配置 Undertow 的 accesslog,將 accesslog 各種占位符都羅列了出來,用戶可以根據(jù)這些信息配置出自己想要的 accesslog 信息以及格式。

到此這篇關(guān)于SpringCloud2020.0.x版--14.UnderTow AccessLog相關(guān)配置簡介的文章就介紹到這了,更多相關(guān)SpringCloud UnderTow AccessLog配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用JAVA+Maven+TestNG框架實現(xiàn)超詳細Appium測試安卓真機教程

    使用JAVA+Maven+TestNG框架實現(xiàn)超詳細Appium測試安卓真機教程

    這篇文章主要介紹了使用JAVA+Maven+TestNG框架實現(xiàn)超詳細Appium測試安卓真機教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • spring-cloud入門之eureka-client(服務(wù)注冊)

    spring-cloud入門之eureka-client(服務(wù)注冊)

    本篇文章主要介紹了spring-cloud入門之eureka-client(服務(wù)注冊),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 開放封閉原則_動力節(jié)點Java學(xué)院整理

    開放封閉原則_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了開放封閉原則,開放-封閉原則是面向?qū)ο笤O(shè)計的核心所在,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • java阻塞隊列BlockingQueue詳細解讀

    java阻塞隊列BlockingQueue詳細解讀

    這篇文章主要介紹了java阻塞隊列BlockingQueue詳細解讀,在新增的Concurrent包中,BlockingQueue很好的解決了多線程中,如何高效安全“傳輸”數(shù)據(jù)的問題,通過這些高效并且線程安全的隊列類,為我們快速搭建高質(zhì)量的多線程程序帶來極大的便利,需要的朋友可以參考下
    2023-10-10
  • Java同步代碼塊解決銀行取錢的安全問題實例分析

    Java同步代碼塊解決銀行取錢的安全問題實例分析

    這篇文章主要介紹了Java同步代碼塊解決銀行取錢的安全問題,結(jié)合實例形式分析了java基于線程的同步問題實現(xiàn)與使用相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • java中GZIP壓縮解壓類使用實例

    java中GZIP壓縮解壓類使用實例

    這篇文章主要介紹了java中GZIP壓縮解壓類使用實例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Spring Boot實現(xiàn)發(fā)送郵件

    Spring Boot實現(xiàn)發(fā)送郵件

    這篇文章主要為大家詳細介紹了Spring Boot實現(xiàn)發(fā)送郵件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Java 反射機制實例詳解

    Java 反射機制實例詳解

    這篇文章主要介紹了Java 反射機制實例詳解的相關(guān)資料,這里對java中反射機制進行了詳細的分析,需要的朋友可以參考下
    2017-09-09
  • 使用@Autowired注解引入server服務(wù)層方法時報錯的解決

    使用@Autowired注解引入server服務(wù)層方法時報錯的解決

    這篇文章主要介紹了使用@Autowired注解引入server服務(wù)層方法時報錯的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 淺析JAVA 循環(huán)結(jié)構(gòu)

    淺析JAVA 循環(huán)結(jié)構(gòu)

    這篇文章主要介紹了JAVA 循環(huán)結(jié)構(gòu)的相關(guān)資料,文中講解的非常細致,示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論