再添加一个支付模块
增加PaymentController的输出信息
serverport采取了@value直接的值注入,从全局配置中读取
@Value("${server.port}")
private String serverPort;
@GetMapping(value="/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
if (payment == null){
return new CommonResult(444,"查询数据失败,ID:"+id,null);
}else{
return new CommonResult(200,"恭喜您!得到对应数据,serverPort= " + serverPort,payment);
}
}
修改OrderController
//之前订单服务被写死了
//public static final String PAYMENT_URL="http://localhost:8001";
public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
RestTemplate支持负载均衡
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced//增加负载均衡注解,让Eureka按照最优策略为指定请求提供微服务的具体主机
public RestTemplate getRegetTemplate(){
return new RestTemplate();
}
}
测试
两个已注册的支付模块会被交替调用