赞
踩
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
String url = "http://localhost:8081/user/"+order.getUserId();
// restTemplate.getForObject(url:传路径,User.class:把json对象直接转成User对象或者其他需要的对象)
User user = restTemplate.getForObject(url, User.class);
order.setUser(user);
// 4.返回
return order;
@Service public class OrderService { @Autowired private OrderMapper orderMapper; @Autowired private RestTemplate restTemplate; public Order queryOrderById(Long orderId) { // 1.查询订单 Order order = orderMapper.findById(orderId); // 服务地址eureka会自己去调用user端口 String url = "http://userservice/user/"+order.getUserId(); // restTemplate.getForObject(url:传路径,User.class:把json对象直接转成User对象或者其他需要的对象) User user = restTemplate.getForObject(url, User.class); order.setUser(user); // 4.返回 return order; } }
@Bean
public IRule randomRule(){
return new RandomRule();
}
---------------------
//yml:
userservice:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # 负载均衡规则
ribbon:
eager-load:
enabled: true # 开启饥饿加载
clients: # 指定饥饿加载的服务名称
- userservice
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。