亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java?Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時時間配置

 更新時間:2024年01月30日 10:02:04   作者:ayuyihu  
Hutool是一個Java基礎工具類,對文件、流、加密解密、轉碼、正則、線程、XML等JDK方法進行封裝,組成各種Util工具類,這篇文章主要給大家介紹了關于Java?Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時時間配置的相關資料,需要的朋友可以參考下

為何要打印Http請求日志

使用hutool工具包中的HttpUtil,為了便于排查問題以及控制請求時間,每次都要在請求前后log日志,每次都需要設置超時時間,十分麻煩。

        log.info("請求路徑:{},請求體:{}", url, body);
        HttpResponse response = HttpUtil.createPost(url).body(body).timeout(3000).execute();
        log.info("請求結果:{}", response);

HttpUtil的請求攔截器(HttpInterceptor.Chain)、響應攔截器(HttpInterceptor.Chain)

從HttpUtil的execute()方法點進去幾步,可以看到以下代碼

	/**
	 * 請求前的攔截器,用于在請求前重新編輯請求
	 */
	final HttpInterceptor.Chain<HttpRequest> requestInterceptors = GlobalInterceptor.INSTANCE.getCopiedRequestInterceptor();
	/**
	 * 響應后的攔截器,用于在響應后處理邏輯
	 */
	final HttpInterceptor.Chain<HttpResponse> responseInterceptors = GlobalInterceptor.INSTANCE.getCopiedResponseInterceptor();
	

	/**
	 * 執(zhí)行Reuqest請求
	 *
	 * @param isAsync 是否異步
	 * @return this
	 */
	public HttpResponse execute(boolean isAsync) {
		return doExecute(isAsync, config.requestInterceptors, config.responseInterceptors);
	}

這里有兩個攔截器配置,分別是請求攔截器配置config.requestInterceptors, 響應攔截器配置config.responseInterceptors

繼續(xù)點進去可以看到攔截器的執(zhí)行邏輯,分別在請求前和請求后執(zhí)行,代碼進行省略,只保留我們需要的邏輯

	/**
	 * 執(zhí)行Reuqest請求
	 *
	 * @param isAsync              是否異步
	 * @param requestInterceptors  請求攔截器列表
	 * @param responseInterceptors 響應攔截器列表
	 * @return this
	 */
	private HttpResponse doExecute(boolean isAsync, HttpInterceptor.Chain<HttpRequest> requestInterceptors,
								   HttpInterceptor.Chain<HttpResponse> responseInterceptors) {
		if (null != requestInterceptors) {
			for (HttpInterceptor<HttpRequest> interceptor : requestInterceptors) {
				interceptor.process(this);
			}
		}

		// 初始化URL
		urlWithParamIfGet();
		// 初始化 connection
		initConnection();
		// 發(fā)送請求
		send();

		// 手動實現(xiàn)重定向
		HttpResponse httpResponse = sendRedirectIfPossible(isAsync);

		// 獲取響應
		if (null == httpResponse) {
			httpResponse = new HttpResponse(this.httpConnection, this.config, this.charset, isAsync, isIgnoreResponseBody());
		}

		// 攔截響應
		if (null != responseInterceptors) {
			for (HttpInterceptor<HttpResponse> interceptor : responseInterceptors) {
				interceptor.process(httpResponse);
			}
		}

		return httpResponse;
	}

HttpUtil的全局日志配置

明白了HttpUtil的execute()方法執(zhí)行過程,現(xiàn)在我們可以很方便的實現(xiàn)HttpUtil的全局日志了

項目啟動時進行添加我們自定義的攔截器即可,這里直接使用toString方法,是因為HttpUtil里面幫我們重寫了toString的邏輯,不需要我們自己去解析HttpRequest和HttpResponse了

    @Autowired
    public void addRequestInterceptor() {
        GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj)));
        GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj)));
    }

HttpUtil的超時時間源碼分析

同樣的,我們點進timeout()方法可以發(fā)現(xiàn),會分別設置連接超時、讀取響應超時。

	/**
	 * 默認連接超時
	 */
	int connectionTimeout = HttpGlobalConfig.getTimeout();
	/**
	 * 默認讀取超時
	 */
	int readTimeout = HttpGlobalConfig.getTimeout();

	/**
	 * 設置超時,單位:毫秒<br>
	 * 超時包括:
	 *
	 * <pre>
	 * 1. 連接超時
	 * 2. 讀取響應超時
	 * </pre>
	 *
	 * @param milliseconds 超時毫秒數(shù)
	 * @return this
	 * @see #setConnectionTimeout(int)
	 * @see #setReadTimeout(int)
	 */
	public HttpConfig timeout(int milliseconds) {
		setConnectionTimeout(milliseconds);
		setReadTimeout(milliseconds);
		return this;
	}
	
	/**
	 * 設置連接超時,單位:毫秒
	 *
	 * @param milliseconds 超時毫秒數(shù)
	 * @return this
	 */
	public HttpConfig setConnectionTimeout(int milliseconds) {
		this.connectionTimeout = milliseconds;
		return this;
	}
	
	/**
	 * 設置連接超時,單位:毫秒
	 *
	 * @param milliseconds 超時毫秒數(shù)
	 * @return this
	 */
	public HttpConfig setReadTimeout(int milliseconds) {
		this.readTimeout = milliseconds;
		return this;
	}

但是這兩個超時字段配置是由默認的全局配置的。

所以配置一個全局的超時時間就比較方便了

HttpUtil的全局超時時間配置

    @Autowired
    public void hutoolHttpUtilConfig() {
        // 設置hutool HttpUtil的request請求參數(shù)打印
        GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj)));

        // 設置hutool HttpUtil的response參數(shù)打印
        GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj)));

        // 設置hutool HttpUtil的連接超時時間、讀取響應超時時間
        HttpGlobalConfig.setTimeout(3000);
    }

附:hutool-http使用注意

Hutool諧音“糊涂”,一方面簡潔易懂,一方面寓意“難得糊涂”,但是糊涂中可能就會掉入坑中。查看HttpUtil源碼,再調(diào)用HttpUtil中的post或者get方法后,會創(chuàng)建HttpRequest對象調(diào)用execute()方法,建立連接發(fā)送請求。

但是初始化連接的時候會直接創(chuàng)建一個新的連接,如果要循環(huán)調(diào)用大量接口,這個是很不合適的。

總結 

到此這篇關于Java Hutool工具包中HttpUtil的日志統(tǒng)一打印及統(tǒng)一超時時間配置的文章就介紹到這了,更多相關HttpUtil日志統(tǒng)一打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論