首页 > 其他分享 >SpringBoot使用Feign进行服务间通信

SpringBoot使用Feign进行服务间通信

时间:2024-01-19 17:32:46浏览次数:26  
标签:Feign SpringBoot springframework id 间通信 org import annotation

一、前言

在分布式系统中,服务间通信是非常常见的情况。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

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

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

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

标签:Feign,SpringBoot,springframework,id,间通信,org,import,annotation
From: https://blog.51cto.com/u_13312531/9302080

相关文章

  • SpringBoot+dynamic-datasource实现多数据源(msyql、sqlserver、postgresql)手动切换
    场景SpringBoot+MybatisPlus+dynamic-datasources实现连接Postgresql和mysql多数据源:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/135653227上面实现通过注解和配置文件的方式去进行多数据源操作。如果业务需求,比如查询第三方接口时提供的是sqlserver的视图连......
  • SpringBoot集成邮件服务进行校验
    一、前言在我们进行注册、登录等操作的时候,为了保证用户信息的安全性,我们经常会需要接收短信验证码等场景,虽然它的安全系数较高,但是由于需要付费使用,所以我们也可以使用邮箱服务接收验证码来实现安全校验,提升系统安全系数。二、环境准备以QQ邮箱为例,我们需要在邮箱中开启SMTP服务获......
  • 微服务、springboot热部署
    添加热部署依赖,如果项目中已有就不用加了<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency......
  • 基于 SpringBoot + magic-api + Vue3 + Element Plus + amis3.0 快速开发管理系统
    Tansci-Boot基于SpringBoot2+magic-api+Vue3+ElementPlus+amis3.0快速开发管理系统Tansci-Boot是一个前后端分离后台管理系统,前端集成amis低代码前端框架,后端集成magic-api的接口快速开发框架。包含基础权限、安全认证、以及常用的一些组件功能。项目......
  • 进程间通信(生产者消费者模型)
    【一】进程间通信介绍什么是进程间通信进程间通信(Inter-processCommunication,IPC)是指在不同进程之间传输数据或信号的机制。由于每个进程拥有自己独立的内存空间,所以不同进程之间无法直接访问对方的变量或数据结构。因此,操作系统提供了多种IPC机制来允许进程之间共享信息和协......
  • springboot配置分页插件pageHelper和数据库方言的几种方式
    方式一:启动类配置分页插件(Application.java)1/**2*pageHelper分页插件3*/4@Bean5publicPageHelperByMyselfpageHelper(){6PageHelperByMyselfpageHelper=newPageHelperByMyself();7Propertiesproperties=newPr......
  • SpringBoot中操作Bean的生命周期的方法
    SpringBoot中操作Bean的生命周期的方法路人路人甲Java2024-01-1719:17发表于上海引言在SpringBoot应用中,管理和操作Bean的生命周期是一项关键的任务。这不仅涉及到如何创建和销毁Bean,还包括如何在应用的生命周期中对Bean进行精细控制。Spring框架提供了多种机制来......
  • Sa-Token介绍与SpringBoot环境下使用
    个人博客:无奈何杨(wnhyang)个人语雀:wnhyang共享语雀:在线知识共享Github:wnhyang-Overview官网:Sa-Token一个轻量级Java权限认证框架,让鉴权变得简单、优雅!介绍Sa-Token是一个轻量级Java权限认证框架,主要解决:登录认证、权限认证、单点登录、OAuth2.0、分布式Session会话......
  • 0.o?让我看看怎么个事儿之SpringBoot自动配置
    学习SpringBoot自动配置之前我们需要一些前置知识点:Java注解,看完就会用学会@ConfigurationProperties月薪过三千不是银趴~是@Import!@Conditional+@Configuration有没有搞头?首先我们提出2个问题:SpringBoot是干什么的?是用来简化Spring原生的复杂的xml配置的进阶框架......
  • SpringBoot+MybatisPlus+dynamic-datasources实现连接Postgresql和mysql多数据源
    场景dynamic-datasource-spring-boot-starter实现动态数据源Mysql和Sqlserver:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/117356693SpringBoot中整合MybatisPlus快速实现Mysql增删改查和条件构造器:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/detail......