当前位置: 首页 > article >正文

在微服务中,如何使用feign在各个微服务中进行远程调用

在微服务中,如何使用feign在不同微服务中进行远程调用

在微服务中,如何使用feign在不同微服务中进行远程调用

步骤:

第一步:

引入feign依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

这里没有指定版本号是因为…中的spring-cloud指定了版本号,如下

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

第二步:在启动类中添加@EnableFeignClients注解

@EnableFeignClients //开启远程调用
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

第三步:编写远程调用接口。现search服务需要调用product服务下/product/attr/info/{attrId}路径下的方法,该被调用的方法为:

    @RequestMapping("/product/attr/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){

        AttrRespVo respVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", respVo);
    }

那么在search服务中编写的接口如下:我们需要保证接口中的方法与product中被调用的方法,方法名可以不同,但是他们的参数返回值类型请求路径要一致。接口方法发送get请求.

@FeignClient(name = "product",url = "http://localhost:10000")
public interface ProductFeignService {
@GetMapping("/product/attr/info/{attrId}")
    public R attrInfo(@PathVariable("attrId") Long attrId);

}

第四步:在业务中远程调用,如下:

@Autowired 
ProductFeignService productFeignService;

// 远程调用
Long attrId=1;
R r = productFeignService.attrInfo(attrId);

http://www.kler.cn/a/540976.html

相关文章:

  • IBM服务器刀箱Blade安装Hyper-V Server 2019 操作系统
  • (2024|Nature Medicine,生物医学 AI,BiomedGPT)面向多种生物医学任务的通用视觉-语言基础模型
  • mysql 存储过程和自定义函数 详解
  • 【C++】 STL -- 算法(二)
  • Java 魔法:精准掌控 PDF 合同模板,指定页码与关键字替换签章日期
  • DeepSeek 评价开源框架存在幻觉么?
  • 通过 SQLAlchemy 实现多表映射
  • 【VR开发】【Unity】【游戏开发】如何控制对象的速度
  • 原生鸿蒙版小艺APP接入DeepSeek-R1,为HarmonyOS应用开发注入新活力
  • 使用sunshine和moonlight串流时的音频输出问题
  • rebase和merge
  • 【含开题报告+文档+PPT+源码】学术研究合作与科研项目管理应用的J2EE实施
  • 用Kibana实现Elasticsearch索引的增删改查:实战指南
  • Linux系统引导与服务管理
  • Android双屏异显Presentation接口使用说明
  • celery + redis - 入门
  • 【C++语言】C++入门
  • 【生产变更】- Oracle RAC添加配置ipv6地址
  • 最简单方法本地部署deepseek及其他大模型(如Llama、千问等),有手就行
  • 【非 root 用户下全局使用静态编译的 FFmpeg】
  • 蓝桥杯---数青蛙(leetcode第1419题)
  • Ruby 日期 时间处理指南
  • c/c++蓝桥杯经典编程题100道(20)最长公共子序列
  • 把DeepSeek 装进 VSCode中
  • 接入 deepseek 实现AI智能问诊
  • Hive之[Hive]详细安装步骤