mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-09 20:57:21 +08:00
feat: Web-flux增加请求日志打印
This commit is contained in:
@@ -21,16 +21,22 @@ import io.netty.channel.ChannelOption;
|
|||||||
import io.netty.handler.timeout.ReadTimeoutHandler;
|
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||||
import io.netty.handler.timeout.WriteTimeoutHandler;
|
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||||
import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
||||||
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
||||||
import org.springframework.web.reactive.function.client.ClientRequest;
|
import org.springframework.web.reactive.function.client.ClientRequest;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
|
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
|
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
|
||||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.netty.http.client.HttpClient;
|
import reactor.netty.http.client.HttpClient;
|
||||||
import top.continew.admin.job.api.JobApi;
|
import top.continew.admin.job.api.JobApi;
|
||||||
import top.continew.admin.job.api.JobBatchApi;
|
import top.continew.admin.job.api.JobBatchApi;
|
||||||
@@ -46,6 +52,7 @@ import top.continew.admin.job.constant.JobConstants;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class HttpExchangeConfiguration {
|
public class HttpExchangeConfiguration {
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
@@ -74,30 +81,55 @@ public class HttpExchangeConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
public HttpServiceProxyFactory httpServiceProxyFactory() {
|
public HttpServiceProxyFactory httpServiceProxyFactory() {
|
||||||
HttpClient httpClient = HttpClient.create()
|
HttpClient httpClient = HttpClient.create()
|
||||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
|
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000)
|
||||||
.doOnConnected(conn -> {
|
.doOnConnected(conn -> {
|
||||||
conn.addHandlerLast(new ReadTimeoutHandler(10));
|
conn.addHandlerLast(new ReadTimeoutHandler(10));
|
||||||
conn.addHandlerLast(new WriteTimeoutHandler(10));
|
conn.addHandlerLast(new WriteTimeoutHandler(10));
|
||||||
});
|
}).wiretap(true);
|
||||||
|
|
||||||
WebClient webClient = WebClient.builder()
|
WebClient webClient = WebClient.builder()
|
||||||
.codecs(config -> config.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper)))
|
.codecs(config -> config.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper)))
|
||||||
.codecs(config -> config.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper)))
|
.codecs(config -> config.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper)))
|
||||||
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
.clientConnector(new ReactorClientHttpConnector(httpClient))
|
||||||
.filter((request, next) -> {
|
.filter(logRequest())
|
||||||
// 设置请求头
|
.filter(logResponse())
|
||||||
ClientRequest filtered = ClientRequest.from(request)
|
.filter((request, next) -> {
|
||||||
.header(JobConstants.NAMESPACE_ID_HEADER, namespace)
|
// 设置请求头
|
||||||
.header(JobConstants.AUTH_TOKEN_HEADER, jobClient().getToken())
|
ClientRequest filtered = ClientRequest.from(request)
|
||||||
.build();
|
.header(JobConstants.NAMESPACE_ID_HEADER, namespace)
|
||||||
return next.exchange(filtered);
|
.header(JobConstants.AUTH_TOKEN_HEADER, jobClient().getToken())
|
||||||
})
|
.build();
|
||||||
.baseUrl(baseUrl)
|
return next.exchange(filtered);
|
||||||
.build();
|
})
|
||||||
return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
|
.baseUrl(baseUrl)
|
||||||
|
.build();
|
||||||
|
return HttpServiceProxyFactory.builderFor(WebClientAdapter.create(webClient)).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JobClient jobClient() {
|
public JobClient jobClient() {
|
||||||
return new JobClient(baseUrl, username, password);
|
return new JobClient(baseUrl, username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印请求日志
|
||||||
|
*/
|
||||||
|
private ExchangeFilterFunction logRequest() {
|
||||||
|
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
||||||
|
log.info("---> {} {}", clientRequest.method(), clientRequest.url());
|
||||||
|
return Mono.just(clientRequest);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印响应日志
|
||||||
|
*/
|
||||||
|
private ExchangeFilterFunction logResponse() {
|
||||||
|
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> clientResponse.bodyToMono(String.class).flatMap(body -> {
|
||||||
|
log.info("<--- {}", clientResponse.statusCode());
|
||||||
|
log.info("Content-Type:{}", clientResponse.headers().contentType().orElse(MediaType.APPLICATION_JSON));
|
||||||
|
log.info("body: {}", body);
|
||||||
|
return Mono.just(ClientResponse.from(clientResponse).body(body).build());
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user