SpringCloudFeign支持对请求和响应进行gzip压缩,以此来提高通信效率。

1、搭建gzip-demo工程

1.1、工程依赖:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud OpenFeign的Starter的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

1.2、工程启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
public class FeignGzipApplication { public static void main(String[] args) {
SpringApplication.run(FeignGzipApplication.class, args);
}
}

1.3、编写测试代码:

client接口:

import cn.springcloud.book.feign.config.HelloFeignServiceConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; //https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/forecast.json 彩云天气API
@FeignClient(name = "caiyunapp", url = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552", configuration = HelloFeignServiceConfig.class)
public interface HelloFeignService { @RequestMapping(value = "/forecast.json", method = RequestMethod.GET)
ResponseEntity<byte[]> searchRepo(); }

config配置类:

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class HelloFeignServiceConfig { /**
*
* Logger.Level 的具体级别如下:
NONE:不记录任何信息
BASIC:仅记录请求方法、URL以及响应状态码和执行时间
HEADERS:除了记录 BASIC级别的信息外,还会记录请求和响应的头信息
FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据
* @return
*/
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
} }

controller类:

import cn.springcloud.book.feign.service.HelloFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloFeignController { @Autowired
private HelloFeignService helloFeignService; // 服务消费者对位提供的服务
@GetMapping(value = "/search")
public ResponseEntity<byte[]> searchGithubRepoByStr() {
return helloFeignService.searchRepo();
} }

1.4、工程配置文件:

server:
port: 8011
spring:
application:
name: ch4-1-gzip logging:
level:
cn.springcloud.book.feign.service.HelloFeignService: debug feign:
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json # 配置压缩支持的MIME TYPE
min-request-size: 2048 # 配置压缩数据大小的下限
response:
enabled: true # 配置响应GZIP压缩

2、启动工程

访问 http://localhost:8011/search

大功告成!!!

由于开启GZIP压缩之后,Feign之间的调用通过二进制协议进行传输,返回值需要修改为ResponseEntity<byte[]>才可以正常显示,否则会导致服务之间的调用结果乱码。

###提示:

提示1:SpringCloud Finchley.RC2版本中,开启Feign GZIP压缩时,会出现乱码,具体解决方案见:https://github.com/spring-cloud/spring-cloud-openfeign/issues/33

提示2:SpringCloud Finchley.M9版本中,开启Feign GZIP压缩会报错。具体解决方案见:https://github.com/spring-cloud/spring-cloud-openfeign/issues/14

最新文章

  1. hive删除数据
  2. ffmpeg-20160325-snapshot-static-bin
  3. [OpenCV] Feature Matching
  4. [运维-服务器 – 2A] – nginx下绑定域名
  5. 【翻译习作】 Windows Workflow Foundation程序开发-第一章04
  6. Rstdio中更换R版本
  7. dsPIC33EP ADC模块初始化及应用实例
  8. 【oracle】Enterprise Manager 无法连接到数据库实例。下面列出了组件的状态---个人解决方案
  9. php校验
  10. asp.net 配置二级域名的共享session,并实现sso单点登录
  11. Linux命令之 文件归档管理
  12. lpc1788控制步进电机28BYJ-48
  13. Apache URL转发
  14. 真机调试以及“Could not find Developer Disk Image”问题解决方案
  15. 通过startup启动tomcat一闪而过的问题
  16. Word2vec 讨论
  17. 某集团BI决策系统建设方案分享
  18. Windows Server 2016-客户端加域端口汇总
  19. 对C# .Net4.5异步机制测试
  20. poj 2528 Mayor’s posters 【离散化】+【线段树】

热门文章

  1. ios兼容
  2. 小程序 之wx.request和wx.showModal简单封装
  3. Apache Flink - 常见数据流类型
  4. 【转】Eclipse MAT内存分析工具(Memory Analyzer Tool)
  5. Go by Example-流控制语句之if/else
  6. Qt之模型/视图(委托)
  7. arcgis python 一个mxd打包mpk
  8. 数字图像处理--算术、几何、谐波、逆谐波均值滤波器Matlab
  9. “kill -9” 和 “kill -15” 有什么不同
  10. SQL SERVER SELECT语句中加锁选项的详细说明