Spring Cloud: Use Consul to replace Eureka

Source: Internet
Author: User
Tags unique id

Eureka has officially announced that it is no longer maintaining the project since 2.0 and has released a scare on the GitHub project wiki:

The main idea is: from the 2.x, the official will not continue to develop, if you need to use 2.x, at your own risk. But in fact I think the problem is not big, Eureka current features are very stable, even if not upgraded, service registration/discovery of these features are sufficient.

If you want to find alternatives, it is recommended to use more rich consul, in addition to service registration, Discovery, Consul also provides K-V storage and other functions, Consul's official website for other similar software also made a detailed comparison, see Consul vs other Software, Interested can see, especially there is a sentence, translated into plain English is: I am not for everyone here, I want to say that other than me are slag residue (it is evident that it is quite confident!). )

To get to the point, first look at Consul deployment installation:

First, cluster planning

Consul uses the agent to run, similar to the Elk Logstash Agent or Zabbix Monitoring System Agent, each need to be found on the service, through the Consul agent client to collect information about the service itself, and then to the consul agent Server reporting, Consul server can be deployed in a cluster.

Plan:

Serial number Node IP Node name Role
1 10.0.xx.55 Server1 Server
2 10.0.xx.203 Server2 Server
3 10.0.xx.204 Server3 Server
4 10.0.xx.205 Client1 Client & Web UI

This is planned according to the formal production environment, if the native development, there is a convenient dev mode (later on). In the table above, we plan to build the consul server cluster of 3 server nodes, plus 1 clients, impersonate the client and act as the consul Web Admin UI (Admin interface).

Second, download the installation

At present, the highest version of Consul is 1.2.0, only need to download the corresponding release compression package to the machine to extract.

2.1wget Https://releases.hashicorp.com/consul/1.2.0/consul_1.2.0_linux_amd64.zip2.2Unzip consul_1.2.0_linux_amd64.zip assumptions are extracted to the ~/consul/bin directory, after decompression will get 1 executable files called Consul2.3sudo cp./consul/usr/local/bin for convenience, you can copy it to/usr/local/bin (this step is optional, root is required) and check to see if the installation is successful:
?  ~ Consul Versionconsul V1.2.0protocol 2 spoken by default, understands 2 to 3 (agent would automatically use protocol >2 When speaking to compatible agents)

If the version output appears, it is ok (4 nodes, each machine is repeated above, all installed)

Third, start

3.1 Starting the server side

The basic commands are:

Consul agent-server-bind=10.0.xx.55-client=0.0.0.0-bootstrap-expect=3-data-dir=/data/application/consul_data/- Node=server1

Looking at a whole bunch of parameters, it's not complicated.

-server indicates that it is started as a service-side identity

-bind indicates which IP to bind to (some servers bind multiple NICs, which can be forced to specify the bound IP via the bind parameter)

-client Specifies the IP that the client accesses (consul has a rich API interface, where the client refers to the browser or caller), 0.0.0.0 represents an unlimited client IP

-bootstrap-expect=3 indicates that the minimum number of nodes in the server cluster is 3, below which the value will not work (note: Like zookeeper, usually the number of clusters is odd, convenient for elections, Consul is raft algorithm)

-data-dir indicates the storage directory for the specified data (the directory must exist)

-node represents the name that the node displays in the Web UI

After successful startup, the terminal window does not close, can be in the browser, access, similar to Http://10.0.xx.55:8500/, normal, you should see a line of text: Consul Agent.

To prevent the terminal from shutting down after consul exits, you can add something to the command just now, similar to the following:
nohup xxx >/dev/null 2>&1 &

That

Nohup Consul Agent-server-bind=10.0.xx.55-client=0.0.0.0-bootstrap-expect=3-data-dir=/data/application/consul_ data/-node=server1 >/dev/null 2>&1 &

To run it in the background.

On the other 2 nodes, similar operations are done:

Nohup Consul Agent-server-bind=10.0.xx.203-client=0.0.0.0-bootstrap-expect=3-data-dir=/data/application/consul_ data/-node=server2 >/dev/null 2>&1 &

Note Change the IP of the bind parameter and the node name in the nodes parameter.

Nohup Consul Agent-server-bind=10.0.xx.204-client=0.0.0.0-bootstrap-expect=3-data-dir=/data/application/consul_ data/-node=server3 >/dev/null 2>&1 &

3.2 Start client side

Almost exactly the same, just remove the-server and run on the 10.0.xx.205:

Nohup Consul agent-client=0.0.0.0-data-dir=/data/application/consul_data/-node=client1  -ui  >/dev/null 2>&1 &

Iv. Formation of Cluster

Now we have 3 server node + 1 client node, but these 4 nodes are independent and can be run on either node:

