docker run -p 8500:8500 \
-v /Users/xx/Documents/docker/consul/consul_server1/data:/consul/data \
-e CONSUL_BIND_INTERFACE='eth0' \
--name=consul-server1 \
--hostname=consul-server1 \
consul agent \
-server \
-bootstrap-expect=1 \
-node=consul-server1 \
-data-dir=/consul/data \
-client 0.0.0.0 \
-ui
docker run \
-v /Users/xx/Documents/docker/consul/consul_server2/data:/consul/data \
-e CONSUL_BIND_INTERFACE='eth0' \
--name=consul-server2 \
--hostname=consul-server2 \
consul agent \
-server \
-bootstrap-expect=1 \
-node=consul-server2 \
-data-dir=/consul/data \
-client 0.0.0.0 \
-ui \
-join 172.17.0.2
--name
docker容器名称
--hostname
docker容器hostname
agent
表示启动 Agent 进程。
-server
表示启动 Consul Server 模式。
-client
表示启动 Consul Cilent 模式。0.0.0.0
表示所有地址都可以访问。
-bootstrap
表示这个节点是 Server-Leader ,每个数据中心只能运行一台服务器。技术角度上讲 Leader 是通过 Raft 算法选举的,但是集群第一次启动时需要一个引导 Leader,在引导群集后,建议不要使用此标志。
-bootstrap-expect=2
当 server 数量达到 3 个之前 consul 是不会引导集群的,当然也不会选出某一个 leader
-ui
表示启动 Web UI 管理器,默认开放端口 8500,所以上面使用 Docker 命令把 8500 端口对外开放。
-node
节点的名称,集群中必须是唯一的。
-join
表示加入到某一个集群中去。 如:-join=192.168.1.23
cd C:\develop\microservice\consul_server1
consul agent ^
-server ^
-bootstrap ^
-node=consul-server1 ^
-data-dir=.\data ^
-config-dir=.\conf ^
-client=0.0.0.0 ^
-ui
cd C:\develop\microservice\consul_server2
consul agent ^
-server ^
-node=consul-server2 ^
-data-dir=.\data ^
-config-dir=.\conf ^
-client=0.0.0.0 ^
-ui ^
-join 127.0.0.1:8301 ^
{
"ports": {
"dns": 8600,
"http": 8500,
"https": -1,
"grpc": 8502,
"serf_lan": 8301,
"serf_wan": 8302,
"server": 8300
},
"services": [
{
"checks": [
{
"tcp": ":8081",
"interval": "5s",
"timeout": "1s"
}
],
"id": "api_server1",
"name": "api_server",
"port": 8081,
"tags": ["urlprefix-/greeting"]
},
{
"checks": [
{
"tcp": ":8082",
"interval": "5s",
"timeout": "1s"
}
],
"id": "api_server2",
"name": "api_server",
"port": 8082,
"tags": ["urlprefix-/greeting"]
}
]
}
job "api_server" {
datacenters = ["dc1"]
type = "service"
group "api_server" {
count = 1
task "api_server1" {
driver = "java"
config {
jar_path = "C:\\develop\\microservice\\repo\\springboot-sample-0.1.0.jar"
jvm_options = [
"-Dserver.port=8081",
"-Dapp.consul.discovery.service-name=api_server",
"-Dapp.consul.discovery.service-id=api_server1"
]
}
resources {
cpu = 500
memory = 256
network {
mbits = 10
port "http" {
static = 8081
}
}
}
service {
name = "api-1"
tags = ["urlprefix-/greeting"]
port = "http"
meta {
meta = "meta for api_server1."
}
check {
type = "tcp"
port = "http"
interval = "10s"
timeout = "2s"
}
}
}
restart {
attempts = 1
}
}
}
sudo docker run -p 8081:8081 \
-e PARAMS="-Dserver.port=8081 -Dapp.consul.discovery.enable=true -Dapp.consul.discovery.consul-host=172.17.0.2:8500 -Dapp.consul.discovery.service-id=service-A-1" \
service_a:0.1.0
sudo docker build -f Dockerfile-A-0.1.0 -t service_a:0.1.0 .
FROM nexus.jp.sbibits.com:8442/amazoncorretto:11.0.5
ADD springboot-sample-0.1.0.jar springboot-sample.jar
ENV PARAMS=""
ENTRYPOINT ["sh","-c","java $PARAMS -jar springboot-sample.jar"]