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、后台查看日志

相关内容

热门资讯

缅北白家致中国公民6人死亡 【#缅北白家致中国公民6人死亡#】#缅北白家涉赌资金超180亿元#公安部部署开展打击缅北涉我犯罪专项...
2025年GEO排名优化公司榜... 数字化浪潮席卷全球的当下,AI搜索已然成为企业提升品牌影响力、抢占市场先机的战略要地。当用户在Dee...
大项目、大订单陆续交付 博隆技... 7月11日,博隆技术发布业绩预告。经公司财务部初步测算,2025年上半年,公司预计实现归属于母公司所...
如何理解特定股票代码开头的含义... 在股票投资领域,理解特定股票代码开头的含义至关重要,它不仅是识别股票的关键标识,还能为股票分析提供有...
东方财富成交额逾百亿! 格隆汇7月11日|东方财富涨超4%,成交额逾100亿元。
港股异动丨药明康德业绩亮眼,刺... 格隆汇7月11日|港股市场CRO概念股集体走强,其中,凯莱英涨超15%,药明康德涨12%,昭衍新药、...
【光明论坛】奋力书写挺膺担当的... 来源:国际在线近日,中华全国青年联合会第十四届委员会全体会议、中华全国学生联合会第二十八次代表大会在...
盛天网络投资成立互娱科技公司 人民财讯7月11日电,企查查APP显示,近日,武汉市带带互娱科技有限公司成立,法定代表人为杨影,注册...
金融科技板块活跃 安硕信息涨幅...   07月11日消息,截止10:45,金融科技板块活跃,瑞达期货涨停,安硕信息、恒宝股份(维权)、国...
石文先委员:构建符合时代要求的... 转自:人民政协报企业是国家经济核心构成和根本支撑。日前,中共中央办公厅、国务院办公厅发布了《关于完善...