微服务 、Spring Cloud 、OpenFeign的使用

这篇具有很好参考价值的文章主要介绍了微服务 、Spring Cloud 、OpenFeign的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

微服务 – Spring Cloud – OpenFeign的使用

OpenFeign 简介

OpenFeign 提供了一种声明式的远程调用接口

OpenFeign 能做什么

目的是为了简易HTTP客户端的编写。

之前在 笔记中介绍了 Ribbon + RestTemplate 的使用。Ribbon + RestTemplate 是多http请求做了封装处理,形成了模版化的调用。但是在实际的开发中,由于对服务依赖的调用可能不止一处,往往一个接口被多处调用,所以需要对每个微服务进行封装。鉴于此 Feign 在此基础上为我们提供了封装操作,由Feign 帮我门定义和实现依赖服务接口的定义。因此简化了我们的操作,只需要创建一个接口并使用注解的形式来配置它(比如Mapper接口上标注@Mapper注解,现在是在一个微服务接口上标注一个@FeignClient注解),就可以完成对服务接口的绑定,简化了Spring cloud Ribbon使用时候封装客户端的开发量

如何使用

Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations

在主启动类上通过注解 @EnableFeignClients、 接口上通过注解 @FeignClient 实现

@SpringBootApplication
@EnableFeignClients
public class WebApplication {

	public static void main(String[] args) {
		SpringApplication.run(WebApplication.class, args);
	}

	@FeignClient("name")
	static interface NameService {
		@RequestMapping("/")
		public String getName();
	}
}

OpenFeign 使用Example

  • eureka服务集群 127.0.0.1:7001 127.0.0.1:7002
  • 微服务 - 消费者服务 consumner-order, 服务注册 eureka
  • 微服务 - 支付服务 payment8001, payment8002, 服务注册进eureka。服务名叫 CLOUD-PAYMENT-SERVICE

1、新建子模块 consumner-order

依赖pom.xml

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、配置文件application.yaml’

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

3、主启动类

@EnableFeignClients 开启 Feign 客户端

@SpringBootApplication
@EnableFeignClients
public class FeignMain80 {

    public static void main(String[] args) {
        SpringApplication.run(FeignClient.class, args);
    }

}

4、接口层 service/PaymentFeignService.java

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    @GetMapping(value = "/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);

}

5、业务控制器 controller/OrderController.java

@RestController
public class OrderController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> paymentLb(@PathVariable("id") Long id) {
         return paymentFeignService.getPaymentById(id);
    }

}

6、启动eureka7001、eureka7002、payment8001、payment8002、consumer-order

访问 127.0.0.1:80//consumer/payment/get/4

# 第一次

{"code":200,"message":"查询成功, serverPort: 8001","data":{"id":4,"serial":"jeffcail00004"}}

# 第二次

{"code":200,"message":"查询成功, serverPort: 8002","data":{"id":4,"serial":"jeffcail00004"}}

# 第三次

{"code":200,"message":"查询成功, serverPort: 8001","data":{"id":4,"serial":"jeffcail00004"}}

# 第四次

{"code":200,"message":"查询成功, serverPort: 8002","data":{"id":4,"serial":"jeffcail00004"}}

OpenFeign 超时控制 (OpenFeign默认超时时间为1秒)

在payment8001、payment8002中新增接口

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

在order 服务中新增调用接口

service/PaymentFeignService.jva

@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeout();

业务控制器 controller/OrderController.java

@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout() {
  return paymentFeignService.paymentFeignTimeout();
}

访问: http://localhost/consumer/payment/feign/timeout

超时错误

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jun 22 13:39:17 CST 2023
There was an unexpected error (type=Internal Server Error, status=500).
Read timed out executing GET http://CLOUD-PAYMENT-SERVICE/payment/feign/timeout

YAML文件中开启超时配置

# 设置feigin 客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
  # 读超时时间 5秒
  ReadTimeout: 5000
  # 建立连接 超时时间 5秒
  ConnectTimeout: 5000

访问: http://localhost/consumer/payment/feign/timeout

等待3秒后文章来源地址https://www.toymoban.com/news/detail-496331.html

8001

