1. 什么是 okhttp ?
okhttp 是由 square 公司開源的一個(gè) http 客戶端。在 Java 平臺(tái)上,Java 標(biāo)準(zhǔn)庫(kù)提供了 HttpURLConnection 類來(lái)支持 HTTP 通訊。不過(guò) HttpURLConnection 本身的 API 不夠友好,所提供的功能也有限。大部分 Java 程序都選擇使用 Apache 的開源項(xiàng)目 HttpClient 作為 HTTP 客戶端。Apache HttpClient 庫(kù)的功能強(qiáng)大,使用率也很高。
2. 為什么要使用 okhttp ?
okhttp 的設(shè)計(jì)初衷就是簡(jiǎn)單和高效,這也是我們選擇它的重要原因之一。它的優(yōu)勢(shì)如下:
- 支持 HTTP/2 協(xié)議。
- 允許連接到同一個(gè)主機(jī)地址的所有請(qǐng)求,提高請(qǐng)求效率。
- 共享Socket,減少對(duì)服務(wù)器的請(qǐng)求次數(shù)。
- 通過(guò)連接池,減少了請(qǐng)求延遲。
- 緩存響應(yīng)數(shù)據(jù)來(lái)減少重復(fù)的網(wǎng)絡(luò)請(qǐng)求。
- 減少了對(duì)數(shù)據(jù)流量的消耗。
- 自動(dòng)處理GZip壓縮。
3. 實(shí)戰(zhàn)目標(biāo)
- Feign 中使用 okhttp 替代 httpclient
- Zuul 中使用 okhttp 替代 httpclient
4. 在 Feign 中使用 okhttp
首先介紹一下工程結(jié)構(gòu),本演示工程包含 provider-server、consumer-server、eureka-server 和 zuul-server 。
4.1 consumer-server 依賴 pom.xml 如下:
代碼清單:chapter19/consumer-server/pom.xml
***
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
</dependencies>
feign-okhttp這里無(wú)需指定版本,目前引入的feign-okhttp版本為 10.2.3 ,而okhttp的版本為 3.8.1 ,如圖:
4.2 配置文件 application.yml
代碼清單:chapter19/consumer-server/src/main/resources/application.yml
***
feign:
httpclient:
enabled: false
okhttp:
enabled: true
- 在配置文件中需關(guān)閉 feign 對(duì) httpclient 的使用并開啟 okhttp 。
4.3 配置類 OkHttpConfig.java
代碼清單:chapter19/consumer-server/src/main/java/com/springcloud/consumerserver/config/OkHttpConfig.java
***
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class OkHttpConfig {
@Bean
public OkHttpClient okHttpClient(){
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.connectionPool(new ConnectionPool(10 , 5L, TimeUnit.MINUTES))
.addInterceptor(new OkHttpLogInterceptor())
.build();
}
}
- 在配置類中將
OkHttpClient注入 Spring 的容器中,這里我們指定了連接池的大小,最大保持連接數(shù)為 10 ,并且在 5 分鐘不活動(dòng)之后被清除。 - 筆者這里配置了一個(gè) okhttp 的日志攔截器。
4.4 日志攔截器 OkHttpLogInterceptor.java
代碼清單:
***
@Slf4j
public class OkHttpLogInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
log.info("OkHttpUrl : " + chain.request().url());
return chain.proceed(chain.request());
}
}
- 這里實(shí)現(xiàn)的接口是
okhttp3.Interceptor,并不是 Spring Boot 中的 Interceptor。 - 筆者這里僅簡(jiǎn)單打印了 okhttp 請(qǐng)求的路徑,如果有業(yè)務(wù)校驗(yàn)權(quán)限等需求可以放在攔截器中實(shí)現(xiàn)。
遠(yuǎn)程 Feign 調(diào)用代碼略過(guò),有需要的讀者可以訪問(wèn) Github 倉(cāng)庫(kù)獲取。
5. 在 Zuul 中使用 okhttp
5.1 pom.xml 加入 okhttp 依賴
代碼清單:chapter19/zuul-server/pom.xml
***
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
5.2 配置文件開啟 okhttp
代碼清單:chapter19/zuul-server/src/main/resources/application.yml
***
ribbon:
http:
client:
enabled: false
okhttp:
enabled: true
- 因?yàn)?Zuul 的負(fù)載均衡實(shí)現(xiàn)是通過(guò) Ribbon 實(shí)現(xiàn)的,所以 Http 客戶端的配置自然也是對(duì) Ribbon 組件的配置。
6. 測(cè)試
我們修改 idea 啟動(dòng)配置,分別在 8000 和 8001 端口啟動(dòng) provider-server ,并且順次啟動(dòng)其余工程,打開瀏覽器訪問(wèn)鏈接:http://localhost:8080/consumer/hello ,多次刷新,可以看到 Hello Spring Cloud! Port : 8000 和 Hello Spring Cloud! Port : 8001 交替書出現(xiàn),可以證明負(fù)載均衡已經(jīng)成功,可以查看 consumer-server 的日志,如下:
2019-09-23 23:15:27.097 INFO 10536 --- [nio-9000-exec-5] c.s.c.intercepter.OkHttpLogInterceptor : OkHttpUrl : http://host.docker.internal:8001/hello
2019-09-23 23:15:27.593 INFO 10536 --- [nio-9000-exec-6] c.s.c.intercepter.OkHttpLogInterceptor : OkHttpUrl : http://host.docker.internal:8000/hello
2019-09-23 23:15:27.942 INFO 10536 --- [nio-9000-exec-7] c.s.c.intercepter.OkHttpLogInterceptor : OkHttpUrl : http://host.docker.internal:8001/hello
2019-09-23 23:15:28.251 INFO 10536 --- [nio-9000-exec-9] c.s.c.intercepter.OkHttpLogInterceptor : OkHttpUrl : http://host.docker.internal:8000/hello
2019-09-23 23:15:47.877 INFO 10536 --- [nio-9000-exec-8] c.s.c.intercepter.OkHttpLogInterceptor : OkHttpUrl : http://host.docker.internal:8001/hello
可以看到我們剛才自定義的日志正常打印,證明現(xiàn)在訪問(wèn)確實(shí)是通過(guò) okhttp 來(lái)進(jìn)行訪問(wèn)的。
7. 示例代碼
示例代碼-Github
示例代碼-Gitee
|轉(zhuǎn)載請(qǐng)注明來(lái)源地址:蜘蛛池出租 http://www.wholesalehouseflipping.com/專注于SEO培訓(xùn),快速排名黑帽SEO https://www.heimao.wiki
