微服务之间调用
共三个模块
git地址:https://gitee.com/wjh1128/cloud
https://gitee.com/wjh1128/cloud.git
cloud-provider-payment8081:服务提供者
cloud-consumer-order80:服务消费者
cloud-api-commons:提供者与消费者共同实体类
使用原始的RestTemplate方式进行服务调用
引入RestTemplate配置
@Configuration
public class ApplicationConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
使用
@RestController
public class OrderController {
private static final String PAYMENT_URL = "http://localhost:8081";
private final RestTemplate restTemplate;
public OrderController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult get(@PathVariable("id") int id) {
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
}
@GetMapping("/consumer/payment/create")
public CommonResult create(@RequestBody Payment payment) {
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
}