OpenFeign
OpenFeign 是 Spring 在 Feign 的基础上封装的 Web 服务客户端,提供了对 Spring Web MVC 注解的支持。
如何引入
- 引入依赖
org.springframework.cloud:spring-cloud-starter-openfeign。 - 启动类添加
@EnableFeignClients注解。 - 使用 Spring Web MVC 注解定义接口,使用
@FeignClient标记。
@FeignClient("pay")
public interface PayService {
@PostMapping("/pay")
String pay(@RequestParam("amount") BigDecimal amount);
}
- 注入 bean,调用。
@Service
public class SimpleOrderService implements OrderService {
@Resource
private PayService payService;
public String order(String goodCode) {
String payResult = payService.pay(BigDecimal.TEN);
return "ok";
}
}

本文介绍OpenFeign作为Spring Cloud的一部分,如何通过@FeignClient和SpringWebMVC注解简化Web服务客户端的定义与调用。OpenFeign提供了一种声明式的方法来创建REST客户端,通过简单的接口定义即可实现远程服务调用。
2436

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



