1. What is okhttp?
okhttp is open source company by the square an http client. On the Java platform, Java standard library provides classes for HTTP communication HttpURLConnection. But HttpURLConnection API itself is not friendly enough, the functionality provided is limited. Most Java programs are choosing to use Apache open source projects HttpClient as HTTP client. Apache HttpClient library of powerful, usage is also high.
2. Why use okhttp?
okhttp is designed is simple and efficient, which is why we selected it one of the important reasons. Its advantages are as follows:
- Supports HTTP / 2 protocol.
- Allow all requests to connect to the same host address, the request to improve efficiency.
- Share Socket, reducing the number of server requests.
- Through a connection pool, the request to reduce latency.
- Response data cache to reduce duplicate network request.
- Reduce the consumption of data traffic.
- Automatic processing GZip compression.
3. actual target
- Feign used okhttp alternative httpclient
- Alternatively httpclient Zuul used okhttp
4. okhttp in the Feign
First, some engineering structures, this demonstration project contains provider-server, consumer-server, eureka-server and zuul-server.
4.1 consumer-server-dependent pom.xml as follows:
Listing: 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
Here without specifying the version introduced in the currentfeign-okhttp
version 10.2.3, andokhttp
the version is 3.8.1, as:
4.2 Profile application.yml
Listing: chapter19 / Consumer-Server / src / main / Resources / application.yml
***
feign:
httpclient:
enabled: false
okhttp:
enabled: true
- In the configuration file to be closed to use feign httpclient and open okhttp.
4.3 configuration class 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();
}
}
- In the configuration class
OkHttpClient
injection Spring container, that we specify the size of the connection pool, the maximum number of holding connection 10, and is removed after 5 minutes of inactivity. - Here the author is configured with a okhttp log interceptors.
4.4 log interceptor OkHttpLogInterceptor.java
Listing:
***
@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());
}
}
- Here implement interfaces are
okhttp3.Interceptor
not Spring Boot the Interceptor. - Here simply print path okhttp request, check if there are traffic rights and other needs can be placed interceptor implemented.
Remote Feign calling code ignored, there is a need readers can get access Github repository.
5. okhttp in the Zuul
5.1 pom.xml added okhttp dependent
Listing: chapter19 / Zuul-Server / pom.xml
***
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
5.2 configuration files open okhttp
Listing: chapter19 / Zuul-Server / src / main / Resources / application.yml
***
ribbon:
http:
client:
enabled: false
okhttp:
enabled: true
- Because the load balancing implementation Zuul is achieved through the Ribbon, so the configuration Http client is configured Ribbon natural components.
6. Test
We modify the idea to start the configuration, respectively, in 8000 and 8001 ports starting provider-server, and in turn starts the rest of the project, open the browser to access the link: http: // localhost: 8080 / consumer / hello, repeatedly refresh, you can see Hello Spring Cloud! Port : 8000
and Hello Spring Cloud! Port : 8001
alternately book appears, you can prove that load balancing has been successful, you can view consumer-server log, as follows:
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
You can see we have just log normal custom printing, now have access to really prove to be accessed by the okhttp.