SpringCloud基于Feign实现远程调用的问题小结

 更新时间:2024年02月21日 09:44:55   作者:Damon小智  
这篇文章主要介绍了SpringCloud基于Feign远程调用,通过使用 Feign 的方式,我们可以更加优雅地进行多参数的远程调用,避免了手动拼接URL或构建复杂的请求体,需要的朋友可以参考下

Spring Cloud 是一个用于构建分布式系统的开发工具包,它提供了一系列的微服务组件,其中之一就是 Feign。Feign 是一种声明式的 Web 服务客户端,它简化了在 Spring Cloud 中进行远程调用的过程。本文将介绍如何在 Spring Cloud 中使用 Feign 进行远程调用。

一、引入Feign依赖

我们在 Spring Cloud 项目的 pom.xml 中,添加 Feign 的依赖。

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

二、定义和使用Feign客户端

在远程调用的服务模块中,创建一个 Feign 客户端接口

package com.example.eurekaconsumer.demos.web;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient ("userseryice")
public interface UserClient {
    @GetMapping("/user/{name}")
    User findById(@PathVariable("name") String name);
}

这个接口使用了 Spring MVC 的注解,定义了远程服务的调用方式。  

三、启动类开启Feign客户端

启动类添加 @EnableFeignClients 注解:

package com.example.eurekaconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerApplication.class, args);
    }
}

四、调用FeignClient接口

在需要应用的模块中,注入 Feign 客户端接口并使用它来进行远程调用。

package com.example.eurekaconsumer.demos.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @Autowired
    private UserClient userClient;
    @RequestMapping("/showUser")
    @ResponseBody
    public User showUser() {
        User userInfo = userClient.findByName("Damon");
        return userInfo;
    }
}

可以看到,使用 Feign 调用的方法非常优雅,可维护性也很强。

五、FeignClient应用实例

1、实现负载均衡

我们可以用 FeignClient 代替 RestTemplate 以实现负载均衡。

我们先看下参考原有的 RestTemplate 实现负载均衡的代码:

package com.example.eurekaconsumer.demos.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/showUser")
    @ResponseBody
    public User showUser() {
        String baseUrl = "http://" + "eureka-provider" + "/user";
        User userInfo = restTemplate.getForObject(baseUrl, User.class);
        return userInfo;
    }
}

可以看到我们用 RestTemplate 实现负载均衡时,遇到没有参数传递的情况还是比较方便的,但是遇到形如 url?param1=xxx&param2=xxx&param3=xxx&param4=xxx 的应用场景时就需要重构代码,非常的不方便。

于是我们使用自带负载均衡的 Feign 远程调用方法,改造后的方法如下:

package com.example.eurekaconsumer.demos.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
    @Autowired
    private UserClient userClient;
    @RequestMapping("/showUser")
    @ResponseBody
    public User showUser() {
        User userInfo = userClient.findByName("Damon");
        return userInfo;
    }
}

以上是一个简单的 Spring Cloud 中基于 Feign 的远程调用的示例。通过使用 Feign,你可以以声明式的方式定义远程服务调用,而无需手动处理 HTTP 请求和响应。这提高了代码的可读性和维护性,使远程调用更加方便。 

Feign 替换 RestTemplate 的好处: 

优势详细内容
声明式
API 定义
使用Feign时,你可以通过简单的注解方式声明HTTP请求,而不需要手动构建请求和处理响应。Feign的注解功能使得定义和维护API变得更加直观和容易。
集成了
负载均衡
在Spring Cloud环境中,Feign与Eureka或其他服务发现组件集成,可以自动进行负载均衡。你只需通过@FeignClient注解指定服务名,Feign就会在调用时自动帮你选择可用的服务实例。
支持多种编码
器和解码器
Feign支持多种编码器和解码器,包括JacksonGson等,这使得处理不同的数据格式变得更加灵活。
支持
内置断路器
Feign内置了断路器(Circuit Breaker)的支持,例如通过Hystrix。这使得在远程调用失败或超时时,可以采取快速失败和降级的策略,提高系统的稳定性和可靠性。
更易扩展Feign的设计使得它更易于扩展和自定义。你可以通过实现RequestInterceptor接口来添加自定义的请求拦截器,或者通过实现ErrorDecoder接口来处理自定义的错误解码逻辑。
简化了
配置和使用
Feign的默认配置较为智能,使得在大多数情况下你无需进行额外的配置就能够正常工作。相比之下,RestTemplate通常需要手动配置。

