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

相关推荐
iiYcyk12 小时前
Hystrix与Sentinel-熔断限流
hystrix·sentinel·springcloud
iiYcyk2 天前
Ribbon和LoadBalance-负载均衡
springcloud
hqxstudying2 天前
Kafka 深入研究:从架构革新到性能优化的全面解析
java·开发语言·微服务·kafka·springcloud
瑞瑞绮绮3 天前
分布式事务的Java实践
java·分布式·springcloud
麦兜*8 天前
MongoDB 聚合管道(Aggregation)高级用法:数据统计与分析
java·数据库·后端·mongodb·springboot·springcloud
麦兜*10 天前
Spring Boot 集成 Docker 构建与发版完整指南
java·spring boot·后端·spring·docker·系统架构·springcloud
勇往直前plus25 天前
一文学习nacos和openFeign
java·学习·微服务·openfeign
毛小茛1 个月前
Spring Cloud Gateway 实现登录校验:构建统一认证入口
springcloud
●VON1 个月前
重生之我在暑假学习微服务第七天《微服务之服务治理篇》
java·学习·微服务·云原生·nacos·架构·springcloud
yh云想1 个月前
《微服务SpringCloud架构实践指南:从Nacos到Gateway的全面解析》
spring cloud·nacos·gateway·openfeign·filter