Spring Cloud OpenFeign笔记

2024-06-14 22:18

本文主要是介绍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笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1061651

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.