一 pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>ch4-1</artifactId> <groupId>cn.springcloud.book</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ch4-1-hello</artifactId> <packaging>jar</packaging> <name>ch4-1-hello</name> <url>http://springcloud.cn</url> <properties> <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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>二 入口程序
package cn.springcloud.book.feign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class SpringCloudFeignApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudFeignApplication.class, args); } }三 编写Feign的接口
package cn.springcloud.book.feign.service; 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; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "github-client", url = "https://api.github.com", configuration = HelloFeignServiceConfig.class) public interface HelloFeignService { /** * content: {"message":"Validation Failed","errors":[{"resource":"Search","field":"q","code":"missing"}], * "documentation_url":"https://developer.github.com/v3/search"} * @param queryStr * @return */ @RequestMapping(value = "/search/repositories", method = RequestMethod.GET) ResponseEntity<byte[]> searchRepo(@RequestParam("q") String queryStr); }四 编写控制器并依赖注入Feign
package cn.springcloud.book.feign.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.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloFeignController { @Autowired private HelloFeignService helloFeignService; // 服务消费者对外提供的服务 @GetMapping(value = "/search/github") public ResponseEntity<byte[]> searchGithubRepoByStr(@RequestParam("str") String queryStr) { return helloFeignService.searchRepo(queryStr); } }五 编写配置类
package cn.springcloud.book.feign.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; } }六 配置文件
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压缩七 运行结果