【SpringCloud之OpenFeign】服务调用学习笔记+使用步骤

一.OpenFeign简介

OpenFeign为微服务架构下服务之间的调用提供了解决方案,OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。

openfeign的实现原理:

基于@EnableFeignClients 将所有被@FeignClient注解的类 注册到容器中。当这些被@FeignClient注解的类被调用时会创建一个动态代理的对象为我们创建被调用类的实例,然后都会被统一转发给 Feign 框架所定义的一个 InvocationHandler , 由该 Handler 完成后续的 HTTP 转换, 发送, 接收, 翻译HTTP响应的工作。

二.使用

①导入依赖

 

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

②主启动类标记注解 @EnableFeignClients (例子:消费者启动类上放)

③创建接口,使用注解 @FeignClient(微服务实例名)

例子:

@Component
@FeignClient("SERVICE-PROVIDER")
public interface ProductFeignService {
    /**
     * 编写技巧,直接把服务提供者那一端的控制器方法的两行拷贝过来,最后加一个分号就可以了,别的什么也不用动
     */
    @RequestMapping("/provider/product/findAll")
    public List<Product> findAll();

    @RequestMapping("/provider/product/findByPid")
    public String findByPid(@RequestParam("pid") Integer pid);

④调用接口

使用@Autowired注入上面注入的接口,进行使用

例子:

@RestController
public class ProductController {
    @Autowired
    private ProductFeignService productFeignService;

    @RequestMapping("/consumer/product/findAll")
    public List<Product> findAll() {
        return productFeignService.findAll();
    }

    @RequestMapping("/consumer/product/findByPid")
    public String findByPid(@RequestParam("pid") Integer pid) {
        return productFeignService.findByPid(pid);
    }
}

⑤设置超时时间

默认Feign客户端只等待一秒,如果服务端处理时间超过一秒,则报错。

设置超时时间可以通过yml修改

ribbon:
  ReadTimeout:  5000
  ConnectTimeout: 5000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud是一个为开发者提供了快速构建分布式系统的工具集,其中非常重要的一部分就是OpenFeignOpenFeign是一个声明式、模板化的HTTP客户端,它可以让开发者更加方便的调用HTTP接口。下面我们来详细了解一下Spring Cloud整合OpenFeign使用方式。 首先,我们需要在pom.xml文件中添加依赖,如下所示: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>{版本号}</version> </dependency> ``` 然后,我们需要在启动类上添加@EnableFeignClients注解,表示开启Feign客户端自动配置。同时,我们还需要通过@FeignClient注解来定义接口。例如: ``` @FeignClient(name = "user-service") public interface UserFeignClient { @GetMapping("/user/findById") User findById(@RequestParam("id") Long id); } ``` 在上面的代码中,@FeignClient注解中的name属性表示调用服务名,而接口中的findById方法就是定义的远程调用的接口。其中,@RequestParam注解表示使用@RequestParam方式传参。 最后,我们在业务代码中使用定义的接口即可。例如: ``` @RestController public class UserController { @Autowired private UserFeignClient userFeignClient; @GetMapping("/findUser") public User findUser(Long id) { return userFeignClient.findById(id); } } ``` 通过以上步骤,我们就可以方便地使用OpenFeign调用HTTP接口,实现微服务之间的远程调用。整合OpenFeign有很多细节需要注意,例如如何处理调用异常、如何配置重试等等。但总体来说,Spring Cloud整合OpenFeign使用起来非常简单,是我们构建分布式系统的重要利器之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九洲带鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值