ruoyi-plus通过OpenFeign调用非jvm微服务接口 日期:2025-05-13 分类:1 人气:1 有一个需求是这样的:我需要做两个项目的集成,其中一个项目A使用的是基于Spring Boot、Spring Cloud & Alibaba的若依微服务架构,另一个项目B使用的是struts+spring+hibernate。请问是否能够将项目B作为项目A下面的一个微服务进行集成? > 经过调查项目B改造微服务难度较大,讨论之后决定使用sidecar将项目B作为异构微服务注册到nacos中,然后在项目A中使用OpenFeign进行服务间调用。 B项目的主要任务就是编写Restful格式的接口,这里不细讲,下面讲一下A项目如何通过微服务访问B项目的接口 ### 新建两个微服务模块 - ruoyi-sidecar,此sidecar服务对应项目B,通过访问sidecar服务来调用项目B。无需知晓被调用服务的IP和端口,只需要知道该服务在注册中心的服务名即可。 - ruoyi-collection,此服务为业务模块,通过`OpenFeign`调用`ruoyi-sidecar`服务。 ### ruoyi-sidecar模块配置sidecar - pom中添加依赖 ``` <!--Spring Cloud Sidecar--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sidecar</artifactId> <version>2022.0.0.0</version> </dependency> ``` - 编写启动类 ``` package com.longday.sidecar; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 桥接模块 * * @author ruoyi */ @SpringBootApplication public class RuoYiSidecarApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(RuoYiSidecarApplication.class); application.run(args); System.out.println("(♥◠‿◠)ノ゙ Sidecar模块启动成功 ლ(´ڡ`ლ)゙ "); } } ``` - 新增nacos配置文件`ruoyi-sidecar.yml` ``` sidecar: # 异构微服务的IP ip: 127.0.0.1 # 异构微服务的端口 port: 8090 # 对应第三方程序的health接口 health-check-url: http://localhost:8090/index/index/health ``` 完成以上三步就可以在其他微服务中通过sidecar调用其他应用的接口了。 ### ruoyi-collection模块增加OpenFeign - pom中添加依赖 ``` <!--Spring Cloud Openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>4.0.3</version> </dependency> ``` - 启动文件中增加注解`EnableFeignClients` ``` package com.longday.collection; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 业务模块 * * @author ruoyi */ @EnableDubbo @EnableFeignClients @SpringBootApplication(scanBasePackages = {"org.dromara", "com.longday"}) public class RuoYiCollectionApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(RuoYiCollectionApplication.class); application.setApplicationStartup(new BufferingApplicationStartup(2048)); application.run(args); System.out.println("(♥◠‿◠)ノ゙ 业务模块启动成功 ლ(´ڡ`ლ)゙ "); } } ``` - 定义FeigenClient接口 ``` package com.longday.collection.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import com.longday.collection.factory.CollectionFeigenClientFallback; /** * 服务接口 * * @author ruoyi */ @FeignClient(contextId = "collectionFeigenClient", value = "ruoyi-sidecar", fallback = CollectionFeigenClientFallback.class) public interface CollectionFeigenClient { /** * test * * @param username 用户名 * @return 结果 */ @GetMapping(value = "/index/index/hello?name={username}") public String hello(@PathVariable("username") String username); } ``` 其中接口的注解@FeignClient的参数value为sidecar模块的名称 方法的注解@GetMapping的参数value为项目B接口的地址 上述方法hello其实是调用服务:http://ruoyi-sidecar/index/index/hello?name=list 也就是调用:http://localhost:8090/index/index/hello?name=list - 调用FeigenClient接口 例如在控制器中调用 ``` @Autowired private CollectionFeigenClient collectionFeigenClient; /** * test */ @GetMapping("/hello/{username}") public R<String> info(@PathVariable("username") String username) { return R.ok(collectionFeigenClient.hello(username)); } ``` 通过注解@Autowired引用上一步定义的FeigenClient接口 ### 鸣谢 - [阿里云](https://developer.aliyun.com/article/839493) - [博客园**文依悠然**](https://www.cnblogs.com/vinphy/p/17119844.html) - [腾讯云**行百里er**](https://cloud.tencent.com/developer/article/2171663) - [脚本之家**卷不动躺不平的粥**](https://www.jb51.net/program/314751out.htm) 标签: java springboot 上一篇:vscode使用Community Server Connector运行JDK1.8项目报错 下一篇:fontforge使用python脚本调整字体 随便看看 2024-02-19 PHP7 运算符“??” 和“?:”的区别 2022-11-30 Linux 后台运行命令 2022-11-25 关于我们 2022-11-30 centos一键系统安装lnmp集成环境 2022-11-30 linux 生成 ssh 公钥 留言