一、consul 简介
consul 具有以下性质:
服务发现:consul通过http 方式注册服务,并且服务与服务之间相互感应。
服务健康监测
key/value 存储
多数据中心
consul可运行在mac windows linux 等机器上。Spring Cloud大型企业分布式微服务云架构源码请加一七九一七四三三八零
二、consul安装
linux
$ mkdir -p $GOPATH/src/github.com/hashicorp && cd $!
$ git clone https:// github. com/ hashicorp/ consul.git
$ cd consul
$ make bootstrap
$ make bootstrap
windows下安装:
见consul怎么在windows下安装
三、构建工程
构建一个consul-miya的springboot工程,导入依赖pring-cloud-starter-consul-discovery,其依赖文件:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.forezp
consul-miya
0.0.1-SNAPSHOT
jar
consul-miya
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-consul-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
在其入口文件ConsulMiyaApplication加入注解@EnableDiscoveryClient,开启服务发现:
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulMiyaApplication {
@RequestMapping("/hi")
public String home() {
return "hi ,i'm miya";
}
public static void main(String[] args) {
new SpringApplicationBuilder(ConsulMiyaApplication.class).web(true).run(args);
}
}
在其配置文件application.yml指定consul服务的端口为8500:
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
healthCheckPath: ${management.contextPath}/health
healthCheckInterval: 15s
instance-id: consul-miya
application:
name: consul-miya
server:
port: 8502
启动工程,访问localhost:8500,可以发现consul-miya被注册了。