【Spring Boot】使用Feign作为HTTP客户端调用远程HTTP服务

简介: 【Spring Boot】使用Feign作为HTTP客户端调用远程HTTP服务

一、背景简介

最近做的一个公司项目,由于功能需求,需要在两个springboot项目间的进行远程调用,我使用的是通过Feign的方式。而springboot本身封装了两种方法HTTP调用方式:

  1. feign的远程调用(http接口调用)
  2. RestTemplate

下面记录一下我使用的过程(项目A调用项目B):

二、调用方(项目A)

首先是调用方(项目A):

项目技术架构:Spring boot(2.0.0.RELEASE) + Mybatis-plus(3.1.1) + druid(1.1.9)

第一步:添加Maven依赖

<!-- openfein的依赖 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <version>2.0.4.RELEASE</version>
</dependency>

第二步:添加@EnableFeignClients注解

在启动类上添加@EnableFeignClients(basePackages = {“com..”}),其中****号代表你实际的项目目录结构,可以根据你的项目实际填写;

第三步:创建FeignClient接口

package com.iot.flowapplication.feign;
import com.iot.flowapplication.common.domain.vo.JsonResult;
import com.iot.flowapplication.organization.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * <p>此类用于流程管理服务调用日报服务查询日报列表</p>
 * <p>@author:xzj</p>
 * <p>@date:2019/8/29</p>
 * <p>@remark</p>
 */
@FeignClient(url = "${dailyServer}", name = "daily")
public interface DailyService {
    /**
     * 日报统计
     *
     * @param currUser 当前登录用户
     * @return 日报列表
     */
    @RequestMapping(value = "/api/statistics/getNoSubmitList", method = RequestMethod.POST)
    JsonResult dailyStatistics(@RequestBody User currUser);
}

说明:

