网站首页 > 教程文章 正文
1.OpenFeign介绍
https://github.com/spring-cloud/spring-cloud-openfeign
- OpenFeign是一个声明式WebService客户端,使用OpenFeign让编写Web Service客户端更加简单
- 它的使用方法是定义一个服务端口然后在上面添加注解
- OpenFeign也支持可插拔式的编码器和解码器
- SpringCloud对OpenFeign进行了封装使其支持SpringMVC标准注解和HttpMessageConverters消息转换器
- OpenFeign可以与Eureka和Ribbon组合使用以支持负载均衡
2.OpenFeign和Feign的区别
- FeignFeign是SpringCloud组件中的一个轻量级RESTful的Http服务客户端Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务Feign的使用方法是:使用Feign的注解定义接口,调用服务注册中心的服务Feign支持的注解和用法请参考官方文档:OpenFeign/feign: Feign makes writing java http clients easier (github.com)Feign本身不支持SpringMVC注解,它有一套自己的注解Feign集成了Ribbon、RestTemplate实现了负载均衡的执行Http调用,只不过对原有的方式(Ribbon+RestTemplate)进行了封装,开发者不必手动使用RestTemplate调服务,而是定义一个接口,在这个接口中标注一个注解即可完成服务调用,这样更加符合面向接口编程的宗旨,简化了开发。
- OpenFeignOpenFeign是SpringCloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口OpenFeign通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务
- 一句话:OpenFeign就是在Feign的基础上做了加强
3.OpenFeign应用实例
需求分析:如下,将原来使用Ribbon+RestTemplate实现:获取服务+远程调用+负载均衡,替换为使用OpenFeign来实现
参考 member-service-consumer-80 创建 member-service-consumer-openfeign-80(步骤参考以前)
(1)创建新模块-member-service-consumer-openfeign-80
(2)修改 pom.xml:拷贝 member-service-consumer-80 的 pom.xml 依赖,并加入 openfeign-starter
<!-- 引入 openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
(3)创建application.yml
server: port: 80 spring: application: name: e-commerce-consumer-openfeign-80eureka: client: register-with-eureka: true #将自己注册到EurekaServer fetch-registry: true service-url: #将自己注册都哪个EurekaServer defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka
(4)创建主启动类
package com.li.springcloud; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author 李 * @version 1.0 */@SpringBootApplication@EnableEurekaClient@EnableFeignClients//启动OpenFeignClientpublic class MemberConsumerOpenfeignApplication { public static void main(String[] args) { SpringApplication .run(MemberConsumerOpenfeignApplication.class,args); }}
(5)创建接口,该接口最终是由OpenFeign来实现的(这里是OpenFeign的核心)
- @FeignClient(value = "MEMBER-SERVICE-PROVIDER") 指定远程调用的地址别名
- 注意这里的 @GetMapping("/member/get/{id}") 指定要调用服务方的哪个方法,路径要和服务方的路径匹配。这是OpenFeign支持的SpringMVC的注解
package com.li.springcloud.service; import com.li.springcloud.entity.Member;import com.li.springcloud.utils.Result;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.stereotype.Component;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable; /** * @author 李 * @version 1.0 */@Component//MEMBER-SERVICE-PROVIDER 是服务提供方[集群]注册到EurekaServer的别名//根据这个key,可以在EurekaServer提供的注册信息中找到对应value,即真正的服务方地址:http://ip+port@FeignClient(value = "MEMBER-SERVICE-PROVIDER")public interface MemberFeignService { //定义方法-远程调用的接口 /** * 1.远程调用的方式是get * 2.远程调用的url http://ip+port/member/get/{id} * 3.MEMBER-SERVICE-PROVIDER 是服务提供方[集群]注册到EurekaServer的别名 * 4.OpenFeign会根据负载均衡来决定要掉用服务提供方的哪个节点(默认是轮询) * 5.OpenFeign的好处是支持了SpringMVC注解+使用接口解耦 * @param id * @return */ @GetMapping("/member/get/{id}") public Result<Member> getMemberById(@PathVariable("id") Integer id);}
(6)创建MemberConsumerFeignController.java
这里的@GetMapping("/member/consumer/openfeign/get/{id}"),是消费方给浏览器的接口。
package com.li.springcloud.controller; import com.li.springcloud.service.MemberFeignService;import com.li.springcloud.utils.Result;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author 李 * @version 1.0 */@RestControllerpublic class MemberConsumerFeignController { //装配MemberFeignService, //使用时该接口会对应一个代理对象,通过代理对象可以该接口的方法 @Resource private MemberFeignService memberFeignService; @GetMapping("/member/consumer/openfeign/get/{id}") public Result getMemberById(@PathVariable("id") Integer id) { return memberFeignService.getMemberById(id); }}
(7)启动EurekaServer,服务消费方,启动本模块主程序,在浏览器中访问:http://localhost:80/member/consumer/openfeign/get/5,可以看到查询数据成功,并且多次刷新会发现调用的接口是轮询的。
注意事项和使用细节
- OpenFeign使用特点是 微服务调用接口+@FeignClient,使用接口进行解耦
- 接口中的@FeignClient(value = "MEMBER-SERVICE-PROVIDER"),这里的MEMBER-SERVICE-PROVIDER就是Eureka Server的服务提供方注册的别名,底层会通过这个别名(key)找到真正的地址(value)
- 接口中的方法,value是不能乱写的,要根据服务消费方的url一致,否则无法访问到服务消费方对应的方法
4.OpenFeign的日志配置
4.1基本介绍
- Feign提供了日志打印功能,可以通过配置来调整日志级别,从而对Feign接口的调用情况进行监控和输出
- 日志级别
- NONE:默认的,不显示任何日志
- BASIC:仅记录请求方法、URL、响应状态码和执行时间
- HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息
- FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据
4.2日志配置-应用实例
(1)在member-service-consumer-80创建OpenFeignConfig.java
package com.li.springcloud.config; import feign.Logger;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; /** * @author 李 * @version 1.0 */@Configurationpublic class OpenFeignConfig { @Bean public Logger.Level loggerLevel() { return Logger.Level.FULL; }}
(2)修改application.yml
logging: level: #对MemberFeignService接口调用过程打印信息-Debug com.li.springcloud.service.MemberFeignService: debug
常见的日志级别有 5 种,分别是 error、warn、info、debug、trace
error:错误日志,指比较严重的错误,对正常业务有影响,需要运维配置监控的;
warn:警告日志,一般的错误,对业务影响不大,但是需要开发关注;
info:信息日志,记录排查问题的关键信息,如调用时间、出参入参等等;
debug:用于开发 DEBUG 的,关键逻辑里面的运行时数据;
trace:最详细的信息,一般这些信息只记录到日志文件中。
(3)重启模块,浏览器访问消费模块,后台输出如下:
5.OpenFeign超时时间配置
OpenFeign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那么会相应的报错。
而实际会因为业务的不同出现超出1秒的情况,这时我们需要调整超时时间:
https://cloud.tencent.com/developer/article/1444369
Feign 的负载均衡底层用的就是 Ribbon。在application.yml中添加如下配置,超过8秒没连接上报连接超时,如果超过8秒没有响应,报请求超时
#全局配置ribbon: # 设置feign客户端超时时间(OpenFeign默认支持ribbon),单位ms,默认超时时间为1s ReadTimeout: 8000 #两端连接所用时间 ConnectionTimeout: 8000
猜你喜欢
- 2025-04-29 Jmeter 接口测试
- 2025-04-29 Spring IoC Container 原理解析
- 2025-04-29 开发JAX-RPC Web Services for WebSphere(下)
- 2025-04-29 Spring Cloud入门看这一篇就够了
- 2025-04-29 国内四大主流报表工具深度对比之多样性数据源
- 2025-04-29 常见的序列化框架及Protobuf原理
- 2025-04-29 RPC、Web Service等几种远程监控通信方式对比
- 2025-04-29 翔云身份证号实名认证接口:让身份核验变得简单与高效
- 2025-04-29 《github精选系列》——SpringBoot 全家桶
- 2025-04-29 Arthas线上服务器问题排查
- 最近发表
-
- 一个可以用来练手的C++开源编译器!
- Linux开发工具使用指南(linux软件开发工具)
- Linux下Makefile文件的模式规则和自动化变量
- 程序员的副业秘籍!一款可以快速搭建各类系统的后台管理系统
- postgresql自定义函数实现,通过contrib模块进行扩展
- Linux GCC编译及Makefile使用(gcc makefile编写)
- wordpress独立站上线两周没收录?原来是robots.txt搞的鬼…
- make sure用法解析(make sure sth)
- 每天一个 Python 库:Django全能Web框架,一站式后台开发
- Makefile实践(makefile经典教程)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)