SpringCloud-Feign

Feign模块间调用实践
本文介绍如何在Spring Cloud项目中使用Feign实现模块间的API调用,通过示例代码展示FeignClient注解及GetMapping的使用,适用于Eureka服务注册中心的环境下。

一 模块间调用Feign

二 代码

https://github.com/XUEZHIQIAN/SpringCloud-Feign.git

package com.hang.cloud.service;

import com.hang.cloud.service.fallback.OrderServiceFallback;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * api
 *
 * @author Hang W 
 */
@FeignClient(name = "order-service", fallback = OrderServiceFallback.class)
public interface OrderService {

    @GetMapping("order/{id}")
    String queryOrderById(@PathVariable(value="id") Long id);

}

三 说明

1.使用 Eureka 服务注册中心

2.使用 Feign 进行调用

`spring-cloud-starter-feign` 是早期版本中使用的依赖名称,但在 Spring Cloud 的后续版本中已经被废弃。取而代之的是 `spring-cloud-starter-openfeign`[^3]。 如果需要在 Maven 项目中引入 OpenFeign 支持,则应在项目的 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 需要注意的是,OpenFeign 已经成为 Spring Cloud 的一部分,并且默认集成了与 Spring Boot 和 Spring MVC 的良好兼容性。因此无需再单独寻找 `spring-cloud-starter-feign` 这一旧版依赖[^4]。 此外,为了确保项目的正常运行,还需要确认所使用的 Spring Cloud 版本号是否匹配当前的 Spring Boot 版本。可以在 `pom.xml` 中定义 Spring Cloud BOM(Bill of Materials),以便管理所有相关依赖的版本一致性。例如: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <!-- 替换为适合您项目的版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 最后,当使用 Feign 或 OpenFeign 时,通常会结合 Eureka 或 Consul 等服务注册中心来实现动态的服务发现功能。这可以通过启用相应的注解完成,比如 `@EnableDiscoveryClient` 或者 `@EnableEurekaClient`。 ### 示例代码 以下是完整的 Maven 配置示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <!-- 根据实际需求调整版本 --> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>11</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <!-- 使用合适的 Spring Cloud 版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency> </dependencies> </project> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值