IT评测·应用市场-qidao123.com

标题: day07-OpenFeign-服务调用 [打印本页]

作者: tsx81429    时间: 2023-4-11 19:11
标题: day07-OpenFeign-服务调用
SpringCloud OpenFeign-服务调用

1.OpenFeign介绍

https://github.com/spring-cloud/spring-cloud-openfeign
2.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的核心
  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,可以看到查询数据成功,并且多次刷新会发现调用的接口是轮询的。
注意事项和使用细节
4.OpenFeign的日志配置

4.1基本介绍

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
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4