一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

二、准备工作

继续用上一节的工程, 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.

三、创建一个feign的服务

  创建feign服务后,配置文件的配置

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port:
spring:
application:
name: service-feign

在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceFeignApplication { public static void main(String[] args) {
SpringApplication.run( ServiceFeignApplication.class, args );
}
}

定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:

@RestController
public class HiController { @Autowired
SchedualServiceHi schedualServiceHi; @GetMapping(value = "/hi")
public String sayHi(@RequestParam String name) {
return schedualServiceHi.sayHiFromClientOne( name );
}
}

Feign 日常采坑

一、FeignClient注解

@FeignClient标签的常用属性如下:

  • name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
  • url: url一般用于调试,可以手动指定@FeignClient调用的地址
  • decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
  • configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
  • fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
  • fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
  • path: 定义当前FeignClient的统一前缀

在使用fallback属性时,需要使用@Component注解,保证fallback类被Spring容器扫描到,GitHubExampleConfig内容如下:

@Configuration
public class GitHubExampleConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}

在使用FeignClient时,Spring会按name创建不同的ApplicationContext,通过不同的Context来隔离FeignClient的配置信息,在使用配置类时,不能把配置类放到Spring App Component scan的路径下,否则,配置类会对所有FeignClient生效.

二、Feign Client 和@RequestMapping

当前工程中有和Feign Client中一样的Endpoint时,Feign Client的类上不能用@RequestMapping注解否则,当前工程该endpoint http请求且使用accpet时会报404
Controller:
@RestController
@RequestMapping("/v1/card")
public class IndexApi { @PostMapping("balance")
@ResponseBody
public Info index() {
Info.Builder builder = new Info.Builder();
builder.withDetail("x", );
builder.withDetail("y", );
return builder.build();
}
}

Feign Client:

@FeignClient(
name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class
)
@RequestMapping(value = "/v1/card")
public interface CardFeignClient { @RequestMapping(value = "/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Info info(); }

if @RequestMapping is used on class, when invoke http /v1/card/balance, like this :

如果 @RequestMapping注解被用在FeignClient类上,当像如下代码请求/v1/card/balance时,注意有Accept header:

Content-Type:application/json
Accept:application/json POST http://localhost:7913/v1/card/balance

那么会返回 404。

如果不包含Accept header时请求,则是OK:

Content-Type:application/json
POST http://localhost:7913/v1/card/balance

或者像下面不在Feign Client上使用@RequestMapping注解,请求也是ok,无论是否包含Accept:

@FeignClient(
name = "card",
url = "http://localhost:7913",
fallback = CardFeignClientFallback.class,
configuration = FeignClientConfiguration.class
) public interface CardFeignClient { @RequestMapping(value = "/v1/card/balance", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
Info info(); }

三、Feign请求超时问题

Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了
解决方案有三种,以feign为例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
该配置是让Hystrix的超时时间改为5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
该配置,用于禁用Hystrix的超时时间
方法三
feign.hystrix.enabled: false
该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。

spring cloud 学习之服务消费者(Feign)的更多相关文章

  1. spring cloud 学习之服务消费者(rest+ribbon)

    学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...

  2. Spring Cloud 声明式服务调用 Feign

    一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...

  3. spring cloud学习(一) 服务注册

    首先spring-cloud相关的简介可以去百度搜索,这里就不多说了,这里分享一个翻译spring cloud官网的中文网站spring cloud中文网 这个学习项目的代码放在 https://gi ...

  4. Spring Cloud(二):服务消费者

    创建“服务消费者” 创建一个基础的Spring Boot工程,命名为springboot-consumer,并在pom.xml中引入需要的依赖内容: <dependency> <gr ...

  5. spring cloud 学习之 服务注册和发现(Eureka)

    一:服务注册和发现(Eureka) 1:采用Eureka作为服务注册和发现组件 2:Eureka 项目中 主要在启动类加上 注解@EnableEurekaServer @SpringBootAppli ...

  6. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  7. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  8. Spring Cloud构建微服务架构(二)服务消费者

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...

  9. spring cloud 学习(3) - feign入门

    feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用.传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码), ...

随机推荐

  1. (转)Java实现Web Service过程中处理SOAP Header的问题

    网上有篇文章,大致这么说的(如下文),最后我采用的wsimport  -XadditionalHeaders的方式. StrikeIron offers two authentication meth ...

  2. csdn下载

    按次收费: http://www.itziy.com/

  3. js报错

    1.如果出现找不到js方法,感觉写的js都正确就是调试报错,可能原因是js文件重复引用 2.在用ajax异步提交时千万别用 submit 控件,submit控件是表单提交控件,提交表单的同时不会执行异 ...

  4. JavaScript Hacks

    JavaScript Hacks,很多都是在网上看到的,觉得好就记下来了.在这里给大家推荐一个项目,里面很多代码片段都值得学习https://github.com/Chalarangelo/30-se ...

  5. 伪响应式开发(PC和Mobile分离)

    screen.width 无论把浏览器缩小还是放大,screen.width的值都不会改变,但是IE9及以上浏览器才支持这个属性. @media screen 媒体查询的巨大缺陷:切换页面布局的时候J ...

  6. NoSQL(Not Only SQL)

    Everything has its properties and has relation with each other. All in world can be related to each ...

  7. 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume

    Android 提供了多种存储数据的方法,其中最简单的是使用Shared Preferences. Shared Preferences 可以存储 Key/value 对,Shared Prefere ...

  8. python 不同目录间的模块调用

    有时候调用的模块不再同一个目录.直接import 是加载不进来的.默认的加载路径是sys.path中指定的路径.如果要指定加载的目录得需要把这个目录加到sys.path里面. 比如要加载父目录的同级目 ...

  9. 然之协同系统3.5(OA+CRM+CASH+TEAM)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mariadb-server 10.0.25 nginx 1.10.0 php7.0.4 collaboration commercial crm ...

  10. K星异客

    http://baike.baidu.com/view/222058.htm 这部改编自基恩·布汝尔1995年出版的同名小说的电影在当年的十月档票房榜上称冠.本来这部电影的外星人主人公属意于威尔.史密 ...