1. Cree el archivo myrule
2. Escriba el contenido de myrule
package com.myrule;
@Configuration
public class MySelfRule {
@Bean
public IRule myRule(){
return new RandomRule(); //随机的方法
}
}
3. Agregue esta anotación CLOUD-PAYMENT-SERVICE a la clase de inicio principal para configurar el
nombre
del servicio como la clase que escribimos hace un momento.
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = MySelfRule.class)
4. Implementar restTemplate en la configuración
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
5. Implementar la interfaz de solicitud en la capa de control.
@RestController
@CrossOrigin
@Slf4j
public class OrderController {
//public static final String PAYMENT_URL = "http://localhost:8001";
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping(value = "/consumer/payment/get")
public CommonResult<Payment> getById(@RequestParam("id") int id){
return restTemplate.getForObject(PAYMENT_URL + "/get?id=" + id, CommonResult.class);
}
}