代码示例:
1. 修改service层代码:
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
//查询订单
Order order = orderMapper.findById(orderId);
//String url = "http://localhost:8081/user/"+order.getUserId();
String url = "http://userservice/user/" + order.getUserId();
User user = restTemplate.getForObject(url, User.class);
order.setUser(user);
return order;
}
}
2. 加 @LoadBalanced 注解
@SpringBootApplication标签:服务,String,RestTemplate,Eureka,public,user,拉取,order,class From: https://www.cnblogs.com/gagaya2/p/17836996.html
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}