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的接口

相关推荐
Mr.wangh18 小时前
SpringCloudConfig(配置中心)
大数据·elasticsearch·搜索引擎·springcloud·config
serendipity_hky3 天前
【SpringCloud | 第2篇】OpenFeign远程调用
java·后端·spring·spring cloud·openfeign
阿拉斯攀登6 天前
Spring Cloud Alibaba 生态中 RocketMQ 最佳实践
分布式·微服务·rocketmq·springcloud·cloudalibaba
失足成万古风流人物8 天前
面试官问MyBatis/OpenFeign的原理?我手搓了个MyHttp怼回去!(反八股版)
mybatis·springboot·openfeign·动态代理
阿拉斯攀登11 天前
SpringCloudAlibaba之Nacos
微服务·服务发现·springcloud
阿拉斯攀登11 天前
深入微服务配置中心:Nacos注册中心的实操细节
java·微服务·云原生·springcloud
小坏讲微服务13 天前
Spring Boot 4.0 与 MyBatis Plus 整合完整指南
java·spring boot·后端·mybatis·springcloud·mybatis plus·java开发
咖啡不甜不好喝14 天前
sentinel踩坑记录
sentinel·springcloud
她说..14 天前
Spring AOP 操作日志框架(CV可用)
java·jvm·spring·springboot·springcloud
better_liang15 天前
每日Java面试场景题知识点之-分布式事务处理
java·微服务·面试·springcloud·分布式事务