@FeignClient(url = “${dailyServer}”, name = “daily”),其中url的值是被调用方的域名加端口,例如:(dailyServer: http://localhost:8006),而name的值可以写成被调用方的服务名称等。

写法总结:

  1. 在resources目录下的yml配置文件里写:dailyServer: http://localhost:8006
  2. 直接在url后面写上,例如:@FeignClient(url = “http://localhost:8006”, name =
    “daily”)

推荐使用第一种写法。

第四步:调用方使用

package com.iot.flowapplication.feign;
import com.iot.flowapplication.basics.web.BaseController;
import com.iot.flowapplication.common.domain.vo.JsonResult;
import com.iot.flowapplication.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * <p>DailyStatisticsController 此类用于:本项目对前端提供的接口映射</p>
 * <p>@author:hujm</p>
 * <p>@date:2021年04月29日 14:37</p>
 * <p>@remark:</p>
 */
@Api(tags = "首页日报统计", value = "daily")
@RestController
@RequestMapping(value = "daily")
public class DailyStatisticsController extends BaseController {
    @Resource
    private DailyService dailyService;
    @ApiOperation(value = "获取日报未提交人员列表")
    @GetMapping(value = "/getNoSubmitList")
    public JsonResult getList() {
        User currUser = getCurrUser();
        JsonResult jsonResult = dailyService.dailyStatistics(currUser);
        return jsonResult;
    }
}

至此调用方的步骤全部完成。

三、调用方(项目B)

以下是被调用方(项目B):

项目技术架构:Spring boot(2.0.0.RELEASE) + Mybatis-plus(3.1.1) + druid(1.1.9)

第一步:添加Maven依赖

<!--openfein的依赖-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
  <version>2.0.4.RELEASE</version>
</dependency>

第二步:添加@EnableFeignClients注解

在启动类上添加@EnableFeignClients(basePackages = {“com..”}),其中****号代表你实际的项目目录结构,可以根据你的项目实际填写;

@EnableFeignClients(basePackages = “com..”)

第三步:创建Controller控制层

此步骤按照正常的Spring MVC方式创建一个Controller路径映射即可。这个Controller和平时写的一样。

package com.iot.daily.module.web;
import com.iot.daily.common.domain.vo.JsonResult;
import com.iot.daily.common.web.BaseController;
import com.iot.daily.module.service.DailyStatisticsService;
import com.iot.daily.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
 * <p>DailyStatisticsController 此类用于:日报统计相关接口</p>
 * <p>@author:hujm</p>
 * <p>@date:2021年04月24日 9:14</p>
 * <p>@remark:</p>
 */
@Api(tags = "日报统计相关接口", value = "statistics")
@RestController
@RequestMapping(value = "/api/statistics")
public class DailyStatisticsController extends BaseController {
    @Resource
    private DailyStatisticsService dailyStatisticsService;
    @ApiOperation(value = "获取日报未提交人员列表,用于Portal页面日报统计未提交人员列表")
    @PostMapping(value = "/getNoSubmitList")
    public JsonResult getNoSubmitList(@RequestBody User user) {
        JsonResult jsonResult = dailyStatisticsService.getNoSubmitList(user);
        return jsonResult;
    }
}

说明:DailyStatisticsService接口,以及Mapper接口,都按照正常的业务流程处理即可。而方法接受的参数(User对象)则为调用方(项目A)传入过来的参数。

完结!


相关文章
|
11月前
|
网络协议 Java
SpringBoot快速搭建TCP服务端和客户端
由于工作需要,研究了SpringBoot搭建TCP通信的过程,对于工程需要的小伙伴,只是想快速搭建一个可用的服务.其他的教程看了许多,感觉讲得太复杂,很容易弄乱,这里我只讲效率,展示快速搭建过程。
1094 58
|
10月前
|
JSON 中间件 Go
Go 网络编程:HTTP服务与客户端开发
Go 语言的 `net/http` 包功能强大,可快速构建高并发 HTTP 服务。本文从创建简单 HTTP 服务入手,逐步讲解请求与响应对象、URL 参数处理、自定义路由、JSON 接口、静态文件服务、中间件编写及 HTTPS 配置等内容。通过示例代码展示如何使用 `http.HandleFunc`、`http.ServeMux`、`http.Client` 等工具实现常见功能,帮助开发者掌握构建高效 Web 应用的核心技能。
491 61
|
8月前
|
数据采集 JSON Go
Go语言实战案例:实现HTTP客户端请求并解析响应
本文是 Go 网络与并发实战系列的第 2 篇,详细介绍如何使用 Go 构建 HTTP 客户端,涵盖请求发送、响应解析、错误处理、Header 与 Body 提取等流程,并通过实战代码演示如何并发请求多个 URL,适合希望掌握 Go 网络编程基础的开发者。
|
11月前
|
Java
SpringBoot快速搭建WebSocket服务端和客户端
由于工作需要,研究了SpringBoot搭建WebSocket双向通信的过程,其他的教程看了许多,感觉讲得太复杂,很容易弄乱,这里我只展示快速搭建过程。
2822 1
|
算法 测试技术 C语言
深入理解HTTP/2:nghttp2库源码解析及客户端实现示例
通过解析nghttp2库的源码和实现一个简单的HTTP/2客户端示例,本文详细介绍了HTTP/2的关键特性和nghttp2的核心实现。了解这些内容可以帮助开发者更好地理解HTTP/2协议,提高Web应用的性能和用户体验。对于实际开发中的应用,可以根据需要进一步优化和扩展代码,以满足具体需求。
1249 29
|
缓存 安全 Java
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
1080 5
|
Java 网络架构 Kotlin
kotlin+springboot入门级别教程,教你如何用kotlin和springboot搭建http
本文是一个入门级教程,介绍了如何使用Kotlin和Spring Boot搭建HTTP服务,并强调了Kotlin的空安全性特性。
460 8
kotlin+springboot入门级别教程,教你如何用kotlin和springboot搭建http
使用Netty实现文件传输的HTTP服务器和客户端
本文通过详细的代码示例,展示了如何使用Netty框架实现一个文件传输的HTTP服务器和客户端,包括服务端的文件处理和客户端的文件请求与接收。
454 1
使用Netty实现文件传输的HTTP服务器和客户端
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
下一篇
开通oss服务