目录地址:
因为是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的接口