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