2、 实现多参数调用

当使用 FeignClient 进行远程调用时,有时我们需要传递多个参数给目标服务。使用 Feign 的多参数远程调用能够使代码更加优雅,避免了手动拼接 URL 或请求参数的繁琐工作。

以下是一个关于 FeignClient 多参数远程调用的应用实例:

① 创建FeignClient接口

首先,定义一个FeignClient接口,使用 @FeignClient 注解标记目标服务的名称。在接口中定义多个参数的远程调用方法。

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "test-service")
public interface TestClient {
    @GetMapping("/api/test")
    String getResource(@RequestParam("param1") String param1,
                       @RequestParam("param2") int param2,
                       @RequestParam("param3") boolean param3);
}

在上述例子中,getResource 方法接收多个参数,分别使用 @RequestParam 注解进行标记。

② 基于FeignClient多参数调用

注入 FeignClient 接口并使用它进行多参数的远程调用。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
    @Autowired
    private TestClient testClient;
    @GetMapping("/test")
    public String test(@RequestParam("param1") String param1,
                             @RequestParam("param2") int param2,
                             @RequestParam("param3") boolean param3) {
        // 调用远程服务并传递多个参数
        String result = testClient.getResource(param1, param2, param3);
        return "Result from test service: " + result;
    }
}

在这个例子中,TestController 的 test 方法接收多个参数,然后使用注入的 TestClient 进行远程调用,并传递这些参数。

通过使用 Feign 的方式,我们可以更加优雅地进行多参数的远程调用,避免了手动拼接URL或构建复杂的请求体。Feign 会自动将参数转化为请求参数,使得代码更加清晰、简洁。这种方式也符合 Spring Cloud 中微服务架构的设计理念,提高了代码的可读性和可维护性。

到此这篇关于SpringCloud基于Feign远程调用的文章就介绍到这了,更多相关SpringCloud Feign远程调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java实现提取QSV文件视频内容

    Java实现提取QSV文件视频内容

    QSV是一种加密的视频文件格式。是爱奇艺公司研发的一种视频文件格式,这篇文章主要为大家介绍了如何利用Java实现提取QSV文件视频内容,感兴趣的可以了解一下
    2023-03-03
  • 使用springboot对外部静态资源文件的处理操作

    使用springboot对外部静态资源文件的处理操作

    这篇文章主要介绍了使用springboot对外部静态资源文件的处理操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java如何提供给第三方使用接口方法详解

    Java如何提供给第三方使用接口方法详解

    最近在做一个项目,因一些机制问题,需要我用java代码调用第三方接口,下面这篇文章主要给大家介绍了关于Java如何提供给第三方使用接口方法的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • 全面了解java基本类型和封装类型的区别及应用

    全面了解java基本类型和封装类型的区别及应用

    下面小编就为大家带来一篇全面了解java基本类型和封装类型的区别及应用。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • SpringBoot浅析安全管理之基于数据库认证

    SpringBoot浅析安全管理之基于数据库认证

    在真实的项目中,用户的基本信息以及角色等都存储在数据库中,因此需要从数据库中获取数据进行认证和授权
    2022-08-08
  • SpringBoot结合Redis实现缓存管理功能

    SpringBoot结合Redis实现缓存管理功能

    本篇文章主要介绍spring boot缓存管理机制及相关概念,以及如何结合Redis实现缓存管理,文中通过代码示例给大家介绍的非常详细,具有一定的参考价值,需要的朋友可以参考下
    2024-01-01
  • idea启动项目报端口号冲突或被占用的解决方法

    idea启动项目报端口号冲突或被占用的解决方法

    这篇文章主要介绍了idea启动项目报端口号冲突或被占用的解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • 浅谈fastjson的常用使用方法

    浅谈fastjson的常用使用方法

    下面小编就为大家带来一篇浅谈fastjson的常用使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Spring如何解决循环依赖的问题

    Spring如何解决循环依赖的问题

    这篇文章主要介绍了Spring是如何解决循环依赖的问题,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Servlet的两种创建方式(xml 注解)示例详解

    Servlet的两种创建方式(xml 注解)示例详解

    这篇文章主要为大家介绍了Servlet的两种创建方式(xml 注解)示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08

最新评论