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

相关推荐
编程彩机1 天前
互联网大厂Java面试:从微服务到分布式缓存的技术场景解析
redis·spring cloud·消息队列·微服务架构·openfeign·java面试·分布式缓存
短剑重铸之日4 天前
《SpringCloud实用版》Stream + RocketMQ 实现可靠消息 & 事务消息
后端·rocketmq·springcloud·消息中间件·事务消息
没有bug.的程序员6 天前
Spring Cloud Stream:消息驱动微服务的实战与 Kafka 集成终极指南
java·微服务·架构·kafka·stream·springcloud·消息驱动
J_liaty6 天前
OpenFeign微服务实战指南
微服务·云原生·架构·openfeign
没有bug.的程序员7 天前
Spring Cloud Gateway:API网关限流与熔断实战
java·开发语言·数据库·spring boot·gateway·api·springcloud
wfsm11 天前
nacos和openfeign
springcloud
J_liaty11 天前
RPC、Feign与OpenFeign技术对比详解
网络·网络协议·rpc·openfeign·feign
无心水11 天前
【分布式利器:腾讯TSF】11、腾讯TSF微服务框架深度对比:全面解析TSF vs Spring Cloud vs Dubbo vs Service Mesh
分布式·spring cloud·微服务·dubbo·springcloud·service mesh·分布式利器
sunnyday042612 天前
Spring Cloud Alibaba Sentinel 流量控制与熔断降级实战指南
spring boot·sentinel·springcloud
递归尽头是星辰15 天前
Dubbo vs OpenFeign:Spring Cloud Alibaba 生态下的 RPC 框架选型与迁移实战
dubbo·微服务架构·openfeign·微服务选型·rpc 框架·http 客户端