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

目录
相关文章
|
3月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
679 3
|
1月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
7月前
|
负载均衡 前端开发 Java
SpringCloud调用组件Feign
本文深入探讨微服务Spring体系中的Feign组件。Feign是一个声明式Web服务客户端,支持注解、编码器/解码器,与Spring MVC注解兼容,并集成Eureka、负载均衡等功能。文章详细介绍了SpringCloud整合Feign的步骤,包括依赖引入、客户端启用、接口创建及调用示例。同时,还涵盖了Feign的核心配置,如超时设置、拦截器实现(Basic认证与自定义)和日志级别调整。最后,总结了`@FeignClient`常用属性,帮助开发者更好地理解和使用Feign进行微服务间通信。
649 1
|
8月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
9月前
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
1701 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
10月前
|
人工智能 安全 Java
AI 时代:从 Spring Cloud Alibaba 到 Spring AI Alibaba
本次分享由阿里云智能集团云原生微服务技术负责人李艳林主讲,主题为“AI时代:从Spring Cloud Alibaba到Spring AI Alibaba”。内容涵盖应用架构演进、AI agent框架发展趋势及Spring AI Alibaba的重磅发布。分享介绍了AI原生架构与传统架构的融合,强调了API优先、事件驱动和AI运维的重要性。同时,详细解析了Spring AI Alibaba的三层抽象设计,包括模型支持、工作流智能体编排及生产可用性构建能力,确保安全合规、高效部署与可观测性。最后,结合实际案例展示了如何利用私域数据优化AI应用,提升业务价值。
981 4
|
11月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
261 6
|
11月前
|
负载均衡 Java 开发者
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
667 5
|
11月前
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
192 5