您的位置:首页 > 理论基础 > 计算机网络

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

2017-06-14 22:59 986 查看

一、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
@EnableFeignClients
public class SpringConsumerServerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConsumerServerFeignApplication.class, args);
}
}
4、配置文件 application.yml
spring:
application:
name: spring-consumer-server-feign
server:
port: 8084
context-path: /spring
#服务注册中心的配置内容,指定服务注册中心的位置
eureka:
client:
serviceUrl:
defaultZone: http://user:password@localhost:8761/eureka/[/code] 

三、自定义Feign的 配置

1、自定义Configuration
@Configuration
public class FooConfiguration {
@Bean
public Contract feignContract() {
//这将SpringMvc Contract 替换为feign.Contract.Default
return 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客户端的接口的完整类名。Feign日志记录仅响应DEBUG级别。logging.level.project.user.UserClient: DEBUG
在配置文件application.yml 中加入:
logging:
level:
com.jalja.org.spring.simple.dao.FeignUserClient: DEBUG
在自定义的Configuration的类中添加日志级别
@Configuration
public 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。该做法除非一些特殊场景,不推荐使用。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: