Spring Cloud 升級之路 - 2020.0.x - 3. Undertow 的 acces

上一節咱們講述瞭如何使用 Undertow 做爲咱們的 Web 服務容器,本小節咱們來分析使用 Undertow 的另外一個問題,也就是如何配置 accesslog,以及 accesslog 的各類佔位符。java


# accesslog 相關配置git


```github

server:spring

  undertow:安全

    # access log相關配置cookie

    accesslog:app

      # 存放目錄,默認爲 logs負載均衡

      dir: ./logide

      # 是否開啓微服務

      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

```


## 注意點 1:日誌文件 rotate 目前只能按照日期


Undertow 的 accesslog 處理核心類抽象是 `io.undertow.server.handlers.accesslog.AccesslogReceiver`。因爲目前 Undertow 的 `AccesslogReceiver` 只有一種實如今使用,也就是 `io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver`。


查看 `DefaultAccessLogReceiver` 的 rotate 時機:


[`DefaultAccessLogReceiver`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/server/handlers/accesslog/DefaultAccessLogReceiver.java)

```

/**

 * 計算 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();

}

```


# accesslog 佔位符


其實 Undertow 中的 accesslog 佔位符,就是以前咱們提到的 Undertow Listener 解析請求後抽象的 HTTP server exchange 的屬性。


