下载依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
需要在启动类上开启配置,Feigh默认自带负载均衡配置
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
新建一个接口
@FeignClient("userservice")//提供者的服务名
public interface UserClients {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
直接使用即可
@Autowired
private UserClients userClients;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId); //忽略,和知识点无关
order.setUser(userClients.findById(orderId));
return order;
}
标签:orderId,findById,Http,RestTemplate,public,Feigh,order,id From: https://www.cnblogs.com/cyknote/p/17924596.html