本文主要是介绍Spring Cloud OpenFeign笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.OpenFeign简介
Feign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用Feign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
2.pom.xml依赖
<!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
3.主启动类上增加注解@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80{public static void main(String[] args) {SpringApplication.run(OrderFeignMain80.class,args);}
}
4.创建服务提供方对应的服务的接口类
@FeignClient(value = “CLOUD-PAYMENT-SERVICE”)中的value值对应的就是服务提供方注册到eruka中的服务名称。
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {@GetMapping(value = "/payment/get/{id}")public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);@GetMapping(value = "/payment/timeout/server")public String getTimeOutServicePort();
}
5.创建controller
@RestController
@Slf4j
public class OrderController {@Resourceprivate PaymentFeignService paymentService;@GetMapping(value = "/consumer/payment/get/{id}")public CommonResult<Payment> query(@PathVariable("id") Long id){return paymentService.getPaymentById(id);}@GetMapping(value = "/consumer/payment/timeout/server")public String getTimeOutServicePort(){return paymentService.getTimeOutServicePort();}
}
6.OpenFeign的超时设置
举个例子:当用户下单时,需要从订单服务中调用支付服务,而支付服务处理请求的时间为3秒,订单服务就会报超时异常。
原因是整合了ribbon,而ribbon中处理请求的默认时长是1秒钟。
调整超时设置配置如下:
ribbon:ReadTimeout: 5000ConnenctTimeout: 5000
还可以通过openFeign修改超时设置
feign:client:config:default:connectTimeout: 10000 #Feign的连接建立超时时间,默认为10秒readTimeout: 60000 #Feign的请求处理超时时间,默认为60
7.修改日志级别
@Configuration
public class FeignConfig {@BeanLogger.Level feignLoggerLevel(){return Logger.Level.FULL;}
}
applicaiton.yml中可以指定各个接口输出什么级别日志。
logging:level:#feign日志以什么级别监控哪个接口com.springcloud.service.PaymentFeignService: debug
如果是按以上的配置,控制台将能看到完整的请求信息。
2020-11-11 22:47:09.346 INFO 62988 --- [p-nio-81-exec-1] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client CLOUD-PAYMENT-SERVICE initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=CLOUD-PAYMENT-SERVICE,current list of Servers=[100.119.202.39:8002, 100.119.202.39:8001],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:2; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:100.119.202.39:8001; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
, [Server:100.119.202.39:8002; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@24aed60f
2020-11-11 22:47:10.346 INFO 62988 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: CLOUD-PAYMENT-SERVICE.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2020-11-11 22:47:11.352 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] <--- HTTP/1.1 200 (2037ms)
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] connection: keep-alive
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] content-length: 4
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] content-type: text/plain;charset=UTF-8
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] date: Wed, 11 Nov 2020 14:47:11 GMT
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] keep-alive: timeout=60
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort]
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] 8002
2020-11-11 22:47:11.353 DEBUG 62988 --- [p-nio-81-exec-1] c.s.service.PaymentFeignService : [PaymentFeignService#getTimeOutServicePort] <--- END HTTP (4-byte body)
这篇关于Spring Cloud OpenFeign笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!