1. Docker 上安装consul, 并启动
https://blog.csdn.net/BogerPeng/article/details/123320030
2. provider 配置
2.1 pom.xml
参考笔记2 中的配置,再加上下面依赖
2.2 YAML
2.3 主启动类
2.4 Controller
3. consumer 端配置
3.1 pom.xml 同2.1
3.2 yaml
server:
port: 808
spring:
application:
name: cloud-consumerconsul-order
#### consul 注册中心地址
cloud:
consul:
host: 192.168.226.128
port: 8500
discovery:
service-name: ${spring.application.name}
3.3 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain808 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain808.class,args);
}
}
3.4 controller
@RestController
public class OrderConsulController {
private String CONSUL_URL = "http://consul-provider-payment";
@Resource
RestTemplate restTemplate;
@GetMapping("/order/consul")
public String getConsulPayment(){
return restTemplate.getForObject(CONSUL_URL+"/payment/consul",String.class);
}
}
测试成功: