1.docker启动consul
下载安装docker以后运行如下代码:
docker run -d -p 8500:8500/tcp --name consul consul agent -server -ui -bootstrap-expect=1 -client=0.0.0.0
如果启动无效,关闭你的防火墙或者开放端口.
2.和SpringBoot集成
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
把maven地址和依赖管理加入到SpringBoot中pom文件中
在yum文件cloud下添加如下代码
spring.cloud.consul.host=你的consul地址
spring.cloud.consul.prot=你的端口号
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.heartbeat.enabled=true
spring.cloud.consul.discovery.health-check-path=health (必须写这个)
在你的启动类上添加发现注解
@EnableDiscoveryClient