到了这里,关于微服务 、Spring Cloud 、OpenFeign的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【springcloud微服务】Spring Cloud Alibaba 整合dubbo与openfeign

    dubbo与springcloud都可以单独作为微服务治理框架在生产中进行使用,但使用过springcloud的同学大概了解到,springcloud生态的相关组件这些年已经逐步停更,这就导致在服务架构演进过程中的迭代断层,以至于一些新的技术组件引入困难重重,于是在国内的市场上就有了升级版的

    2024年02月07日
    浏览(36)
  • Spring Cloud OpenFeign:基于Ribbon和Hystrix的声明式服务调用

    💗wei_shuo的个人主页 💫wei_shuo的学习社区 🌐Hello World ! Spring Cloud OpenFeign是一个声明式的服务调用框架,基于Feign并整合了Ribbon和Hystrix;目标是简化分布式系统中编写服务间调用的代码,并提供一种更加优雅和便捷的方式来进行服务之间的通信 依赖导入 application.yml配置 启

    2024年02月05日
    浏览(34)
  • Spring Cloud OpenFeign 的使用及踩坑指南

    Feign 和OpenFeign Feign OpenFeign openFeign的优势 OpenFeign应用 1. 导入依赖 2. 使用 3. 日志配置 4. 数据压缩 OpenFeign高级应用 OpenFeign熔断降级的两种方式-降级方法和降级工厂 踩坑指南 坑一:Http Client 坑二:全局超时时间 坑三:单服务设置超时时间 遇到的问题 1. 使用Spring MVC注解,但请

    2024年02月14日
    浏览(31)
  • Spring Cloud Alibaba【OpenFeign实现服务降级、Dubbo实现服务生产者、 Dubbo消费者调用接口 】(三)

    目录 服务调用_OpenFeign实现服务降级 服务调用_Dubbo实现服务生产者 

    2024年02月17日
    浏览(30)
  • Spring Cloud Alibaba 同时兼容dubbo与openfeign

    dubbo与springcloud都可以单独作为微服务治理框架在生产中进行使用,但使用过springcloud的同学大概了解到,springcloud生态的相关组件这些年已经逐步停更,这就导致在服务架构演进过程中的迭代断层,以至于一些新的技术组件引入困难重重,于是在国内的市场上就有了升级版的

    2024年02月07日
    浏览(24)
  • 【Spring Cloud Alibaba】(三)OpenFeign扩展点实战 + 源码详解

    【Spring Cloud Alibaba】(一)微服务介绍 及 Nacos注册中心实战 【Spring Cloud Alibaba】(二)微服务调用组件Feign原理+实战 书接上文,我们掌握了Feign的基本使用、核心原理,以及Spring Cloud Alibaba如何快速整合Feign,真的太简单了!你是不是觉得这样就够了?但在实际项目使用OpenF

    2024年02月20日
    浏览(29)
  • 《Spring Cloud学习笔记:Nacos配置管理 & OpenFeign & LoadBalancer & Getway》

    基于Feign的声明式远程调用(代码更优雅),用它来去代替我们之前的RestTemplate方式的远程调用 首先我们来看一下,微服务架构下关于配置文件的一些问题: 配置文件相对分散。在一个微服务架构下,配置文件会随着微服务的增多变的越来越多,而且分散在各个微服务中,不

    2024年02月04日
    浏览(32)
  • org.springframework.cloud:spring-cloud-starter-openfeign:jar is missing详解

    openfeign无法导入的问题 我感觉最近带的好几个新人在搭建springCloud基础框架的时候,会犯一个非常小的错误,导致进度卡住了。 这个错误就是Feign导入的错误: ‘dependencies.dependency.version’ for org.springframework.cloud:spring-cloud-starter-openfeign:jar is missing. 表面上看是jar包没有下载下

    2024年02月07日
    浏览(34)
  • 高版本Spring Cloud中OpenFeign整合Hystrix熔断降级不生效的问题及解决方案

    本文将介绍在使用高版本Spring Cloud(2020.0.x及以后),由于取消了Hystrix的集成,导致OpenFeign与Hystrix熔断降级不生效的问题。同时,也将给出解决该问题的方案,即添加feign.circuitbreaker.enabled=true配置。 随着Spring Cloud框架的发展,高版本Spring Cloud(2020.0.x及以上)中取消了对Hy

    2024年02月22日
    浏览(30)
  • 【合集】Spring Cloud 组件——架构进化史话 & Eureka,Nacos,OpenFeign,Ribbon,Sentinel,Gateway ,Seata+事务. . .

    Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式会话,集群状态)。 注意: 首先,尽管Spring Cloud带有“Cloud”这个单词,但它并不是云计算解

    2024年02月08日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包