一、下载启动
官网下载地址:https://developer.hashicorp.com/consul/downloads
解压启动:在解压位置打开cmd窗口 执行consul agent -dev
结果:
二、服务整合
1、添加pom依赖
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
2、添加配置
spring:
application:
name: consul-provider-payment
####consul注册中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
3、添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
4、编写业务类
@RestController
@Slf4j
public class PaymetnController
{
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/consul")
public String paymentConsul()
{
return "springcloud with consul: "+serverPort+"\t "+ UUID.randomUUID().toString();
}
}
5、注册结果
6、测试结果