Consul small note

First, find out what is the service?

1, service discovery

If I need to use A service and its IP and port are stable, then I can call when the call directly to the REST service. So, when the network environment is stable, you want to call multiple services to clients as long as they know the network location can be, and past practice is to configure in the configuration file, and when the network environment changes, you need to change the configuration of each caller.

The client ------- IP + port ------> 1 server

------- IP + port ------> server 2

------- IP + port ------> server N

However, if the service can be found in the server network location information together, so that the client may want to call the name of the service, you can get their IP and port services.

------- ------- client service name> service discovery <--------- registered IP + port ----------- server N

Client <----- IP + port ------- service discovery

Why IP service discovery can save all the services side of the + port it? Because there is an internal service registry, which is a database of available service instances, service registry to provide management and query API API functions, service instances using the Administration API to register and unregister from the service registry, system components with the query API found examples of available services.

Characteristics service registry REST API is highly available and up to date, eureka approach is to provide a service for registering and querying instances, service instances using the POST request to register its network location, every 30s on the use PUT request to refresh their registration information. The consul using health monitoring.

2, how to register IP and port determined?

IP acquisition mode

The following are the main way to get an IP address:

  • Manual configuration requires registration of IP, which is to write the configuration file, of course, this method is not feasible to write dead in the micro-services, because the micro-level services are basically support the expansion of multi-machine deployment, this approach will bring luck Victoria on a lot of inconvenience.

  • To acquire by way of traversing the network card, first find an address for the local loopback IP address.

  • In some network planning better standardization in the room, you can manually specify the name of the NIC way to specify which network card corresponding to an IP address to register.

  • Establish a socket connection directly to the service registration center, by way of access to the local IP socket.getLocalAddress ().

Obtaining port

Obtaining the port, there is no standardized protocol.

  • If RPC applications, will have a startup configuration to specify a service listening port, the port registered can be used when the value of configuration items directly.

  • HTTP application of traditional WEB container provided, there will be a listening port configuration file to configure the container port value can be used to configure items directly during registration.

  • In SpringBoot framework for Java applications, by (SpringBoot version 1.x) EmbeddedServletContainerInitiallizedEvent.getEmbeddedServletContainer (). GetPort () to get.

Two, consul Introduction

1. What is the consul?

consul is a multi-distributed data centers can use high, for shared service discovery and configuration tools, the use of algorithms to ensure consistency Raft services, and support for health checks.

2, consul Key Features

  • Service discovery: For service information via DNS or HTTP.

  • Health check: the given service may be provided with any number of health checks associated with (e.g., cpu utilization status code or web).

  • The dynamic information may be configured by the storage class consul: K / V storage.

  • Multiple Data Center: Support multiple data centers, out of the box.

  • WEB UI: through web pages can now understand the operation of the service, at a glance.

3, consul of principle

 

Whether or FIG CLIENT SERVER are regarded as a node, consul is a cluster consisting of N and M CLIENT SERVER thereof.

  • CLIENT is consul of client mode. In this mode, all registered to the current node's service will be forwarded to the SERVER, itself is not persistent information.

  • SERVER is the consul of server mode, the same function and CILENT, but the only difference is that it will persist all the information locally, so it encounters an error information can be retained and used. SERVER node is also involved Raft, it maintains membership information, registration services, health checks and other functions.

  • SERVER-LEADER shows that it manages SERVER entire cluster, which is responsible for synchronization information to other registered SERVER, but also responsible for detecting the health of each node.

  • LAN Gossip pool contains all the nodes in the data center, eliminating the need to configure the server address to the client, it can be done automatically, and each node can be a node failure check, you can also broadcast the event.

  • WAN Gossip pool contains all the SERVER (does not include the CLIENT), the main servers located in different data centers, typically communicate via the Internet or a wide area network.

consul requires a cluster, usually three to five SERVER nodes, usually with a CLIENT of N Service. Service to the Consul through HTTP API or write directly Consul profile way, CLIENT can be considered stateless, registration information will be forwarded to the SERVER by RPC, the service information is stored on each node SERVER achieve strong by Raft consistency.

If the Program To access the Service on other servers, you can provide through the CLIENT to access the machine's HTTP API, the native CLIENT can forward the request to the SERVER, SERVER query to the current return after information Service eventually Program can be obtained Service all deployment information, which can then send a request to deploy a Service.

Third, health check

1, consul health check

consul in the agent refers to the node (either CLIENT or SERVER), consul providing Script / TCP / HTTP and Interval, and TTL, etc., but the consul health check mainly registered by the service of the Agent to handle. But there is a downside is that, after the discovery node consul hang up, state services become unusable.

2, consul and Zookeeper, etcd difference

  • Zookeeper node using the temporary mechanism to create a temporary service node service starts, services and nodes perish.

  • etcd use TTL mechanism to create key-value pair when the service starts business, regularly updated TTL, TTL expired service is unavailable.

Zookeeper and etcd of key-value store are strong consistency of key-value pairs are automatically synchronized to multiple nodes, as long as there is on a node can be considered a corresponding service is available.

Consul data synchronization is strong consistency of service registration information will be synchronized, compared SERVER ZK between nodes, etcd, service information is persistent, even if the service is not available to deploy, and can still query to the service deployment . But the state of business services available is determined by the registration to the Agent if the Agent does not work, you can not determine the true status of the service, of course, the Agent hung up, to take over the other Agent will not work, because then hang Agent this also shows the state of the server may not be very good, this time masked node function and what service is reasonable.

3, health check mode

Health Inspection Service is divided into client and server heartbeat active detection in two ways

1) The client heartbeat

  • The client sends regular intervals "heartbeat" way to indicate to the server service status of their normal heartbeat may be in the form of TCP, also in the form of HTTP.

  • Maintain client and server socket connection length, a client achieve their own heartbeat.

  • Zookeeper not actively send a heartbeat, but a temporary node dependent properties of the component itself provides to maintain the session Zookeeper temporary connection nodes.

But the client heartbeat, the heartbeat active and maintain a long connection of clients just show normal on the link, the service status is not necessarily normal.

2) active detection server

  • The server calls the service publisher HTTP interface to complete a health check.

  • For the HTTP service does not provide RPC application, the server call interface service publisher to complete the health check.

  • You can perform a comprehensive inspection by the form of a script.

But the service end proactive monitoring is also problematic. Service registry automatically call RPC service can not be done on an interface versatility; publishers to the network service is different from the service registry, in many scenarios, the server can not initiate a health check.

 

Four, consul security

As a result of consul gossip mechanism, the RPC system, HTTPS to provide functionality, different security mechanisms used by the two systems, which provide symmetric key encryption gossip, the RPC may be used to end the TLS client authentication, HTTPS is used client authentication to-end TLS.

I am here because of the skill is not enough, after leaving the pit fill.

 

reference:

Service Discovery - Introduction consul, deployment and use 

Micro-talk service registration and discovery services

 

Guess you like

Origin www.cnblogs.com/NYfor2018/p/11221055.html