spring cloud 之 Feign 使用HTTP请求远程服务

一、Feign 简介

在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端。我们可以使用JDK原生的URLConnection、Apache的Http Client、Netty的异步HTTP Client, Spring的RestTemplate。但是,用起来最方便、最优雅的还是要属Feign了。

Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。

二、feign的使用在spring cloud中的使用

1、添加依赖

      <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId>   </dependency>

2、创建FeignClient

@FeignClient(name="SPRING-PRODUCER-SERVER/spring")public interface FeignUserClient {  @RequestMapping( value = "/findAll/{name}",method = RequestMethod.GET)  public List<SpringUser> findAll(@PathVariable("name") String name);    @RequestMapping( value = "/findUserPost",method = RequestMethod.POST)  public SpringUser findUserPost(@RequestBody SpringUser springUser);//复合类型好像默认是POST请求}

@FeignClient(name="SPRING-PRODUCER-SERVER/spring"):用于通知Feign组件对该接口进行代理(不需要编写接口实现),name属性指定我们要调用哪个服务。使用者可直接通过@Autowired注入。

@RequestMapping表示在调用该方法时需要向/group/{groupId}发送GET请求。

@PathVariable与SpringMVC中对应注解含义相同。

原理:Spring Cloud应用在启动时,Feign会扫描标有@FeignClient注解的接口,生成代理,并注册到Spring容器中。生成代理时Feign会为每个接口方法创建一个RequetTemplate对象,该对象封装了HTTP请求需要的全部信息,请求参数名、请求方法等信息都是在这个过程中确定的,Feign的模板化就体现在这里。

3、启动类上添加注解

@Configuration@ComponentScan@EnableAutoConfiguration@EnableEurekaClient@EnableFeignClientspublic class SpringConsumerServerFeignApplication {public static void main(String[] args) {        SpringApplication.run(SpringConsumerServerFeignApplication.class, args);    }}

4、配置文件 application.yml

spring: application:  name: spring-consumer-server-feignserver:  port: 8084 context-path: /spring#服务注册中心的配置内容,指定服务注册中心的位置eureka: client:  serviceUrl:   defaultZone: http://user:password@localhost:8761/eureka/

三、自定义Feign的 配置

1、自定义Configuration

@Configurationpublic class FooConfiguration {    @Beanpublic Contract feignContract() {//这将SpringMvc Contract 替换为feign.Contract.Defaultreturn new feign.Contract.Default();    }}

2、使用自定义的Configuration

@FeignClient(name="SPRING-PRODUCER-SERVER/spring",configuration=FooConfiguration.class)public interface FeignUserClient {    @RequestLine("GET /findAll/{name}")public List<SpringUser> findAll(@Param("name") String name);     /* @RequestMapping( value = "/findAll/{name}",method = RequestMethod.GET)  public List<SpringUser> findAll(@PathVariable("name") String name);    @RequestMapping( value = "/findUserPost",method = RequestMethod.POST)  public SpringUser findUserPost(@RequestBody SpringUser springUser);*/}

@RequestLine:是feign的注解为每个创建的Feign客户端创建一个记录器。默认情况下,记录器的名称是用于创建Feign客户端的接口的完整类名。Feign日志记录仅响应DEBUG级别。logging.level.project.user.UserClient: DEBUG在配置文件application.yml 中加入:

logging: level:  com.jalja.org.spring.simple.dao.FeignUserClient: DEBUG

在自定义的Configuration的类中添加日志级别

@Configurationpublic class FooConfiguration {   /* @Bean    public Contract feignContract() {        //这将SpringMvc Contract 替换为feign.Contract.Default        return new feign.Contract.Default();    }*/@Bean    Logger.Level feignLoggerLevel() {//设置日志return Logger.Level.FULL;    }}

PS: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 使用HTTP请求远程服务的详细内容,更多请关注其它相关文章!

辽远或偏僻的地方,而会常常想起这一次的旅行,

spring cloud 之 Feign  使用HTTP请求远程服务

相关文章:

你感兴趣的文章:

标签云: