SpringCloud OpenFeign使用和配置,Java OpenFeign 使用教程
SpringCloud feign日志控制打印配置,SpringCloud feign超时配置
================================
©Copyright 蕃薯耀 2021-03-11
http://fanshuyao.iteye.com/
一、SpringCloud OpenFeign概述
spring-cloud-openfeign
声明式REST客户端:Feign创建一个用JAX-RS或Spring MVC批注装饰的接口的动态实现。
官方文档:
https://docs.spring.io/spring-cloud-openfeign/docs/2.2.7.RELEASE/reference/html/
Feign是声明性的Web服务客户端。 它使编写Web服务客户端更加容易。 要使用Feign,请创建一个接口并对其进行注释。 它具有可插入注释支持,包括Feign注释和JAX-RS注释。 Feign还支持可插拔编码器和解码器。 Spring Cloud添加了对Spring MVC注释的支持,并支持使用Spring Web中默认使用的相同HttpMessageConverters。 Spring Cloud集成了Ribbon和Eureka以及Spring Cloud LoadBalancer,以在使用Feign时提供负载平衡的http客户端。
Java版本相容性
Feign 10.x及更高版本基于Java 8构建,并且应可在Java 9、10和11上运行。对于那些需要JDK 6兼容性的应用程序,请使用Feign9.x。
Feign集成了Ribbon,默认是轮询的负载均衡。
二、SpringCloud OpenFeign使用和配置
使用Eureka实现服务注册和和发现
1、pom.xml引入依赖:spring-cloud-starter-openfeign
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.7.RELEASE</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.4</version> </dependency>
2、application.properties配置
server.port=8645 spring.application.name=SPRINGCLOUD-EUREKA-FEIGN-WEB #eureka服务端的实例名称 eureka.instance.hostname=eureka8701.com #eureka实例名称 eureka.instance.instance-id=eureka-feign-8645 #路径显示IP地址 eureka.instance.prefer-ip-address=true #eureka客户端向服务端发送心跳的时间间隔,单元为秒,默认为30秒 eureka.instance.lease-renewal-interval-in-seconds=2 #eureka服务端收到最后一次心跳等待的时间上限,超时将移除服务,单元为秒,默认为90秒 eureka.instance.lease-expiration-duration-in-seconds=5 #false表示向注册中心注册自己 eureka.client.register-with-eureka=false #是否从Eureka抓取已有的注册信息,默认为true。单节点不用设置,集群必须设置为true,才能配置ribbon使用负载均衡 eureka.client.fetch-registry=true #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址 eureka.client.service-url.defaultZone=http://eureka8501.com:8501/eureka
3、启动类
@EnableFeignClients申明该项目是Feign客户端,不然声明式接口服务注入失败
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication //@EnableFeignClients申明该项目是Feign客户端,不然声明式接口服务注入失败 @EnableFeignClients public class SpringCloudFeignWeb8645Application { public static void main(String[] args) { SpringApplication.run(SpringCloudFeignWeb8645Application.class, args); } }
4、接口服务类
@FeignClient:name和value是一个东西,值为Eureka注册的服务名称
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import com.lqy.springCloud.feign.web.controller.Result; /** * OpenFeign是一种声明式、接口化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。 * */ //不要@Component也可以 //@FeignClient:name和value是一个东西,值为Eureka注册的服务名称 @FeignClient(value="SPRINGCLOUD-EUREKA-SERVER") public interface OpenFeignService { @RequestMapping("/test") public Result test(); @RequestMapping("/timeout") public Result timeout(); }
5、Controller请求类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.lqy.springCloud.feign.web.service.OpenFeignService; @RestController @RequestMapping("/web") public class FeignController { @Autowired private OpenFeignService openFeignService; @RequestMapping(value="/get", produces = MediaType.APPLICATION_JSON_VALUE) public Result get() { Result result = openFeignService.test(); return result; } /** * * @return */ @RequestMapping(value="/timeout", produces = MediaType.APPLICATION_JSON_VALUE) public Result timeout() { Result result = openFeignService.timeout(); return result; } }
6、feign超时配置(application.properties配置)
启用Hystrix后,其超时配置默认为1000毫秒。 因此,它可能发生在我们之前配置的客户端超时之前。 增加此超时将阻止它的发生。
#feign客户端建立连接最大的时间 feign.client.config.default.read-timeout=3000 #建立连接后,从服务器获取响应结果的最大时间 feign.client.config.default.connect-timeout=3000
openFeign超时报错:
This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Mar 10 17:10:19 CST 2021 There was an unexpected error (type=Internal Server Error, status=500). Read timed out executing GET http://SPRINGCLOUD-EUREKA-SERVER/timeout feign.RetryableException: Read timed out executing GET http://SPRINGCLOUD-EUREKA-SERVER/timeout at feign.FeignException.errorExecuting(FeignException.java:249) at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:129) at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
7、feign日志控制打印配置(application.properties配置)
格式:logging.level.完整的类路径:DEBUG
#打印日志的4个级别:none、basic、headers、full feign.client.config.default.logger-level=full #设置要日志打印的接口类 logging.level.com.lqy.springCloud.feign.web.service.OpenFeignService: DEBUG
打印的日志详细:
2021-03-10 17:03:12.532 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] ---> GET http://SPRINGCLOUD-EUREKA-SERVER/test HTTP/1.1 2021-03-10 17:03:12.533 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] ---> END HTTP (0-byte body) 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] <--- HTTP/1.1 200 (4ms) 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] connection: keep-alive 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] content-type: application/json 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] date: Wed, 10 Mar 2021 09:03:12 GMT 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] keep-alive: timeout=60 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] transfer-encoding: chunked 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] 2021-03-10 17:03:12.538 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] {"result":true,"timestamp":"2021-03-10 17:03:12","msg":"操作成功。","datas":"端口:8601"} 2021-03-10 17:03:12.539 DEBUG 6428 --- [nio-8645-exec-3] c.l.s.f.web.service.OpenFeignService : [OpenFeignService#test] <--- END HTTP (97-byte body)
8、feign请求启用请求或响应GZIP压缩(application.properties配置)
#您可以考虑为您的Feign请求启用请求或响应GZIP压缩。通过启用以下属性之一来做到这一点: feign.compression.request.enabled=true feign.compression.response.enabled=true
(如果文章对您有所帮助,欢迎捐赠,^_^)
================================
©Copyright 蕃薯耀 2021-03-11
http://fanshuyao.iteye.com/
相关推荐
标签:cloud、spring、openfeign、core、springframework、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变...
标签:cloud、spring、openfeign、core、springframework、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持...
所以有些公司开发使用的就是早期的 Spring Cloud,而在后期公司想进行版本升级的时候,改成使用Spring Cloud OpenFeign,但 Feign 使用的是他自己独有的那一套注解,如果我们想全部进行修改成 Spring MVC注解的话,...
标签:springframework、cloud、spring、openfeign、core、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变...
Springcloud + openfeign+mybatisplus+swagger+msql使用eureka注册中心 Eureka由两部分组成,服务端和客户端,服务端是注册中心,用来接收其他服务的注册,客户端是java客户端,用开注册,并实现负载均衡,其中...
SpringCloud尚硅谷-OpenFeign服务接口调用及代码
手把手教你集成spring cloud + shiro微服务框架;用最少的工作量,改造基于shiro安全框架的微服务项目,实现spring cloud + shiro 框架集成。博客地址:...
分布式事务组件seata的使用demo,AT模式、TCC模式,集成springboot、springcloud(nacos注册中心、openFeign服务调用、Ribbon负载均衡器)、spring jpa,数据库采用mysql demo中使用的相关版本号,具体请看代码。...
使用openfeign的继承特性,发现了一个问题。以本Demo的接口为例: com.example.controller.InheritProviderController.inherit()方法上没有继承到@GetMapping ,但是它为什么还能作为接口被访问?其实这是SpringMVC...
作为Spring Cloud的子项目之一,Spring Cloud OpenFeign以将OpenFeign集成到Spring Boot应用中的方式,为微服务架构下服务之间的调用提供了解决方案。首先,利用了OpenFeign的声明式方式定义Web服务客户端;其次还...
feign是一个客户端声明式http远程调用工具,提供了接口和注解方式进行调用 ,推荐使用feign继承体系方式进行内部RPC调用。
以电商项目为线索,快速掌握 JDK17 + springboot3 + springcloud Alibaba 专栏源码。 2 技术选型 JDK17 持久层: MyBatis-Plus 数据库: MySQL5.7 其他: SpringCloud Alibaba 技术栈 服务注册与发现:Nacos ...
基于SpringCloud构建的微服务 基础版本 组件说明: 组件名称 版本 描述 SpringBoot 2.1.4 基础框架 Spring Cloud Greenwich.SR1 基础框架 Spring Cloud Gateway - 网关 Spring Cloud Eureka - 注册中心 Spring Cloud...
6 - 服务配置中心:SpringCloud Config 7 - 服务消息驱动:SpringCloud Stream 8 - 服务链路跟踪:SpringCloud Sleuth 概念 Spring Cloud是⼀系列框架的有序集合。 它利⽤ Spring Boot 的开发便利性巧妙地简化了...
配合博客https://blog.csdn.net/qq_42017523/article/details/121519054使用
标签:cloud、spring、openfeign、core、springframework、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持...
服务调用:Spring Cloud OpenFeign 文档管理:Swagger2 服务监控:Spring Boot Admin 日志管理:ES + Kibana、Zipkin ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载...
轻松阅读是一款图书阅读类 APP,基于 Spring Cloud 开发的微服务实战项目,涉及 SpringCloud-Gateway、Nacos、Hystrix、OpenFeign、Jwt、ElasticSearch 等技术栈的应用。 轻松阅读是一款图书阅读类 APP,基于 Spring...
其对于Spring Cloud的最常用的注册中心、服务提供者及消费者、服务调用OpenFeign、Hystrix监控、服务网关gateway、消息驱动的微服务Spring Cloud Stream、分布式集群及分布式配置中心等都使用案例进行了说明,使得...
围棋java源码eclipse 该项目通过自动配置和绑定到 Spring Environment 和其他 Spring 编程模型习语,为 Spring Boot 应用程序提供 OpenFeign 集成。 一、特点 声明式 REST 客户端:Feign 创建了用 JAX-RS 或 Spring ...