SpringCloudAlibaba-整合openfeign和loadbalence(三)

目录地址:

SpringCloudAlibaba整合-CSDN博客

因为是order服务,调用user和product服务;所以这里在order模块操作;

1.引入依赖

XML 复制代码
<!--openfeign-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

2.启动类添加注解 @EnableFeignClients

3.编写接口,如:RemoteUserService,里面调用user的接口

java 复制代码
@Component
@FeignClient(name="my-user")
public interface RemoteUserService {
    @RequestMapping("/user/listAll")
    public List<User> listAll();

    @GetMapping("/user/getById")
    public User getById(@RequestParam("id") Integer id);
}

4.在order服务中,写一个controller,调用RemoteProductService中的接口

java 复制代码
@RestController
public class RemoteController {
    @Autowired
    private RemoteUserService remoteUserService;

    @RequestMapping("/getUserById")
    public User getUserById(){

        return remoteUserService.getById(1);
    }
}

5.访问order中的getUserById接口,访问成功

order成功调用了user的接口

相关推荐
欢醉8 小时前
一次RabbitMQ重启引发的网关雪崩复盘
springcloud·架构设计
欢醉7 天前
生产复盘:请求报413 Request Entity Too Large问题分享
springboot·springcloud
苏生Susheng11 天前
【软件实施】Linux企业运维常用命令手册
java·linux·运维·服务器·springboot·springcloud·软件实施
jonyleek19 天前
技术实践:基于 Vue3 + Spring Cloud 微服务实现私有化文档系统的类 SaaS 协同体验
微服务·私有化部署·vue3·springcloud·协同编辑·jvs企业文档·无忧企业文档
欢醉21 天前
干货分享Gateway踩坑实录:Netty直接内存把网关吃垮了OutOfDirectMemoryError
jvm·springcloud
Rain的Java大神之路25 天前
介绍一下分布式事务
java·分布式·后端·spring·spring cloud·架构·springcloud
LayZhangStrive1 个月前
后端通识 - 远程服务调用RPC
网络·网络协议·rpc·sentinel·openfeign·远程服务调用
SL-staff1 个月前
JVS数字底座实践:如何复用企业文档能力快速构建知识类应用
低代码·微服务·springcloud·知识管理·权限控制·jvs·elasticsea
952361 个月前
Sentinel
java·后端·spring·sentinel·springcloud
明天谭1 个月前
EzCloud微服务SaaS平台架构解析:多租户零代码一体化企业系统整体设计
开源·springcloud·微服务架构·saas多租户·企业级开发·ezcloud