部署Consul
- 服务器上先安装好docker以及docker-compose
- 将docker-consul.yml文件上传到服务器
version: '3'
services:
consul-server1:
image: consul:latest
restart: always
hostname: "consul-server1"
ports:
- "8500:8500"
- "53"
volumes:
- /dockervolume/consul/data1:/consul/data
- /dockervolume/consul/config1:/consul/config
- /dockervolume/consul/log1:/consul/log
command: "agent -server -bootstrap-expect 3 -ui -disable-host-node-id -client 0.0.0.0 -data-dir /consul/data -config-dir /consul/config"
consul-server2:
image: consul:latest
restart: always
hostname: "consul-server2"
ports:
- "8501:8500"
- "53"
volumes:
- /dockervolume/consul/date2:/consul/data
- /dockervolume/consul/config2:/consul/config
- /dockervolume/consul/log2:/consul/log
command: "agent -server -ui -join consul-server1 -disable-host-node-id -client 0.0.0.0 -data-dir /consul/data -config-dir /consul/config"
depends_on:
- consul-server1
consul-server3:
image: consul:latest
restart: always
hostname: "consul-server3"
ports:
- "8502:8500"
- "53"
volumes:
- /dockervolume/consul/data3:/consul/data
- /dockervolume/consul/config3:/consul/config
- /dockervolume/consul/log3:/consul/log
command: "agent -server -ui -join consul-server1 -disable-host-node-id -client 0.0.0.0 -data-dir /consul/data -config-dir /consul/config"
depends_on:
- consul-server1
- 在该文件所在目录执行以下语句自动创建三个consul节点
docker-compose -f docker-consul.yml up -d
- 在三个节点本地挂载目录config1,config2,config3文件夹下放置同一个acl.json 文件
{
"acl_datacenter": "dc1",
"acl_master_token": "这里放置自己生成的uuid",
"acl_default_policy": "deny"
}
- 重启三个consul节点
- 用postman访问以下地址
PUT请求
请求头:X-Consul-Token acl.json文件里的uuid
请求路径:http://ip:8500/v1/acl/create
请求body:{
"Name": "Agent Token",
"Type": "client",
"Rules": "node \"\" { policy = \"write\" } service \"\" { policy = \"read\" }"
}
- 此时可以使用acl.json里面的uuid来登录查看consul并且创建其他的token
springboot项目集成consul
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
spring:
application:
name: 项目名称
cloud:
consul:
host: ip地址
port: 端口号
discovery:
acl-token: consul里面的token