个人理解openfeign = 远程接口调用+ribbon
接口调用过程
service创建在消费者module中,负责负载均衡和远程接口调用,而消费接口只需要调用feiService,就像调用本地方法一样
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
service
@Component
@FeignClient(value = "PAYMENT-SERVICE") // 注册中心的服务名,可以调用这些同名的服务实例
public interface OrderService {
@GetMapping("/payment/get/{id}") // 远程服务接口
public CommonResult<Payment> get(@PathVariable("id") int id);
}
controller 调用
@Slf4j
@RestController
public class OrderController {
@Resource
OrderService orderService;
@GetMapping("/consumer/payment/get/{id}") //消费接口
public CommonResult<Payment> get(@PathVariable int id){
return orderService.get(id);// 直接调用service即可,里面的细节消费端不需要知道
}
}
超时控制
客户端默认设置的等待时间是1s,当处理比较复杂的业务,处理时间可能大于1s时,需要手动在yml文件中添加配置超时时间
# 这里没有提示,但依然会生效
ribbon:
ReadTimeout: 3000
ConnectTimout: 1000
日志增强
feign 自带日志
开启方式
- 首先配置,注入bean
- 在yml配置属性