Feign

4 篇文章 0 订阅
1 篇文章 0 订阅

什么是Feign(官网解释)

为服务消费者(Consumer)整合Feign

依赖:

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

使用Feign 非常的简单- 创建一个接口,并在接口的添加一些注解即可,如下

/**

* @author caisetian:

* @create date:2018年9月8日 下午5:18:50

* @FeignClient 注解中的microservice-provider-user 是一个任意的客户端的名称,用于创建Ribbon 负载均衡器,由于本例

* 中使用了Eureka ,所以Ribbon 会把 microservice-provider-user 解析成Eureka Server 服务注册表中的服务。

* 还可使用url 属性指定请求的URL(URL可以是完整的URL 或者主机名)

* 例如: @FeignClient(name = "microservice-provider-user",url="http://localhost:8000")

* 然后在Controller 中调用即可

*/

@FeignClient(name = "microservice-provider-user")

public interface UserFeignClient {

 

@RequestMapping(value="/{id}",method=RequestMethod.GET)

public User findById(@PathVariable("id") Long id);

}

在Controller 中使用

/**

* @author caiqiufang:

* @create date:2018年8月29日 下午11:00:55

*/

@RestController

public class ConsumerMovieController {

// @Autowired

// RestTemplate restTemplate;

@Autowired

private UserFeignClient userFeignClient;

@GetMapping("/user/{id}")

public User user(@PathVariable Long id){

return userFeignClient.findById(id);

}

}

然后为SpringBoot 启动类添加如下注解: @EnableFeignClients 即可

 

自定义Feign 配置:

在SpringCloud 中默认的配置类是FeignClientsConfiguration,该类定义了Feign默认使用的编码器,解码器,所有使用的契约等。通过注解@FeignClient 的configuration 来指定自定义配置类,由先级比FeignClientsConfiguration 高。

 

http://cloud.spring.io/spring-cloud-static/Edgware.SR4/single/spring-cloud.html#netflix-feign-starter 地址中可以看到如下,意思是说springcloud 提供了如下的自动配置,另外虽然有的配置没有提供默认值,但是也会扫描其中列出的类型(也支持自定义)

Spring Cloud Netflix provides the following beans by default for feign (BeanType beanName: ClassName):

  • Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
  • Encoder feignEncoder: SpringEncoder
  • Logger feignLogger: Slf4jLogger
  • Contract feignContract: SpringMvcContract
  • Feign.Builder feignBuilder: HystrixFeign.Builder
  • Client feignClient: if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used.

The OkHttpClient and ApacheHttpClient feign clients can be used by setting feign.okhttp.enabled or feign.httpclient.enabled to true, respectively, and having them on the classpath. You can customize the HTTP client used by providing a bean of either ClosableHttpClient when using Apache or OkHttpClient whe using OK HTTP.

Spring Cloud Netflix does not provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client:

  • Logger.Level
  • Retryer
  • ErrorDecoder
  • Request.Options
  • Collection<RequestInterceptor>
  • SetterFactory

 

Feign对压缩的支持

一些场景下可能需要请求或响应进行压缩,此时可以使用以下属性 启用Feign的压缩功能“:

feign.compression.request.enable = true

feign.compression.reponse.enable = false

 

feign:

compression:

request:

mime-types:

- text/xml # 用于支持的媒体类型

- application/xml

- application/json

min-request-size: 2048 # 设置请求的最小阈值

Feign 对日志的支持:

/**

* 需要注意Feign 的日志打印只会对DEGUB 级别做出响应

* Logger.Level有 :

* NONE: 不做任何的日志记录

* BASIC:仅记录请求方法,URL, 响应状态代码以及执行时间

* HEADERS:记录BASIC 级别的基础上,记录请求和响应的header

* FULLL: 记录请求和响应的header ,body及元数据。

*

* @return

*/

@Bean

Logger.Level feignLoggerLevel(){

return Logger.Level.FULL;

}

 

标注: 感谢 周立 微服务实战相关书籍

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值