【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)传入过来的参数。

完结!


相关文章
|
6月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
6月前
|
Java Spring
Spring boot 运行服务jar外配置配置文件方式总结
Spring boot 运行服务jar外配置配置文件方式总结
995 0
|
5月前
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
|
3月前
|
Java 网络架构 Kotlin
kotlin+springboot入门级别教程,教你如何用kotlin和springboot搭建http
本文是一个入门级教程,介绍了如何使用Kotlin和Spring Boot搭建HTTP服务,并强调了Kotlin的空安全性特性。
120 7
kotlin+springboot入门级别教程,教你如何用kotlin和springboot搭建http
|
4月前
|
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序列化)
|
4月前
|
Java API 对象存储
微服务魔法启动!Spring Cloud与Netflix OSS联手,零基础也能创造服务奇迹!
这段内容介绍了如何使用Spring Cloud和Netflix OSS构建微服务架构。首先,基于Spring Boot创建项目并添加Spring Cloud依赖项。接着配置Eureka服务器实现服务发现,然后创建REST控制器作为API入口。为提高服务稳定性,利用Hystrix实现断路器模式。最后,在启动类中启用Eureka客户端功能。此外,还可集成其他Netflix OSS组件以增强系统功能。通过这些步骤,开发者可以更高效地构建稳定且可扩展的微服务系统。
80 1
|
3月前
|
SQL JSON 缓存
你了解 SpringBoot 在一次 http 请求中耗费了多少内存吗?
在工作中常需进行全链路压测并优化JVM参数。通过实验可精确计算特定并发下所需的堆内存,并结合JVM新生代大小估算GC频率,进而优化系统。实验基于SpringBoot应用,利用JMeter模拟并发请求,分析GC日志得出:单次HTTP请求平均消耗约34KB堆内存。复杂环境下,如公司线上环境,单次RPC请求内存消耗可达0.5MB至1MB,揭示了高并发场景下的内存管理挑战。
|
4月前
|
Java API 开发者
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
855 0
|
6月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
146 3
|
5月前
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?