springcloud整合OpenFeign
创始人
2024-04-23 11:34:55
0

OpenFeign是什么

OpenFeign是一个声明式web服务客户端,让web服务调用更容易,只需要在接口上进行注解配置即可

能干嘛

Feign对Ribbon进行了封装,使得接口和调用的路径进行绑定,以注解的形式配置即可

OpenFeign和Feign的区别

1、都内置了Ribbon
2、Feign是Netflix公司写的,OpenFeign是SpringCloud写的
3、OpenFeign支持SpringMvc注解,如@RequestMapping等

demo

1、建module
consumer-feign-order80
2、改pom


cloud2020com.atguigu.springcloud1.0-SNAPSHOT4.0.0cloud-consumer-feign-order80org.springframework.cloudspring-cloud-starter-openfeignorg.springframework.cloudspring-cloud-starter-netflix-eureka-clientcom.atguigu.springcloudcloud-api-commons${project.version}compileorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-actuatororg.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-testtest

3、配yml

server:port: 80#spring:
#  application:
#    name: cloud-order-serviceeureka:client:register-with-eureka: false # 不注册进eureka服务中心service-url:defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4、主启动

@SpringBootApplication
@EnableFeignClients // 启动feign
public class CloudConsumerFeignOrder80Application {public static void main(String[] args) {SpringApplication.run(CloudConsumerFeignOrder80Application.class, args);System.out.println("启动成功");}}

5、业务类

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {@GetMapping(value = "/payment/get/{id}")CommonResult getPaymentById(@PathVariable("id") Long id);@GetMapping(value = "/payment/feign/timeout")String paymentFeignTimeout();
}

6、controller

@RestController
@RequestMapping("consumer")
@Slf4j
public class OrderFeignController {@Resourceprivate PaymentFeignService paymentFeignService;@GetMapping(value = "/payment/get/{id}")public CommonResult getPaymentById(@PathVariable("id") Long id) {return paymentFeignService.getPaymentById(id);}@GetMapping(value = "/payment/feign/timeout")public String paymentFeignTimeout() {// OpenFeign客户端一般默认等待1秒钟return paymentFeignService.paymentFeignTimeout();}
}

7、测试
http://localhost/consumer/payment/get/31

超时控制

1、生产者故意sleep5s

    @GetMapping("feign/timeout")public String getFeignTimeOut() {try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}return serverPort;}

2、消费者service添加超时方法

    String paymentFeignTimeout();

3、消费者controller添加超时方法

    @GetMapping(value = "/payment/feign/timeout")public String paymentFeignTimeout() {// OpenFeign客户端一般默认等待1秒钟return paymentFeignService.paymentFeignTimeout();}

4、测试
http://localhost/consumer/payment/feign/timeout
5、OpenFeign默认等待一秒钟,超过后报错
6、yml设置超时控制

#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:#指的是建立连接所用的时间,适用于网络状况正常的情况下, 两端连接所用的时间ReadTimeout: 5000#指的是建立连接后从服务器读取到可用资源所用的时间ConnectTimeout: 5000

日志打印功能

1、是什么?
对feign的链路调用情况进行监控和输出
2、日志级别:NONE、BASIC、HEADERS、FULL
3、配置日志bean

@Configuration
public class FeignConfig {/*** NONE,* BASIC,* HEADERS,* FULL;* 

* 日志级别** @return*/@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;} }

4、yml配置

logging:level:# feign日志以 debug 级别监控 com.atguigu.springcloud.service.PaymentFeignService 接口com.atguigu.springcloud.service.PaymentFeignService: debug

5、后台查看日志

相关内容

热门资讯

阿索卡·塔诺为什么没有在星球大... 阿索卡·塔诺为什么没有在星球大战正传出现?阿索卡没有听说过,,是什么鬼阿索卡是阿纳金年轻时收的徒弟 ...
河北公布第二批美丽河湖名单,1... 转自:河北新闻网河北公布第二批美丽河湖名单,15个河湖入选全省美丽河湖总数达26个河北日报讯(记者马...
热血英豪灵异少女带幻影水晶 热血英豪灵异少女带幻影水晶我看见有人用灵异带幻影能幻影出灵异的ZXC,怎么弄的? 是不是他改了...
潍柴:自主锻造“国产引擎” 加... 历时10年,投入1.2亿元,接连突破甲醇发动机抗机油乳化技术、低温冷启动技术、控早燃爆震技术等多项行...
西班牙多地将出现强降雨等极端天... 来源:央视新闻客户端当地时间7月10日,西班牙国家气象局警告称,当地时间11日起,该国多个地区将出现...
来中国前,黄仁勋先去见了特朗普   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! 【文/观察者网 柳...
警惕虚假宣传诱导网络贷款的风险 转自:中国银行保险报网□本报记者 仇兆燕7月10日,金融监管总局金融消费者权益保护局发布风险提示,提...
比特币突破11.7万美元 续创... .ct_hqimg {margin: 10px 0;} .hqimg_wrapper {text-a...
翼年代记漫画最近章节都说什么了... 翼年代记漫画最近章节都说什么了?OVA《东京默示录》是漫画107话到135话的内容,漫画要从136话...
中国天瑞水泥遭Yu Kuo C... .ct_hqimg {margin: 10px 0;} .hqimg_wrapper {text-a...