-
启动Consul
下载Consul https://www.consul.io/downloads
在consul.exe路径下,启动conusl
命令:consul agent -dev
-
客户端程序
添加依赖 pom.xml<!-- SpringCloud consul server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.yml
# Springcloud 微服务注册进Consul # 启动Consul ,进入consul.exe目录:consul agent -dev # 查看Consul首页: http://localhost:8500/ server: port: 8006 spring: application: name: consul-provider-payment cloud: # consul 注册中心地址 consul: host: localhost port: 8500 discovery: #hostname: 127.0.0.1 service-name: ${spring.application.name}
启动类
@SpringBootApplication @EnableDiscoveryClient public class ConsulPayment8006Application { public static void main(String[] args) { SpringApplication.run(ConsulPayment8006Application.class, args); } }
测试类
@RestController public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping("/payment/consul") public String paymentTestConsul(){ return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString(); } }
启动主程序
-
测试
SpringCloud注册进Consul
最新推荐文章于 2024-04-15 11:30:53 发布