在spring cloud netflix栈中,各个微服务都是以http接口的形式暴露自身服务的,因此在调用远程服务时就必须使用http客户端。我们可以使用jdk原生的urlconnection、apache的http client、netty的异步http client, spring的resttemplate。但是,用起来最方便、最优雅的还是要属feign了。
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个HTTP请求。
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-feign server: port: 8084 context-path: /spring #服务注册中心的配置内容,指定服务注册中心的位置 eureka: client: serviceUrl: defaultZone: http://user:password@localhost:8761/eureka/
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。该做法除非一些特殊场景,不推荐使用。
<br/><br/>
以上就是spring cloud 之 Feign 使用HTTP请求远程服务的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号