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";
}
}