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

相关内容

热门资讯

近4000万元!全国总工会部署... 全国各地目前已陆续进入高温季节,为切实维护广大职工健康权益,降低高温作业和高温天气作业对劳动者身体健...
捷安高科涨2.01%,成交额2... 7月3日,捷安高科盘中上涨2.01%,截至10:32,报11.16元/股,成交2632.86万元,换...
从制造、消费、运输、税务等多领... 央视网消息:工业和信息化部数据显示,2025年前5个月,我国规模以上电子信息制造业增加值同比增长11...
浙江让快递小哥从过客变主人 【#浙江让快递小哥从过客变主人#】近日,浙江。府山街道召开新就业群体协商议事会,聚焦外卖、快递小哥权...
李在明:应恢复朝韩间沟通 改善... 人民财讯7月3日电,韩国总统李在明当地时间7月3日举行上任首月记者会,就朝鲜半岛和平及对朝政策问题表...
蓝英装备跌2.02%,成交额6... 7月3日,蓝英装备盘中下跌2.02%,截至10:36,报25.72元/股,成交6.59亿元,换手率8...
打好政策组合拳 精准发力稳就业 来源:内蒙古日报 “人社政策服务包,让我清晰知晓人才补贴、用工保障等政策,有效提升企业政策运用能力,...
神宇股份涨2.00%,成交额1... 7月3日,神宇股份盘中上涨2.00%,截至10:37,报39.19元/股,成交1.17亿元,换手率2...
冠盛股份:半固态电池量产线预计... 格隆汇7月3日|冠盛股份在6月的调研中表示,吉林省东驰新能源科技有限公司作为公司的研发合作平台,目前...
启迪药业涨2.07%,成交额5... 7月3日,启迪药业盘中上涨2.07%,截至10:39,报11.32元/股,成交5822.69万元,换...