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

相关推荐
小坏讲微服务3 分钟前
Docker Compose搭建Git仓库私服上传微服务
分布式·git·docker·微服务·容器·springcloud·springalibaba
小坏讲微服务14 小时前
Spring Cloud Alibaba整合SkyWalking的监控完整使用
java·微服务·架构·springcloud·监控·skywalking·java微服务
小坏讲微服务3 天前
Spring Cloud Alibaba 2025.0.0 与 Nacos 3.1.0 集群整合
分布式·nacos·架构·springcloud·nacos集群·springalibaba
没有bug.的程序员3 天前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud
serendipity_hky9 天前
【微服务 - easy视频 | day01】准备工具+gateway网关及路由至内部服务
java·微服务·架构·gateway·springcloud
三口吃掉你12 天前
微服务之OpenFeign、hystrix熔断降级、loadbalancer负载均衡
hystrix·微服务·负载均衡·openfeign
装不满的克莱因瓶13 天前
【Java架构师】各个微服务之间有哪些调用方式?
java·开发语言·微服务·架构·dubbo·restful·springcloud
众俗14 天前
SpringCloudAlibaba整合开发
springcloud·alibaba
阿Y加油吧25 天前
微服务——day02
springcloud
1.01^10001 个月前
[3-03-01].第07节:搭建服务 - 服务重构cloud-consumer-ocommon
springcloud