转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/87877080谢谢
Spring Cloud Feign
Spring Cloud Feign 概述
Spring Cloud Feign 基于Netflix Feign 整合了Spring Cloud Ribbon与Spring Cloud Hystrix,还提供了一种声明式的web服务客户端的定义方式,具备可插拔的注解,包括Feign注解和JAX-RS注解,还支持可插拔的HTTP编码器与解码器.
Spring Cloud Feign 搭建
- pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
- Application类上
@EnableDiscoveryClient
@EnableFeignClients // 开启Feign
- 自定义Controller
@RestController
public class ConsumerController {
@Autowired
private FeignClient feignClient;
@GetMapping("/{id}")
public String findById(@PathVariable Long id) {
return this.feignClient.findById(id);
}
}
基本
- yml
server:
port: 8010
spring:
application:
name: consumer-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
单个参数请求
- 制定Feign接口
@FeignClient(name = "provider-service") //声明需要调用的服务提供者的名称
public interface FeignClient {
/**
* 与提供者在Controller定义的一样请求方式和请求路径
* 通过编写"翻译器"(Contract),可以让Feign知道第三方注解的含义
* 同样,SpringCloud也提供了翻译器,会将注解告诉Feign,因此,接口可以直接使用该注解
* 默认支持注解:@RequestMapping、@RequestParam、@RequestHeader、@PathVariable
*/
@RequestMapping(value = "/provider/{id}", method = RequestMethod.GET)
public String findById(@PathVariable("id") Long id);
}
多参数请求
- 自定义Feign接口
使用Feign构造多参数请求:1.使用post传递对象;2.get在参数列表上分别一个一个,隔开的传输;3.get 传递map
@FeignClient(name = "provider-service")
public interface UserFeignClient {
//该请求不会成功,错误代码
@RequestMapping(value = "/provider/get", method = RequestMethod.GET)
public User get0(User user);
@RequestMapping(value = "/provider/get", method = RequestMethod.GET)
public User get1(@RequestParam("id") Long id, @RequestParam("username") String username);
//传递Map<String, Object> map 提供者可以使用在代码里使用json转换成map
@RequestMapping(value = "/provider/get", method = RequestMethod.GET)
public User get2(@RequestParam("map") String map);
//如果是传递List,是不会成功的 传递数组可以
@RequestMapping(value = "/provider/info/get", method = RequestMethod.GET)
public User get3(@RequestParam("ids") String[] ids);
@RequestMapping(value = "/provider/post", method = RequestMethod.POST)
public User post(@RequestBody User user);
}
使用Feign注解
- yml
server:
port: 8010
spring:
application:
name: consumer-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
- 自定义配置类
/**
* 该类为Feign的配置类
* 注意:该不应该在主应用程序上下文的@ComponentScan中
*/
@Configuration
public class FeignConfiguration {
/**
* 将契约改为feign原生的默认契约。这样就可以使用feign自带的注解了。
* @return 默认的feign契约
*/
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
}
- 制定Feign接口
/**
* 使用@FeignClient的configuration属性,指定feign的配置类。
*/
@FeignClient(name = "provider-service",configuration = FeignConfiguration.class)
public interface FeignClient {
/**
* 使用feign自带的注解@RequestLine
*/
@RequestLine("GET /provider/{id}")
public String findById(@Param("id") Long id);
}
Feign日志配置
- 自定义配置类
@Configuration
public class FeignLogConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.BASIC;
}
}
- 制定Feign接口
/**
* 使用@FeignClient的configuration属性,指定feign的配置类。
*/
@FeignClient(name = "provider-service",configuration = FeignLogConfiguration.class)
public interface FeignClient {
/**
* 使用feign自带的注解@RequestLine
*/
@RequestMapping(value = "/provider/{id}", method = RequestMethod.GET)
public String findById(@PathVariable("id") Long id);
}
- yml
server:
port: 8010
spring:
application:
name: consumer-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
logging:
level:
com.amor.cloud.feign.FeignClient: DEBUG # 将Feign接口的日志级别设置成DEBUG,因为Feign的Logger.Level只对DEBUG作出响应。
Feign压缩配置
feign.compression.request.enabled:设置为true开启请求压缩
feign.compression.response.enabled:设置为true开启响应压缩
feign.compression.request.mine-types:数据类型列表,默认为text/xml,application/xml,application/json
feign.compression.request.min-request-size:设置请求内容的最小阀值,默认2048