SpringCloudAlibaba-整合openfeign和loadbalence(三)

目录地址:

SpringCloudAlibaba整合-****博客

因为是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的接口

上一篇:C++11 列表初始化、initializer_list、auto、decltype、array


下一篇:ActiveMQ入门案例(queue模式和topic模式)