SpringCloud使用Feign

简介: SpringCloud发起Feign请求的工具

快速在项目中使用Feign, 无需定义FeignClient类即可使用

一、使用方法

1. 创建SpringCloud项目并加入依赖

<dependency>
    <groupId>cn.gjing</groupId>
    <artifactId>tools-starter-feign</artifactId>
    <version>1.1.0</version>
</dependency>

2. 启动类标注@EnableFeignUtil注解

@SpringBootApplication
@EnableEurekaClient
@EnableFeignUtil
public class FeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeignApplication.class, args);
    }
}

3. 使用案例

@RestController
public class TestController {

    @GetMapping("/test1")
    @ApiOperation(value = "测试1", httpMethod = "GET")
    public ResponseEntity test1() throws URISyntaxException {
        Map<String, String> map = new HashMap<>(16);
        map.put("a", "参数a");
        //使用 URL 访问,自定义返回值类型
        String result = FeignClientUtil.of(String.class, RouteType.URL, "http://127.0.0.1:8090/")
                .execute(HttpMethod.POST, map, "/user")
                .getResult();
        return ResponseEntity.ok(result);
    }

    @GetMapping("/test2")
    public ResponseEntity test2() throws URISyntaxException {
        Map<String, String> map = new HashMap<>(16);
        map.put("a", "参数a");
        map.put("b", "参数b");
        //使用服务名访问,带负载均衡功能,自定义返回值类型
        String result = FeignClientUtil.of(String.class, RouteType.NAME, "demo")
                .execute(HttpMethod.POST, map, "/user")
                .getResult();
        return ResponseEntity.ok(result);
    }

    @GetMapping("/test3")
    public ResponseEntity test3() throws URISyntaxException {
        //使用服务名访问,带负载均衡功能,默认返回值类型(String)
        String result = FeignClientUtil.ofByName("demo")
                .execute(HttpMethod.GET, null, "/user/1")
                .getResult();
        return ResponseEntity.ok(result);
    }

    @GetMapping("/test4")
    public ResponseEntity test4() throws URISyntaxException {
        //使用URL访问,默认返回值类型(String)
        String result = FeignClientUtil.ofByUrl("127.0.0.1:8080")
                .execute(HttpMethod.GET, null, "/user")
                .getResult();
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/test5")
    public ResponseEntity test5() throws URISyntaxException {
        //发起参数为json类型
        PageResult result = FeignClientUtil.of(PageResult.class, RouteType.NAME, "demo")
                .executeByJsonEntity(PageResult.of("xxx", 1), "/user")
                .getResult();
        return ResponseEntity.ok(result.toString());
    }
}

注意点:

URL请求时,可请求与当前服务不在同一个Eureka注册中心下的其他服务接口, 使用服务名路由,会带负载均衡功能,必须与当前服务在同一个Eureka注册中心下;

二. 内部方法介绍

1、of: 生成FeignClientUtil实例

FeignClientUtil.of(responseType, routeType, targetAddress);

参数介绍

参数 描述
responseType 返回值类型, 必填
routeType 路由类型, 总共有两种, 分别为URL和服务名路由, 必填
targetAddress 目标地址, 服务名路由模式直接传对应服务名, URL路由需要协议+IP+端口, 如: http://127.0.0.1:8080, 必填

2、ofByName: 生成采用服务名路由的实例

该实例发起请求后返回值类型为字符串类型

FeignClientUtil.ofByName("demo");

3、ofByUrl: 生成采用Url路由的实例

该实例发起请求后返回值为字符串类型

FeginClientUtil.ofByUrl("http://127.0.0.1:8080");

4、execute: 发起请求

FeignClientUtil.execute(method, queryMap, methodPath);

参数介绍

参数 描述
method HttpMethod对象, 指定请求类型是GET还是POST等等
queryMap 请求参数, 无参可传null
methodPath 请求的接口路径, 如: /user_list

5、executeByJsonEntity: 发起Json请求

FeignClientUtil.executeByJsonEntity(jsonEntity, methodPath);

参数介绍

参数 描述
jsonEntity Json字符串、实体对象、Map皆可
methodPath 请求的接口路径, 如: /user_list

6、getResult: 获取请求结果

FeignClientUtil.getResult();

工具就介绍到这里啦,源代码地址:tools-starter-feign

目录
相关文章
|
1天前
|
监控 安全 Java
Spring cloud原理详解
Spring cloud原理详解
10 0
|
5天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
14 1
|
7天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
32 6
|
11天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
12天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
13天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
28 1
|
13天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
15 2
|
13天前
|
监控 Java Sentinel
Spring Cloud Sentinel:概念与实战应用
【4月更文挑战第28天】在分布式微服务架构中,确保系统的稳定性和可靠性至关重要。Spring Cloud Sentinel 为微服务提供流量控制、熔断降级和系统负载保护,有效预防服务雪崩。本篇博客深入探讨 Spring Cloud Sentinel 的核心概念,并通过实际案例展示其在项目中的应用。
24 0
|
14天前
|
Cloud Native Java Nacos
Spring Cloud Nacos:概念与实战应用
【4月更文挑战第28天】Spring Cloud Nacos 是一个基于 Spring Cloud 构建的服务发现和配置管理工具,适用于微服务架构。Nacos 提供了动态服务发现、服务配置、服务元数据及流量管理等功能,帮助开发者构建云原生应用。
21 0