说明
OpenFeign可以将提供者提供的Restful服务伪装为接口进行消费,消费者只需使用“feign
接口 + 注解”的方式即可直接调用提供者提供的 Restful 服务,而无需再使用 RestTemplate。
maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
接口注解
@FeignClient("liqy-provider-depart")
// 参数为要调用的提供者相应的uri
@RequestMapping("/liqy")
public interface DepartService {
@PostMapping("/save")
boolean saveDepart(@RequestBody Data data);
}
启动类注解
@EnableFeignClients // 启用所有Feign客户端
超时设置
feign:
client:
config:
default:
# 连接超时
connectTimeout: 5000 # 单位:毫秒
# 读超时
readTimeout: 5000 # 单位:毫秒
Spring Cloud OpenFeign:优雅地调用远程Restful服务
OpenFeign是Spring Cloud的一个组件,它简化了服务间的调用,允许开发者通过注解定义接口来消费Restful API。在启动类中启用Feign客户端,然后在接口上使用@FeignClient和@RequestMapping注解,即可实现无须使用RestTemplate的直接调用。同时,还能配置超时参数如connectTimeout和readTimeout,提高系统稳定性。
6938

被折叠的 条评论
为什么被折叠?