[官網文檔](https://undertow.io/undertow-docs/undertow-docs-2.1.0/#exchange-attributes)的表格並非最全的,而且注意點並無說明,例如某些佔位符必須打開某些 Undertow 特性才能使用等等。這裏咱們列出下。


首先先提出一個注意點,參數佔位符,例如 `%{i,你要看的header值}` 查看 header 的某個 key 的值。**逗號後面注意不要有空格,由於這個空格會算入 key 裏面致使拿不到你想要的 key**。


## 請求相關屬性

| 描述 | 縮寫佔位符 | 全名佔位符 | 參數佔位符 | 源碼 |

|------|-------|------|------|------|

| 請求傳輸協議,等價於**請求協議** | 無 | `%{TRANSPORT_PROTOCOL}` | 無 | [`TransportProtocolAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/TransportProtocolAttribute.java) |

| 請求模式,例如 http、https 等 | | `%{SCHEME}` | 無 | [`RequestSchemeAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestSchemeAttribute.java) |

| 請求協議,例如 `HTTP/1.1` 等 | `%H` | `%{PROTOCOL}` | 無 | [`RequestProtocolAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestProtocolAttribute.java) |

| 請求方法,例如 GET、POST 等 | `%m` | `%{METHOD}` | 無 | [`RequestMethodAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestMethodAttribute.java) |

| 請求 Header 的某一個值 | 無 | 無 | `%{i,你要看的header值}` | [`RequestHeaderAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestHeaderAttribute.java)|

| Cookie 的某一個值| 無 | 無 | `%{c,你要看的cookie值}` 或者 `%{req-cookie,你要看的cookie值}` | 分別對應 [`CookieAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/CookieAttribute.java) 和 [`RequestCookieAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestCookieAttribute.java) |

| 路徑參數 PathVariable 因爲並無被 Undertow 的 Listener 或者 Handler 解析處理,因此攔截不到,沒法確認是不是一個 PathVariable 仍是就是 url 路徑。因此,**PathVariable 的佔位符是不會起做用的**。 | 無 | 無 | `%{p, 你想查看的路徑參數 key }` | [`PathParameterAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/PathParameterAttribute.java) |

| 請求參數,即 url 的 ? 以後鍵值對,這裏能夠選擇查看某個 key 的值。| 無 | 無 | `%{q, 你想查看的請求參數 key}` | [`QueryParameterAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/QueryParameterAttribute.java) |

| 請求參數字符串,即 url 的 ? 以後的全部字符} |`%q`(不包含 ?)|`%{QUERY_STRING}`(不包含 ?);`%{BARE_QUERY_STRING}`(包含 ?)| 無 | [`QueryStringAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/QueryStringAttribute.java) |

|請求相對路徑(在 Spring Boot 環境下,大多數狀況 RequestPath 和 RelativePath 還有 ResolvedPath 是等價的),即除去 host,port,請求參數字符串的路徑 | `%R` | `%{RELATIVE_PATH}` 或者 `%{REQUEST_PATH}` 或者 `%{RESOLVED_PATH}` | 無 | 分別對應 [`RelativePathAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RelativePathAttribute.java) 和 [`RequestPathAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestPathAttribute.java) 和 [`ResolvedPathAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ResolvedPathAttribute.java)|

| 請求總體字符串,包括請求方法,請求相對路徑,請求參數字符串,請求協議,例如 `Get /test?a=b HTTP/1.1` | `%r` | `%{REQUEST_LINE}` | 無 | [`RequestLineAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestLineAttribute.java) |

| 請求 URI,包括請求相對路徑,請求參數字符串 | `%U` | `%{REQUEST_URL}` | 無 | [`RequestURLAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RequestURLAttribute.java) |

| 處理請求的線程 | `%I` | `%{THREAD_NAME}` | 無 | [`ThreadNameAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ThreadNameAttribute.java) |


注意:


1. 路徑參數 PathVariable 因爲並無被 Undertow 的 Listener 或者 Handler 解析處理,因此攔截不到,沒法確認是不是一個 PathVariable 仍是就是 url 路徑。因此,**PathVariable 的佔位符是不會起做用的**。


## 請求地址相關


| 描述 | 縮寫佔位符 | 全名佔位符 | 參數佔位符 | 源碼 |

|------|-------|------|------|------|

|host 和 port,通常就是 HTTP 請求 Header 中的 Host 值,若是 Host 爲空則獲取本地地址和端口,若是沒獲取到端口則根據協議用默認端口(http:80,,https:443)| 無 | `%{HOST_AND_PORT}` | 無 | [`HostAndPortAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/HostAndPortAttribute.java) |

|請求本地地址 IP| `%A` | `%{LOCAL_IP}` | 無 | [`LocalIPAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/LocalIPAttribute.java)|

|請求本地端口 Port| `%p` | `%{LOCAL_PORT}` | 無 | [`LocalPortAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/LocalPortAttribute.java)|

|請求本地主機名,通常就是 HTTP 請求 Header 中的 Host 值,若是 Host 爲空則獲取本地地址 | `%v` | `%{LOCAL_SERVER_NAME}` | 無 | [`LocalServerNameAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/LocalServerNameAttribute.java)|

|請求遠程主機名,經過鏈接獲取遠端的主機地址 | `%h` | `%{REMOTE_HOST}` | 無 | [`RemoteHostAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RemoteHostAttribute.java)|

|請求遠程 IP,經過鏈接獲取遠端的 IP | `%a` | `%{REMOTE_IP}` | 無 | [`RemoteIPAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/RemoteIPAttribute.java)|


注意:


1. 請求的遠程地址咱們通常不從請求鏈接獲取,而是經過 Http Header 裏面的 `X-forwarded-for` 或者 `X-real-ip` 等獲取,由於如今請求都是經過各類 ***,負載均衡器發上來的。


## 響應相關屬性


| 描述 | 縮寫佔位符 | 全名佔位符 | 參數佔位符 | 源碼 |

|------|-------|------|------|------|

| 發送的字節數大小,除了 Http Header 之外 | `%b` (若是爲空就是 -) 或者 `%B` (若是爲空就是 0) | `%{BYTES_SENT}` (若是爲空就是 0) | 無 | [`BytesSentAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/BytesSentAttribute.java)|

| accesslog 時間,這個不是收到請求的時間,而是響應的時間 | `%t` | `%{DATE_TIME}` | `%{time, 你自定義的 java 中 SimpleDateFormat 的格式}` | [`DateTimeAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/DateTimeAttribute.java)|

| HTTP 響應狀態碼 | `%s` | `%{RESPONSE_CODE}` | 無 | [`ResponseCodeAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ResponseCodeAttribute.java)|

| HTTP 響應緣由 | 無 | `%{RESPONSE_REASON_PHRASE}` | 無 | [`ResponseReasonPhraseAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ResponseReasonPhraseAttribute.java)|

| 響應 Header 的某一個值 | 無 | 無 | `%{o,你要看的header值}` | [`ResponseHeaderAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ResponseHeaderAttribute.java)|

| 響應 Cookie 的某一個值 | 無 | 無 | `%{resp-cookie,你要看的cookie值}` | [`ResponseCookieAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ResponseCookieAttribute.java)|

| 響應時間,**默認 undertow 沒有開啓請求時間內統計,須要打開才能統計響應時間** | `%D`(毫秒,例如 56 表明 56ms) `%T`(秒,例如 5.067 表明 5.067 秒) | `%{RESPONSE_TIME}`(等價於 `%D`) `%{RESPONSE_TIME_MICROS}` (微秒) `%{RESPONSE_TIME_NANOS}`(納秒) | 無 | [`ResponseTimeAttribute`](https://github.com/undertow-io/undertow/blob/2.2.7.Final/core/src/main/java/io/undertow/attribute/ResponseTimeAttribute.java)|


注意:**默認 undertow 沒有開啓請求時間內統計,須要打開才能統計響應時間**,如何開啓呢?經過註冊一個 `WebServerFactoryCustomizer` 到 Spring ApplicationContext 中便可。請看下面的代碼(項目地址:[https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/](https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/)):


[`spring.factories`(省略無關代碼)](https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/spring-cloud-iiford-service-common/src/main/resources/META-INF/spring.factories)


```

# AutoConfiguration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

    com.github.hashjang.spring.cloud.iiford.service.common.auto.UndertowAutoConfiguration

```


[`UndertowAutoConfiguration`](https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/spring-cloud-iiford-service-common/src/main/java/com/github/hashjang/spring/cloud/iiford/service/common/auto/UndertowAutoConfiguration.java)

```

//設置proxyBeanMethods=false,由於沒有 @Bean 的方法互相調用須要每次返回同一個 Bean,不必代理,關閉增長啓動速度

@Configuration(proxyBeanMethods = false)

@Import(WebServerConfiguration.class)

public class UndertowAutoConfiguration {

}

```


[`WebServerConfiguration`](https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/spring-cloud-iiford-service-common/src/main/java/com/github/hashjang/spring/cloud/iiford/service/common/undertow/WebServerConfiguration.java)


```

//設置proxyBeanMethods=false,由於沒有 @Bean 的方法互相調用須要每次返回同一個 Bean,不必代理,關閉增長啓動速度

@Configuration(proxyBeanMethods = false)

public class WebServerConfiguration {

    @Bean

    public WebServerFactoryCustomizer<ConfigurableUndertowWebServerFactory> undertowWebServerAccessLogTimingEnabler(ServerProperties serverProperties) {

        return new DefaultWebServerFactoryCustomizer(serverProperties);

    }

}

```


[`DefaultWebServerFactoryCustomizer`](https://github.com/HashZhang/spring-cloud-scaffold/blob/master/spring-cloud-iiford/spring-cloud-iiford-service-common/src/main/java/com/github/hashjang/spring/cloud/iiford/service/common/undertow/DefaultWebServerFactoryCustomizer.java)

```

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

}

```

相關文章
相關標籤/搜索