1 consul安装
准备目录
mkdir -p /opt/consul/web
mkdir -p /opt/consul/data
mkdir -p /opt/consul/conf
mkdir -p /opt/consul/run/
每个阶段欺服务
consul agent -server -bootstrap -syslog \
-ui-dir=/opt/consul/web \
-data-dir=/opt/consul/data \
-config-dir=/opt/consul/conf \
-pid-file=/opt/consul/run/consul.pid \
-client=x.x.x.x \
-bind=x.x.x.x \
-node=hostname \
-disable-host-node-id
组成集群
consul join --http-addr x.x.x.x:8500 x.x.x.x
springboot集成consul
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
spring.cloud.consul.discovery.port=${server.port}
spring.cloud.consul.discovery.healthCheckUrl=url/health
spring.cloud.consul.discovery.healthCheckInterval=10s
consul-template nginx集成
nginx配置增加
include /usr/local/nginx/consul/*.conf;
nginx.ctmpl 文件
upstream backend1 {
{{range service "consul服务名称"}}
server {{ .Address }}:{{ .Port }};
{{ end }}
}
server {
server_name test1.xcar.com.cn;
location / {
proxy_pass http://backend1;
}
}
consul-tempalte命令
nohup consul-template --consul-addr x.x.x.x:8500 --template "/usr/local/nginx/consul/nginx.ctmpl:/usr/local/nginx/consul/vhost.conf:/usr/local/nginx/sbin/nginx -s reload" --log-level=info &