社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
Feign和OpenFeign的主要区别如下图
那我们怎么使用OpenFeign呢?
pom.xml
<!-- OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
PaymentService .java
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
OrderFeignController .java
@RestController
@Slf4j
public class OrderFeignController {
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/feign/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
@GetMapping("/consumer/feign/payment/timeout")
public String getPaymentById() {
// openfeign-ribbon,客户端默认等待1秒钟
return paymentFeignService.paymentFeignTimeout();
}
}
application.yml
server:
port: 80
spring:
application:
name: cloud-order-feign-service
eureka:
client:
# true表示将自己注册进Eureka Server,默认为true
register-with-eureka: true
# 是否从Eureka Server抓取以有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合Ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
application.yml
# 设置feign客户端超时时间(OpenFeign默认支持Ribbon)
ribbon:
# 指的是建立连接后从服务器读取到可用资源所用时间
ReadTimeout: 5000
# 指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ConnectTimeout: 5000
么配置上面的yml文件时,如果我们调用如下的方法
会报错
@GetMapping("/feign/timeout")
public String paymentFeignTimeout() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
return serverPort;
}
配置了,就会出现以下结果
怎么使用日志?
第一步:FeignConfig.java
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
第二步:application.yml
logging:
level:
# feign日志已什么级别监控哪个接口
com.atguigu.springcloud.service.PaymentFeignService: debug
第三步:测试该接口下的方法,得到如下日志
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!