SpringBoot使用Feign进行服务间通信的实现示例代码

 更新时间:2024年01月19日 11:10:03   作者:wx59bcc77095d22  
Feign是一个开源的Java HTTP客户端,可以帮助我们在SpringBoot应用中快速构建和使用HTTP客户端,方便实现服务间的通信,本文就来介绍一下SpringBoot使用Feign进行服务间通信的实现示例代码,感兴趣的可以了解一下

一、前言

在分布式系统中,服务间通信是非常常见的情况。Feign是一个开源的Java HTTP客户端,可以帮助我们在SpringBoot应用中快速构建和使用HTTP客户端,方便实现服务间的通信。与其他HTTP客户端相比,Feign具有简化 HTTP API定义、支持多种HTTP请求方法、支持请求和响应的压缩、支持请求和响应的日志记录、支持多种负载均衡器、支持自定义拦截器和错误处理器等特点。

二、SpringBoot集成

1.添加依赖

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

2.启用Feign客户端

我们需要在启动类上添加@EnableFeignClients注解,启用Feign客户端。

package com.example.nettydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
@EnableFeignClients
public class NettyDemoApplication {

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

3.定义Feign客户端接口

@FeignClient注解里面的url指定需要请求的URL地址,name指定客户端的名称。

package com.example.nettydemo.feign;

import com.example.nettydemo.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author qx
 * @date 2023/12/28
 * @des Feign客户端
 */
@FeignClient(url = "http://127.0.0.1:8090/user", name = "user")
public interface UserFeignClient {


    @GetMapping("/{id}")
    User selectUserById(@PathVariable("id") Long id);

}

4.定义目标控制层接口

package com.example.nettydemo.controller;

import com.example.nettydemo.entity.User;
import com.example.nettydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2023/12/28
 * @des
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/{id}")
    public User selectUserById(@PathVariable("id") Long id) {
        return userService.getUserById(id);
    }
}

5.创建Feign测试控制层

package com.example.nettydemo.controller;

import com.example.nettydemo.entity.User;
import com.example.nettydemo.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2023/12/28
 * @des Feign测试
 */
@RestController
@RequestMapping("/userFeign")
public class UserFeignController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/{id}")
    public User getUserInfo(@PathVariable("id") Long id) {
        return userFeignClient.selectUserById(id);
    }
}

6.测试

我们先测试目标请求接口是否正确。

SpringBoot使用Feign进行服务间通信_通信

然后我们再使用Feign的方式请求接口的方式进行测试。

SpringBoot使用Feign进行服务间通信_通信_02

这样我们使用Feign方式请求,成功请求目的地址获取到了一样的数据。

到此这篇关于SpringBoot使用Feign进行服务间通信的实现示例代码的文章就介绍到这了,更多相关SpringBoot Feign服务间通信 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java的三种IO模型详解(BIO、NIO、AIO)

    java的三种IO模型详解(BIO、NIO、AIO)

    本文介绍了BIO、NIO和AIO三种不同的IO模型,分别分析了它们的工作机制、实现方式以及与BIO的对比,BIO是阻塞的,每个连接需要一个线程;NIO是同步非阻塞的,通过缓冲区和选择器实现I/O多路复用;AIO是异步的,操作系统处理IO操作,完成后通知应用程序
    2024-11-11
  • Spring Gateway处理微服务的路由转发机制

    Spring Gateway处理微服务的路由转发机制

    我们详细地介绍了Spring Gateway,这个基于Spring 5、Spring Boot 2和Project Reactor的API网关,通过这篇文章,我们可以清晰地看到Spring Gateway的工作原理,以及它的强大之处,感兴趣的朋友一起看看吧
    2024-08-08
  • C++排序算法之桶排序原理及实现详解

    C++排序算法之桶排序原理及实现详解

    这篇文章主要介绍了C++排序算法之桶排序原理及实现详解, C++ 桶排序是一种线性时间复杂度的排序算法,它通过将待排序元素分配到不同的桶中,然后对每个桶中的元素进行排序,最后将所有桶中的元素按顺序合并得到有序序列,需要的朋友可以参考下
    2023-10-10
  • SpringMVC Idea 搭建 部署war的详细过程

    SpringMVC Idea 搭建 部署war的详细过程

    本文介绍了如何在IntelliJ IDEA中使用Maven模板创建一个Web项目,并详细说明了如何配置web.xml、创建springmvc-servlet.xml和application.properties文件,以及如何使用Maven打包生成WAR文件并部署到Tomcat服务器,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • Java线程池ForkJoinPool实例解析

    Java线程池ForkJoinPool实例解析

    这篇文章主要介绍了Java线程池ForkJoinPool实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Java代码中4种字符串拼接方式分析

    Java代码中4种字符串拼接方式分析

    本文主要介绍了Java代码中4种字符串拼接方式分析,主要介绍了“+”号、StringBuilder、StringJoiner、String#join,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Spring boot中使用Spring-data-jpa方便快捷的访问数据库(推荐)

    Spring boot中使用Spring-data-jpa方便快捷的访问数据库(推荐)

    Spring Data JPA 是 Spring 基于 ORM 框架、JPA 规范的基础上封装的一套JPA应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。这篇文章主要介绍了Spring-boot中使用Spring-data-jpa方便快捷的访问数据库,需要的朋友可以参考下
    2018-05-05
  • Spring的组合注解和元注解原理与用法详解

    Spring的组合注解和元注解原理与用法详解

    这篇文章主要介绍了Spring的组合注解和元注解原理与用法,结合实例形式详细分析了spring组合注解和元注解相关功能、原理、配置及使用方法,需要的朋友可以参考下
    2019-11-11
  • 使用Java获取文件树的代码实现

    使用Java获取文件树的代码实现

    Java语言提供了丰富的库和工具,使得我们可以方便地获取和操作Java文件的语法树(AST, Abstract Syntax Tree),在这篇博客中,我们将探讨如何使用Java来获取一个Java文件的语法树,并展示详细的代码示例和运行结果,需要的朋友可以参考下
    2024-08-08
  • Java数据结构之优先级队列(PriorityQueue)用法详解

    Java数据结构之优先级队列(PriorityQueue)用法详解

    优先级队列是一种先进先出的数据结构,操作的数据带有优先级,这种数据结构就是优先级队列(PriorityQueue)。本文将详细讲讲Java优先级队列的用法,感兴趣的可以了解一下
    2022-07-07

最新评论