In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the method of distributed service registration discovery and unified configuration management Consul. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Consul server is configured to use
Download the appropriate version and extract it and copy the executable file to the / usr/local/consul directory
Create a configuration file for service
Silence$ sudo mkdir / etc/consul.dsilence$ echo'{"service": {"name": "web", "tags": ["rails"], "port": 80}}'| sudo tee / etc/consul.d/web.json
Start the agent
Silence$ / usr/local/consul/consul agent-dev-node consul_01-config-dir=/etc/consul.d/-ui
The-dev parameter represents the startup of the local test environment; the-node parameter represents the custom cluster name; and the-config-drir parameter represents the directory of services's registration configuration files, that is, the folder created above-ui launches the web-ui management page that comes with it.
Query mode of cluster members
Silence-pro:~ silence$ / usr/local/consul/consul members
HTTP protocol data query
Silence-pro:~ silence$ curl http://127.0.0.1:8500/v1/catalog/service/web[ {"ID": "ab1e3577-1b24-d254-f55e-9e8437956009", "Node": "consul_01", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": {"lan": "127.0.0.1" "wan": "127.0.0.1"}, "NodeMeta": {"consul-network-segment": "}," ServiceID ":" web "," ServiceName ":" web "," ServiceTags ": [" rails "]," ServiceAddress ":"," ServicePort ": 80 "ServiceEnableTagOverride": false, "CreateIndex": 6, "ModifyIndex": 6}] silence-pro:~ silence$
Web-ui Management Consul Web UI
Consul's web-ui can be used to view service status, check cluster nodes, control access lists and set up KV storage systems, which is much easier to use than Eureka and ETCD,Consul 's web-ui. (Eureka and ETCD will be briefly introduced in the next article. )
Import and export of data stored in 7.KV
Silence-pro:consul silence$. / consul kv import @ temp.jsonsilence-pro:consul silence$. / consul kv export redis/
The content format of the temp.json file is as follows. Generally speaking, the saved file is exported after the configuration of the management page, and the file needs to be imported later.
[{"key": "redis/config/password", "flags": 0, "value": "MTIzNDU2"}, {"key": "redis/config/username", "flags": 0, "value": "U2lsZW5jZQQ ="}, {"key": "redis/zk/", "flags": 0 "value": "}, {" key ":" redis/zk/password "," flags ": 0," value ":" NDU0NjU= "}, {" key ":" redis/zk/username "," flags ": 0," value ":" ZGZhZHNm "}]
Consul's KV storage system is a tree node structure similar to zk, which is used to store relevant key/value key-value pair information. We can use KV storage system to implement the configuration center mentioned above, and store unified configuration information in KV storage system. It is convenient for each instance to obtain and use the same configuration. And after changing the configuration, each service can automatically pull the latest configuration, without the need to restart the service.
Consul Java client uses
Maven pom dependency increased, version can be changed freely
Com.orbitz.consul consul-client 0.12.3 org.springframework.boot spring-boot-starter-actuator
Consul basic utility class, extended as needed
Package com.coocaa.consul.consul.demo;import com.google.common.base.Optional;import com.google.common.net.HostAndPort;import com.orbitz.consul.*;import com.orbitz.consul.model.agent.ImmutableRegCheck;import com.orbitz.consul.model.agent.ImmutableRegistration;import com.orbitz.consul.model.health.ServiceHealth;import java.net.MalformedURLException;import java.net.URI;import java.util.List Public class ConsulUtil {private static Consul consul = Consul.builder () .withHostAndPort (HostAndPort.fromString ("127.0.0.1 Consul.builder 8500")) .build (); / * * Service Registration * / public static void serviceRegister () {AgentClient agent = consul.agentClient () Try {/ * Note the registration interface: * need to provide a health check service URL And how often do you visit the service (here is 3s) * / agent.register (8080, URI.create ("http://localhost:8080/health").toURL(), 3," tomcat "," tomcatID "," dev ") } catch (MalformedURLException e) {e.printStackTrace ();}} / * * Service acquisition * * @ param serviceName * / public static void findHealthyService (String serviceName) {HealthClient healthClient = consul.healthClient (); List serviceHealthList = healthClient.getHealthyServiceInstances (serviceName). GetResponse () ServiceHealthList.forEach ((response)-> {System.out.println (response);});} / * * Storage KV * / public static void storeKV (String key, String value) {KeyValueClient kvClient = consul.keyValueClient (); kvClient.putValue (key, value) } / * obtain value * / public static String getKV (String key) {KeyValueClient kvClient = consul.keyValueClient (); Optional value = kvClient.getValueAsString (key); if (value.isPresent ()) {return value.get ();} return "" according to key } / * find consistent nodes (should be all server nodes in the same DC) * / public static List findRaftPeers () {StatusClient statusClient = consul.statusClient (); return statusClient.getPeers ();} / * get leader * / public static String findRaftLeader () {StatusClient statusClient = consul.statusClient (); return statusClient.getLeader () } public static void main (String [] args) {AgentClient agentClient = consul.agentClient (); agentClient.deregister ("tomcatID");}}
3. The registration of services and the acquisition and storage of KV data can be realized through the above basic tool classes.
Consul cluster building
Three hosts Consul download and installation, I do not have a physical host here, so through three virtual machines to achieve. Virtual machine IP score 192.168.231.145192.168.231.146192.168.231.147
145,146hosts are started as Server mode, 147as Client mode, Server and Client are only for Consul clusters, and have nothing to do with the service!
Server mode starts 145, node name is set to N1, and data center is unified with dc1
[root@centos145 consul] #. / consul agent-server-bootstrap-expect 2-data-dir / tmp/consul-node=n1-bind=192.168.231.145-datacenter=dc1bootstrap_expect = 2: A cluster with 2 servers will provide no failure tolerance. See https://www.consul.io/docs/internals/consensus.html#deployment-tablebootstrap_expect > 0: expecting 2 servers== > Starting Consul agent...== > Consul agent running! Version: 'v1.0.1' Node ID: '6cc74ff7-7026-cbaa-5451-61f02114cd25' Node name:' N1 'Datacenter:' dc1' (Segment:') Server: true (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS:-1, DNS: 8600) Cluster Addr: 192.168.231.145 (LAN: 8301) WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false TLS-Incoming: false== > Log data will now stream in as it occurs: 2017-12-06 23:26:21 [INFO] raft: Initial configuration (index=0): [] 2017-12-06 23:26:21 [INFO] serf: EventMemberJoin: n1.dc1 192.168.231.145 2017-12-06 23:26:21 [INFO] serf: EventMemberJoin: N1 192.168.231.145 2017-12-06 23:26:21 [INFO] agent: Started DNS server 127.0.0 .1 udp 8600 (udp) 23:26:21 on 2017-12-06 [INFO] raft: Node at 192.168.231.145 raft 8300 [Follower] entering Follower state (Leader: ") 2017-12-06 23:26:21 [INFO] consul: Adding LAN server N1 (Addr: tcp/192.168.231.145:8300) (DC: dc1) 23:26:21 [INFO] consul: Handled member-join event for server" n1.dc1 "in Area "wan" 2017-12-06 23:26:21 [INFO] agent: Started DNS server 127.0.0.1 agent (tcp) 2017-12-06 23:26:21 [INFO] agent: Started HTTP server on 127.0.0.1 area 8500 (tcp) 2017-12-06 23:26:21 [INFO] agent: started state syncer 2017-12-06 23:26:28 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:26:30 [WARN] raft: no known peers Aborting election 2017-12-06 23:26:49 [ERR] agent: Coordinate update error: No cluster leader 2017-12-06 23:26:54 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:27:24 [ERR] agent: Coordinate update error: No cluster leader 2017-12-06 23:27:27 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:27:56 [ERR] agent: Coordinate update error: No cluster Leader 2017-12-06 23:28:02 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:28:27 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:28:33 [ERR] agent: Coordinate update error: No cluster leader
So far, only 145 has been started, so there is no cluster yet.
4.Server mode starts 146Node name is N2, and web-ui management page function is enabled on N2
[root@centos146 consul] #. / consul agent-server-bootstrap-expect 2-data-dir / tmp/consul-node=n2-bind=192.168.231.146-datacenter=dc1-uibootstrap_expect = 2: A cluster with 2 servers will provide no failure tolerance. See https://www.consul.io/docs/internals/consensus.html#deployment-tablebootstrap_expect > 0: expecting 2 servers== > Starting Consul agent...== > Consul agent running! Version: 'v1.0.1' Node ID: 'eb083280-c403-668f-e193-60805c7c856a' Node name:' N2 'Datacenter:' dc1' (Segment:'') Server: true (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS:-1, DNS: 8600) Cluster Addr: 192.168.231.146 (LAN: 8301) WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false TLS-Incoming: false== > Log data will now stream in as it occurs: 2017-12-06 23:28:30 [INFO] raft: Initial configuration (index=0): [] 2017-12-06 23:28:30 [INFO] serf: EventMemberJoin: n2.dc1 192.168.231.146 2017-12-06 23:28:31 [INFO] serf: EventMemberJoin: N2 192.168.231.146 2017-12-06 23:28:31 [INFO] raft: Node at 192.168.231. 146 Follower 8300 [Follower] entering Follower state (Leader: ") 23:28:31 on 2017-12-06 [INFO] consul: Adding LAN server N2 (Addr: tcp/192.168.231.146:8300) (DC: dc1) 2017-12-06 23:28:31 [INFO] consul: Handled member-join event for server" n2.dc1 "in area" wan "2017-12-06 23:28:31 [INFO] agent: Started DNS server 127.0.0.1: 8600 (tcp) 23:28:31 on 2017-12-06 [INFO] agent: Started DNS server 127.0.0.1 udp) 2017-12-06 23:28:31 [INFO] agent: Started HTTP server on 127.0.0.1 agent 8500 (tcp) 2017-12-06 23:28:31 [INFO] agent: started state syncer 2017-12-06 23:28:38 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:28:39 [WARN] raft: no known peers Aborting election 2017-12-06 23:28:57 [ERR] agent: Coordinate update error: No cluster leader 2017-12-06 23:29:11 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:29:30 [ERR] agent: Coordinate update error: No cluster leader 2017-12-06 23:29:38 [ERR] agent: failed to sync remote state: No cluster leader 2017-12-06 23:29:57 [ERR] agent: Coordinate update error: No cluster leader
Similarly, there is no cluster found, at this time N1 and N2 are started, but each other does not know the existence of the cluster!
5. Add N1 node to N2
[silence@centos145 consul] $. / consul join 192.168.231.146
At this time, both N1 and N2 print and find the log information of the cluster.
6. At this time, N1 and N2 are already Server mode nodes in a cluster.
7.Client mode initiates 147
[root@centos147 consul] #. / consul agent-data-dir / tmp/consul-node=n3-bind=192.168.231.147-datacenter=dc1== > Starting Consul agent...== > Consul agent running! Version: 'v1.0.1' Node ID: 'be7132c3-643e-e5a2-9c34murcad99063a30e' Node name:' n3' Datacenter: 'dc1' (Segment:'') Server: false (Bootstrap: false) Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS:-1, DNS: 8600) Cluster Addr: 192.168.231.147 (LAN: 8301) WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false TLS-Incoming: false== > Log data will now stream in as it occurs: 23:36:46 on 2017-12-06 [INFO] serf: EventMemberJoin: N3 192.168.231.147 2017-12-06 23:36:46 [INFO] agent: Started DNS server 127.0.0.1 agent (udp) 23:36:46 on 2017-12-06 [INFO] agent: Started DNS server 127.0.0.1 INFO 8600 (tcp) 23:36:46 on 2017-12-06 [INFO] Agent: Started HTTP server on 127.0.0.1 INFO 8500 (tcp) 2017-12-06 23:36:46 [INFO] agent: started state syncer 2017-12-06 23:36:46 [WARN] manager: No servers available 2017-12-06 23:36:46 [ERR] agent: failed to sync remote state: No known Consul servers 2017-12-06 23:37:08 [WARN] manager: No servers available 2017-12-06 23:37:08 [ERR] agent: failed to Sync remote state: No known Consul servers 2017-12-06 23:37:36 [WARN] manager: No servers available 2017-12-06 23:37:36 [ERR] agent: failed to sync remote state: No known Consul servers 2017-12-06 23:38:02 [WARN] manager: No servers available 2017-12-06 23:38:02 [ERR] agent: failed to sync remote state: No known Consul servers 2017-12-06 23:38:22 [WARN] manager: No servers available 2017/ 12/06 23:38:22 [ERR] agent: failed to sync remote state: No known Consul servers
8. Add node N3 to the cluster on n3
[silence@centos147 consul] $. / consul join 192.168.231.145
9. View the cluster node information again
10. At this time, the three-node Consul cluster has been successfully built! In fact, N1 and N2 are started in Server mode, and N2 is started in Client mode.
11. The main difference between Server mode and Client mode of Consul is that a Consul cluster controls the number of Server nodes in the cluster through the startup parameter-bootstrap-expect, the nodes in Server mode maintain the state of the cluster, and if a Server node exits from the cluster, it will trigger the Leader re-election mechanism to re-elect a Leader; among the remaining Server mode nodes, while the nodes in Client mode are free to join and exit.
twelve。 Start web-ui in N2
These are all the contents of the article "distributed Service Registration Discovery and Unified configuration Management Consul". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.