SpringCloudAlibaba-整合openfeign和loadbalence(三)

目录地址:

SpringCloudAlibaba整合-CSDN博客

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

1.引入依赖

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

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

@RestController
public class RemoteController {
    @Autowired
    private RemoteUserService remoteUserService;

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

        return remoteUserService.getById(1);
    }
}

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

order成功调用了user的接口

相关推荐

  1. SpringCloudAlibaba-整合openfeignloadbalence

    2024-04-14 14:16:01       36 阅读
  2. SpringCloudAlibaba-整合sentinel(四)

    2024-04-14 14:16:01       36 阅读
  3. SpringCloudAlibaba-整合gateway(五)

    2024-04-14 14:16:01       38 阅读
  4. Sentinel整合OpenFeign

    2024-04-14 14:16:01       53 阅读
  5. openfeign整合sentinel进行降级

    2024-04-14 14:16:01       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-14 14:16:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-14 14:16:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-14 14:16:01       82 阅读
  4. Python语言-面向对象

    2024-04-14 14:16:01       91 阅读

热门阅读

  1. 多汗症的物理治疗有哪些?

    2024-04-14 14:16:01       33 阅读
  2. 关系网络(c++题解)

    2024-04-14 14:16:01       30 阅读
  3. AspectJ框架实现AOP简介

    2024-04-14 14:16:01       36 阅读
  4. leetcode - 402. Remove K Digits

    2024-04-14 14:16:01       26 阅读
  5. OpenCV深拷贝效率对比

    2024-04-14 14:16:01       35 阅读
  6. vue和nunjucks的变量插值的形式{{}}冲突

    2024-04-14 14:16:01       28 阅读
  7. 【00150】金融理论与实务计算题公式汇总

    2024-04-14 14:16:01       30 阅读
  8. CUDA 12.4文档4 编程接口之使用NVCC编译

    2024-04-14 14:16:01       41 阅读