Consul members

You can see that only the information of the node itself.

To add yourself to the cluster, you can run the following command (assuming: the other 3 nodes are joined 10.0.xx.55)

Consul Join 10.0.xx.55

After success, the output will be:

Successfully joined cluster by contacting 1 nodes.

The other 2 nodes (referred to as nodes other than 10.0.xx.55) are joined in the cluster, and can be verified again after completion

You can see the information for all 4 nodes.

Tips: If in turn, to remove 1 nodes from the cluster, you can run Consul leave on that node.

V. Web UI

10.0.xx.205, may have the classmate noticed, when start Consul, we add a-ui parameter, this represents will launch consul comes with the Web management interface, Access Http://10.0.xx.205:8500/ui

You can see the information for each node.

VI. Service Registration/Discovery/deregistration

In services, there are no other services registered in the service except Consul. You can manually register a service through the API:

With postman (or other REST API tools, curl), send the following json,http to the http://10.0.xx.55:8500/v1/agent/service/register method is specified as put, Content-type is specified as Application/json

{    "ID": "My-service-id",    "Name": "My-service-name",    "Tags": [        "release=1",        "mytag=xyz"    ],    "Address": "192.168.1.1",    "Port": 8000,    "Meta": {        "my_version": "4.0"    },    " Enabletagoverride ": false,    " Check ": {        " deregistercriticalserviceafter ":" 90m ",        " HTTP ":"// yjmyzz.cnblogs.com/",        " Interval ":" 10s "    }}

The name of the parameter can be guessed from the meaning, it is not explained in detail, want to delve into, suggest to see the Consul API documentation. After the send is complete, look at the Web UI

will be able to see the newly registered service My-service-name, especially to mention that there are metadata-map in Tags,eureka can provide metadata, consul the corresponding features for tags.

In addition to the Web UI, you can discover the details of the service through the rest interface:

Http://10.0.21.55:8500/v1/agent/services This rest API can list all services:

{"    My-service-id": {        "Kind": "",        "id": "My-service-id",        "service": "My-service-name",        "Tags": [            "Release=1",            "mytag=xyz"        ],        "Meta": {            "my_version": "4.0"        },        "Port": 8000,        " Address ":" 192.168.1.1 ",        " Enabletagoverride ": false,        " CreateIndex ": 0,        " Modifyindex ": 0,        " Proxydestination ":",        "Connect": null    }}

Service discovery, in fact, is implemented through this API, there is a service registration will have the reverse Operation: Service logoff. The same is done through the API

Curl-x PUT http://10.0.21.55:8500/v1/agent/service/deregister/My-service-id

So I'm going to write off the My-service-id service.

Vii. Dev Developer mode

In front of this a burst of tossing requires several machines, this machine debugging development is not very convenient, for this consul thoughtful to provide the dev mode, the use of the way is very simple

Consul Agent-dev

Development mode, with the Web UI, directly http://localhost:8500/can, very convenient.

Viii. integration with the Spring-cloud

The front cushion, in fact, is to integrate with Spring-cloud, very simple:

Compile (' org.springframework.cloud:spring-cloud-starter-consul-discovery ')

Add a dependency on Org.springframework.cloud:spring-cloud-starter-consul-discovery

The following nodes are then configured in APPLICATION.YML:

Spring: ...  Cloud:    Consul:      host:127.0.0.1      port:8500      discovery:        tags:version=1.0,author=yjmyzz        Healthcheckpath:/info.json        healthcheckinterval:5s        instanceId: ${spring.application.name}:${ Vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

Note:

Typically on a microservices machine that needs to be deployed, it is agreed to install the Consul agent, so here the host is usually designated as 127.0.0.1 (native Test consul can be started with dev mode);

Tags equivalent to Eureka in the Metadata-map, everyone according to the actual need to set

Healthcheckpath is a URL for health checks that can be configured to/health or other URLs that can detect the running state of a micro service

Healthcheckinterval time interval for Healthcheck

Instanceid This long list, Spring Cloud official website document recommendation, in order to guarantee to generate a unique ID, can also be replaced by

${spring.application.name}:${spring.cloud.client.ipaddress}

(That is, end with IP), this instanceid is the service ID in the Consul service list

Finally, to remind you: if you use Consul to replace Eureka, and your project is dependent on the Eureka jar package, it is best to remove the Eureka automatic configuration from the startup class, refer to the following:

Reference Documentation:
1, https://www.consul.io/api/index.html

2, https://www.consul.io/intro/getting-started/install.html

3, Https://cloud.spring.io/spring-cloud-static/spring-cloud-consul/1.3.3.RELEASE/multi/multi_ Spring-cloud-consul-discovery.html

Spring Cloud: Use Consul to replace Eureka

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.