OpenFeign远程调用详解【微服务篇】

目录

一 什么是openfeign

二 怎么使用

1.导入依赖

2.编写FeignClient接口

3.使用注解开启openfeign功能


一 什么是openfeign

在使用之前,我们需要先知道openfeign是什么?以及它的功能是什么?在哪里需要使用它。那么接下来我们先通过一张流程图来进行一个了解。

这是一个我们发送请求到controller,然后服务器接收我们的请求,但是因为我们使用的是微服务,也就是各个服务分开独立 。我的初始请求是请求到服务器1,但是现在我需要服务器2中的数据,那么我需要向服务器2 发送请求来获得我需要的数据。而两个服务器之间如何发送请求?这就是openfeign的功能。

二 怎么使用

那么了解了openfeign之后,我们就来看看如何使用它。

1.导入依赖

openfeign需要把导入两个依赖,一个是openfeign,一个负载均衡,因为openfeign在发送请求的时候需要对集群服务使用负载均衡算法得到最终发送请求的服务实例。

        <!--OpenFeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
       
        <!--负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

2.编写FeignClient接口

package com.feisi.clients;
import com.fei.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 用户http的客户端接口
 */
@RequestMapping("user")
@FeignClient("user-service")    //@FeignClient 表示该接口是OpenFeign的客户端接口
public interface UserClient {
    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Integer id);
}

3.使用注解开启openfeign功能

最后只需要在启动类使用注解@EnableFeignClients("com.feisi.clients")来开启这个注解,括号里面填的是你的上面那个接口的全限定名,这样才能扫描到。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供OpenFeign远程调用的示例。首先,请确保您已经在项目中引入了OpenFeign的依赖。 接下来,您需要定义一个Feign客户端接口,用于声明远程调用的方法。这里以调用一个名为"example-service"的远程服务为例,示例代码如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "example-service") public interface ExampleServiceClient { @GetMapping("/api/example") String getExampleData(); } ``` 在上述示例中,我们使用`@FeignClient`注解指定了远程服务的名称,这里是"example-service"。然后通过`@GetMapping`注解指定了远程调用的URL路径。 接下来,您需要在启动类上添加`@EnableFeignClients`注解,以启用Feign客户端的自动配置。 完成以上准备工作后,您就可以在需要调用远程服务的地方注入`ExampleServiceClient`接口,并使用其中定义的方法进行远程调用了。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ExampleController { @Autowired private ExampleServiceClient exampleServiceClient; @GetMapping("/example") public String getExampleData() { return exampleServiceClient.getExampleData(); } } ``` 在上述示例中,我们通过`@Autowired`注解将`ExampleServiceClient`接口注入到`ExampleController`中,并在`getExampleData()`方法中调用该接口的方法。 这就是一个简单的OpenFeign远程调用的示例。您可以根据实际需要,按照类似的方式进行远程调用。希望对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值