OpenFeign中如何使用明文http2呢?
下文笔者讲述OpenFeign使用明文http2的方法及示例分享,如下所示
http2简介
HTTP2(超文本传输协议第2版) 简称为h2(基于TLS/1.2或以上版本的加密连接)或h2c(非加密连接) 是HTTP协议的的第二个主要版本,使用于万维网 HTTP2的特点: 1.使用静态和动态字典表来减少重复请求头的传输 2.使用哈夫曼编码来压缩整数和字符串 3.服务端推送--避免多次请求
OpenFeign支持Http2的实现思路
openfeign支持Http2是基于底层的Client 只要Client支持http2,则openFeign支持http2 openfeign可使用以下Client: URLConnection: java9之前不支持http2 Apache HttpClient: 5.0开始支持,4.X不支持 OKHttp: 支持, h2或H2_PRIOR_KNOWLEDGE(明文http2)都支持下文笔者采用示例的方式讲述openFeign支持http2的示例
1.引入相应依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <!-- 使用undertow代替tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> 注意事项: undertow在配置文件中 server.http2.enabled=true无效,需采用以下代码的形式配置 import io.undertow.UndertowOptions; import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfig { @Bean public UndertowServletWebServerFactory embeddedServletContainerFactory() { UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory(); factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true)); return factory; } } 2、客户端 引入openfeign及okhttp: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.1.0</version> </dependency> application.yml中配置启用okhttp feign: okhttp: enabled: true 由于使用明文http2 所以需手动指定其协议 配置如下: @Bean public okhttp3.OKHttpClient okHttpClient(){ list<Protocol> protocols = new ArrayList<>(); protocols.add(Protocol.H2_PRIOR_KNOWLEDGE); OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder() .readTimeout(60, TimeUnit.SECONDS) .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(120, TimeUnit.SECONDS) .connectionPool(new ConnectionPool()); // 添加拦截器用于打印响应时所使用的协议 builder.addInterceptor(new ResponseInterceptor()); builder.protocols(protocols); return builder.build(); } 拦截器打印相应的协议 import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; import org.jetbrains.annotations.NotNull; import java.io.IOException; public class ResponseInterceptor implements Interceptor { @NotNull @Override public Response intercept(@NotNull Chain chain) throws IOException { Request request = chain.request(); try { Response response = chain.proceed(request); System.out.println(response.protocol()); return response; } catch (Exception e) { e.printStackTrace(); throw e; } } } //feign接口定义如下: import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import java.util.Map; @FeignClient("demo3-service") public interface Demo3Service { @GetMapping("/v1/demo3/demo") Map<String, String> demo(); } @Autowired private Demo3Service demo3Service; @GetMapping("/demo3") public Map<String, String> demo3() throws Exception { return demo3Service.demo(); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。