dawdler-config-center-consul-core
Module Introduction
The core module of the unified configuration center
1. dawdler-config.yml configuration file
Configures different types of configuration centers and their related settings. Currently only consul is supported, and custom extensions are supported.
dawdler-config.yml supports multi-environment configuration. See Unified configuration center and multi-environment support.
consul:
host: localhost
port: 8500
separator:
token:
wait-time: 10000
watch-keys:
- /orderConfig
- /user
#TLSConfig:
#keyStoreInstanceType: JKS, JCEKS, PKCS12, PKCS11, DKS
#certificatePath:
#certificatePassword:
#keyStorePath:
#keyStorePassword:
host: the ip of the consul server
port: the port exposed by the consul server
separator: the separator. Usually it does not need to be configured; it is only meaningful in the keys scenario, such as the following request:
Assuming the directories /config/config-uat /config/config-dev /config/config currently exist
visiting http://localhost:8500/v1/kv/config?keys&separator=-&wait=5s&index=2 returns [
"config/config", "config/config-" ].
token: used for authentication
wait-time: the polling timeout duration, in milliseconds. The wait parameter of blocking queries is passed to consul in seconds and clamped to 1-600 seconds (the consul server has a limit of about 10 minutes for blocking queries)
watch-keys: the keys to watch. It is a list, and the related configuration is refreshed only when a watched key changes. You can also use / to watch all keys.
TLSConfig: certificate-related configuration. If there is sensitive data and consul needs to be deployed on the Internet, this configuration is recommended. For details, see: consul encryption
2. Installing consul
Refer to consul downloads to complete the installation.
2.1 Using consul in a production environment
Consul supports multiple DataCenters, and data centers can be interconnected through the Internet. To improve communication efficiency, only Server nodes join the cross-data-center communication.
Within a single data center, Consul nodes fall into two types, Client and Server (all nodes are called Agents). Server nodes store data, and Clients are responsible for health checks and forwarding data requests to Servers. Among Server nodes there is one Leader and multiple Followers; the Leader node synchronizes data to the Followers. The recommended number of Servers is 3 or 5, and when the Leader goes down an election mechanism is started to produce a new Leader.
Consul nodes within a cluster maintain membership through the gossip protocol, so a node knows which nodes currently exist in the cluster and whether these nodes are Clients or Servers. The gossip protocol within a single data center uses both TCP and UDP communication, both on port 8301. Cross-data-center gossip also uses both TCP and UDP communication, on port 8302.
Read and write requests for data within the cluster can be sent directly to a Server, or forwarded to a Server through a Client via RPC. Requests eventually reach the Leader node. When slight data staleness is acceptable, read requests can also be completed on an ordinary Server node. Both read/write and replication of data within the cluster are done over TCP port 8300.
Note: consul only exposes the LAN ip. If you use a configuration center that needs a ui for operation, you can use nginx to expose an external IP and forward it to the LAN IP exposed by consul, and use auth_basic to authenticate and authorize access to the ui.
2.1.1 nginx load balancing for a multi-server-node cluster
In a production environment consul generally uses cluster mode; you can set up cluster mode by referring to the official documentation. It is recommended to use nginx to load balance multiple cluster nodes.
The call relationship for service registration (the client side of the configuration center also uses this approach) is as follows:
service->nginx (multiple nginx+lvs can be considered to ensure high availability)->(load balancing multiple)consul_server
2.1.2 A single client connecting to a cluster of server nodes
The call relationship for service registration (the client side of the configuration center also uses this approach) is as follows:
service->consul_client->consul_server
2.2 Configuration and startup for a single-machine test environment
consul agent -data-dir=/data/consul/data -server -bootstrap-expect 1 -bind=0.0.0.0 -client=0.0.0.0 -ui &
dev: used for the local development environment.
server: start as a server; the default is client.
data-dir: the directory where data is stored.
bind: the ip address to listen on; by default it binds to 0.0.0.0.
client: the ip address of the client; 0.0.0.0 exposes it externally so any ip can access it.
ui: enables the web management interface.
After startup, visit the consul management interface to verify success.
2.3 Cluster environment
Here is an example of a cluster with three servers:
# Configure three machines and start them
consul agent -server -bootstrap-expect 3 -data-dir=/data/consul/data -node=n1 -bind=192.168.43.131 -client=0.0.0.0 -ui&
consul agent -server -bootstrap-expect 3 -data-dir=/data/consul/data -node=n2 -bind=192.168.43.130 -client=0.0.0.0 -ui&
consul agent -server -bootstrap-expect 3 -data-dir=/data/consul/data -node=n3 -bind=192.168.43.137 -client=0.0.0.0 -ui&
# nodes n1 and n3 join n2; execute in 43.137 and 43.131 respectively
consul join 192.168.43.130
# you can view node status with consul members
Returned information is as follows:
Node Address Status Type Build Protocol DC Partition Segment
n1 192.168.43.137:8301 alive server 1.11.3 2 dc1 default <all>
n2 192.168.43.131:8301 alive server 1.11.3 2 dc1 default <all>
n3 192.168.43.130:8301 alive server 1.11.3 2 dc1 default <all>
Explanation of some parameters:
server: start as a server. The default is client
bootstrap-expect: the minimum number of servers required by the cluster; when below this number, the cluster becomes invalid
data-dir: the directory where data is stored; for more information please refer to the consul data synchronization mechanism
node: the node id; each node in the cluster must have a unique name. By default, Consul uses the machine's hostname
bind: the ip address to listen on. It binds to 0.0.0.0 by default and can be omitted. It indicates the address Consul listens on, and it must be accessible to other nodes in the cluster. Consul by default listens on the first private IP, but it is best to provide one. Servers on production machines usually have several network cards, so specifying one will not go wrong
client: the ip address of the client; 0.0.0.0 means no restriction on ip (without this, the ui :8500 below cannot be accessed)
ui: enables access to the UI interface
-config-dir specifies the configuration folder; Consul loads all files in it
-datacenter specifies the data center name; the default is dc1
2.4 Security
Be aware that consul should not expose ports to the Internet. For simple usage, you can use nginx Basic HTTP authentication for authorization and then reverse proxy to the consul port on the LAN.
It is recommended to use tokens for authentication. Tokens can be configured in consul's configuration file or managed through consul acl. consul supports ACL (Access Control List), which provides fine-grained control over consul's services, including read/write permissions, tokens, service registration, service discovery, etc.