springcloud-服务提供者注册进consul
1.创建服务提供者模块
2.引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>cn.aib.springcloud</groupId>
<artifactId>springclud-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.改配置
server: port: 8006 # 服务别名 spring: application: name: consul-provider-payment cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
4.主启动
@SpringBootApplication @EnableDiscoveryClient public class PaymentConsulApplication8006 { public static void main(String[] args) { SpringApplication.run(PaymentConsulApplication8006.class, args); } }
5.写业务类
@RestController public class consulController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "/payment/consul") public String paymentconsul(){ return "springcloud with consul: " + serverPort + "\t" + UUID.randomUUID().toString(); } }
6.测试