springcloud 消费者模块 使用resttemplate远程调用API

1.微服务 消费者调用微服务提供的方法

2.消费者端口绑定80 绑定80不需要加端口号 server.port=80

3.消费者模块不需要写任何业务 只需要一个controller

4.由于是两个不同的模块 不可以直接使用 需要使用RestTemplate(RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。)用于调用支付模块的API

5.把resttemplate注入容器 配置类 注册组件

6.controller类编写

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    //支付模块端口
    private static  final  String BASE_URL="http://localhost:8001";

    @PostMapping("/comsumer/payment/create")
    public CommonResult<PayMent> create(@RequestBody PayMent payMent){
        return restTemplate.postForObject(BASE_URL+"/payment/create",payMent,CommonResult.class);
    }

    @GetMapping("/comsumer/payment/get/{id}")
    public CommonResult<PayMent> getPaymentByid(@PathVariable("id") Long id){
        return restTemplate.getForObject(BASE_URL+"/payment/get/"+id,CommonResult.class);
    }
}

7.在run dashboard启动两个模块

8.支付模块调用--》消费者模块调用


原文地址:https://www.cnblogs.com/lyx666/p/12745406.html