day07-OpenFeign-服务调用

打印 上一主题 下一主题

主题 956|帖子 956|积分 2868

SpringCloud OpenFeign-服务调用

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的区别


  • Feign

    • Feign是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调服务,而是定义一个接口,在这个接口中标注一个注解即可完成服务调用,这样更加符合面向接口编程的宗旨,简化了开发。

  • OpenFeign

    • OpenFeign是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
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>
复制代码
(3)创建application.yml
  1. server:
  2.   port: 80
  3. spring:
  4.   application:
  5.     name: e-commerce-consumer-openfeign-80
  6. eureka:
  7.   client:
  8.     register-with-eureka: true #将自己注册到EurekaServer
  9.     fetch-registry: true
  10.     service-url:
  11.       #将自己注册都哪个EurekaServer
  12.       defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka
复制代码
(4)创建主启动类
  1. package com.li.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. /**
  7. * @author 李
  8. * @version 1.0
  9. */
  10. @SpringBootApplication
  11. @EnableEurekaClient
  12. @EnableFeignClients//启动OpenFeignClient
  13. public class MemberConsumerOpenfeignApplication {
  14.     public static void main(String[] args) {
  15.         SpringApplication
  16.                 .run(MemberConsumerOpenfeignApplication.class,args);
  17.     }
  18. }
复制代码
(5)创建接口,该接口最终是由OpenFeign来实现的(这里是OpenFeign的核心

  • @FeignClient(value = "MEMBER-SERVICE-PROVIDER") 指定远程调用的地址别名
  • 注意这里的 @GetMapping("/member/get/{id}")  指定要调用服务方的哪个方法,路径要和服务方的路径匹配。这是OpenFeign支持的SpringMVC的注解
  1. package com.li.springcloud.service;
  2. import com.li.springcloud.entity.Member;
  3. import com.li.springcloud.utils.Result;
  4. import org.springframework.cloud.openfeign.FeignClient;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. /**
  9. * @author 李
  10. * @version 1.0
  11. */
  12. @Component
  13. //MEMBER-SERVICE-PROVIDER 是服务提供方[集群]注册到EurekaServer的别名
  14. //根据这个key,可以在EurekaServer提供的注册信息中找到对应value,即真正的服务方地址:http://ip+port
  15. @FeignClient(value = "MEMBER-SERVICE-PROVIDER")
  16. public interface MemberFeignService {
  17.     //定义方法-远程调用的接口
  18.     /**
  19.      * 1.远程调用的方式是get
  20.      * 2.远程调用的url http://ip+port/member/get/{id}
  21.      * 3.MEMBER-SERVICE-PROVIDER 是服务提供方[集群]注册到EurekaServer的别名
  22.      * 4.OpenFeign会根据负载均衡来决定要掉用服务提供方的哪个节点(默认是轮询)
  23.      * 5.OpenFeign的好处是支持了SpringMVC注解+使用接口解耦
  24.      * @param id
  25.      * @return
  26.      */
  27.     @GetMapping("/member/get/{id}")
  28.     public Result<Member> getMemberById(@PathVariable("id") Integer id);
  29. }
复制代码
(6)创建MemberConsumerFeignController.java
这里的@GetMapping("/member/consumer/openfeign/get/{id}"),是消费方给浏览器的接口。
  1. package com.li.springcloud.controller;
  2. import com.li.springcloud.service.MemberFeignService;
  3. import com.li.springcloud.utils.Result;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import javax.annotation.Resource;
  8. /**
  9. * @author 李
  10. * @version 1.0
  11. */
  12. @RestController
  13. public class MemberConsumerFeignController {
  14.     //装配MemberFeignService,
  15.     //使用时该接口会对应一个代理对象,通过代理对象可以该接口的方法
  16.     @Resource
  17.     private MemberFeignService memberFeignService;
  18.     @GetMapping("/member/consumer/openfeign/get/{id}")
  19.     public Result getMemberById(@PathVariable("id") Integer id) {
  20.         return memberFeignService.getMemberById(id);
  21.     }
  22. }
复制代码
(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
  1. package com.li.springcloud.config;
  2. import feign.Logger;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. /**
  6. * @author 李
  7. * @version 1.0
  8. */
  9. @Configuration
  10. public class OpenFeignConfig {
  11.     @Bean
  12.     public Logger.Level loggerLevel() {
  13.         return Logger.Level.FULL;
  14.     }
  15. }
复制代码
(2)修改application.yml
  1. logging:
  2.   level:
  3.     #对MemberFeignService接口调用过程打印信息-Debug
  4.     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秒没有响应,报请求超时
  1. #全局配置
  2. ribbon:
  3.   # 设置feign客户端超时时间(OpenFeign默认支持ribbon),单位ms,默认超时时间为1s
  4.   ReadTimeout: 8000
  5.   #两端连接所用时间
  6.   ConnectionTimeout: 8000
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表