An example of using docker compose to build a consumer cluster environment

Time:2021-12-9

Basic concepts of consumer

Server mode and client mode
Server mode and client mode are the types of consumer nodes; Client is not a user client.

  • The server mode provides data persistence.
  • The client mode does not provide persistence, and in fact it does not work. It just forwards the user’s client’s request to the server mode node. Therefore, the node in client mode can be imagined as lb (load balance), which is only responsible for request forwarding.
  • Usually, multiple nodes in server mode need to be configured, such as 3 and 5. There is no limit to the number of client mode nodes.

Command line parameters for server mode startup

  • -Server: indicates the current server mode; If not specified, it indicates client mode.
  • -Node: Specifies the name of the current node in the cluster.
  • -Config dir: specify the path of the configuration file and define the of the service; All. JSON terminated files under the path are accessed; The default value is: / consumer / config.
  • -Data dir: the directory where consumer stores data; The default value is: / consumer / data.
  • -Datacenter: the name of the data center. The default value is DC1.
  • -UI: use the Web UI interface that comes with consumer.
  • -Join: join to an existing cluster.
  • -Enable script checks: check whether the service is active, similar to starting heartbeat.
  • -Bind: bind the IP address of the server.
  • -Client: the client can access IP. The default value is “127.0.0.1”, that is, only loopback connection is allowed.
  • -Bootstrap expect: the expected number of server nodes in a data center. When the consumer starts, it will wait until the server reaches this number before booting the whole cluster. The value of this parameter must be consistent on all server nodes of the same datacenter.

Here’s another parameter – bootstrap, which controls whether a server is running in bootstrap mode: when a server is in bootstrap mode, it can elect itself as a leader; Note that only one server in a data center can be in bootstrap mode. Therefore, this parameter can only be used in the development environment with only one server. It cannot be used in the cluster product environment with multiple servers. Otherwise, if multiple servers mark themselves as leaders, the data will be inconsistent. In addition, this tag cannot be specified at the same time as – bootstrap expect.

Use docker compose to build the following consumer cluster environment

  • The cluster contains three servers: node1, node2 and node3
  • The cluster contains a client: node4; And provide Web UI access services on the client

Edit the docker-compose.yml file


version: '2'
networks:
  byfn:
 
services:
  consul1:
    image: consul
    container_name: node1
    command: agent -server -bootstrap-expect=3 -node=node1 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    networks:
      - byfn
 
  consul2:
    image: consul
    container_name: node2
    command: agent -server -retry-join=node1 -node=node2 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - byfn
 
  consul3:
    image: consul
    container_name: node3
    command: agent -server -retry-join=node1 -node=node3 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1
    depends_on:
        - consul1
    networks:
      - byfn
 
  consul4:
    image: consul
    container_name: node4
    command: agent -retry-join=node1 -node=ndoe4 -bind=0.0.0.0 -client=0.0.0.0 -datacenter=dc1 -ui 
    ports:
      - 8500:8500
    depends_on:
        - consul2
        - consul3
    networks:
      - byfn

Start service


$ docker-compose up
$ docker exec -t node1 consul members
Node   Address          Status  Type    Build  Protocol  DC   Segment
node1  172.21.0.2:8301  alive   server  1.4.0  2         dc1  <all>
node2  172.21.0.4:8301  alive   server  1.4.0  2         dc1  <all>
node3  172.21.0.3:8301  alive   server  1.4.0  2         dc1  <all>
ndoe4  172.21.0.5:8301  alive   client  1.4.0  2         dc1  <default>

visit http://127.0.0.1:8500

Registration configuration center example

spring:
  application:
    name: cloud-payment-service
  ####Consumer registry address
  cloud:
    consul:
      enabled: true
      host: 127.0.0.1
      port: 8500
      discovery:
        hostname: 127.0.0.1
        prefer-ip-address: true
        service-name: ${spring.application.name}
        #healthCheckInterval: 15s
        instance-id: ${spring.application.name}-8002
        enabled: true

Examples of kV access


$ docker exec -t node4 consul kv put foo "Hello foo"
$ docker exec -t node4 consul kv put foo/foo1 "Hello foo1"
$ docker exec -t node4 consul kv put foo/foo2 "Hello foo2"
$ docker exec -t node4 consul kv put foo/foo21 "Hello foo21"
$ docker exec -t node4 consul kv get foo
Hello foo
$ docker exec -t node4 consul kv get -detailed foo/foo1
CreateIndex      124
Flags            0
Key              foo/foo1
LockIndex        0
ModifyIndex      124
Session          -
Value            Hello foo1
$ docker exec -t node4 consul kv get -keys -separator="" foo
foo
foo/foo1
foo/foo2
foo/foo2/foo21
$ docker exec -t node4 consul kv get not-a-real-key
Error! No key exists at: not-a-real-key

The above is the details of using docker compose to build a consumer cluster environment. For more information about docker compose cluster environment, please pay attention to other related articles of developeppaer!