In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to deal with the problem of Consul multi-instance registration in Spring Cloud Finchley. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction to consul
Consul has the following properties:
Service Discovery: consul registers services through http, and services and services interact with each other.
Service health monitoring
Key/value storage
Multiple data centers
Consul can run on machines such as mac windows linux.
As Spring Cloud's support for Etcd has not been able to come out of the incubator, so at present, most users are still using Eureka and Consul. Before, because Eureka 2.0 is not open source news, coupled with some eye-catching headline party media, the number of Eureka users has decreased, so I believe that among the users who choose Spring Cloud, many users will choose Consul to do service registration and discovery.
This article will talk about the most serious pitfall when we use the latest Finchley version of Spring Cloud + Consul 1.2.x: the problem of multi-instance registration.
Problem interpretation
Problem: this problem may not necessarily be found during the development phase, but when multiple instances are deployed online, you will find that there is only one instance in the Consul.
Cause: the main reason for this problem is that when Spring Cloud Consul registers, the instance name (InstanceId) uses the value of "service name-port number" (that is, {spring.application.name}-{server.port}). You can see that if the instance name does not change the port number, the instance name will be the same. If you are familiar with Spring Cloud Consul, you may ask why there is no such problem in the old version. This is mainly due to the change in Consul's criteria for instance uniqueness. In the old version of Consul, if the instance name is the same, but the service address is different, it will still be considered as a different instance. In Consul 1.2.x, the service instance name becomes the unique identity in the cluster, which leads to the above problem.
Solution method
Now that we know the reason, we can aim at solving it. Here are two specific solutions:
Method 1: specify a new rule by configuring the property
As an example, the instance naming convention is configured directly through the spring.cloud.consul.discovery.instance-id parameter. Here, it is rough to organize instance names together by random numbers. Of course, this kind of organization is not good, because random numbers can still conflict, so you can also use more responsible rules to organize instance names.
Spring.cloud.consul.discovery.instance-id=$ {spring.application.name}-${random.int [10000999999]}
Method 2: reset the instance name by extending ConsulServiceRegistry
Since the ability to define instance names by configuring properties is limited, we want to be able to define them in a more flexible way. At this point we can modify it by overriding the register method of ConsulServiceRegistry. For example, the following implementation:
Public class MyConsulServiceRegistry extends ConsulServiceRegistry {public MyConsulServiceRegistry (ConsulClient client, ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) {super (client, properties, ttlScheduler, heartbeatProperties);} @ Override public void register (ConsulRegistration reg) {reg.getService (). SetId (reg.getService (). GetName () + "+ reg.getService (). GetAddress () +" + reg.getService (). GetPort ()); super.register (reg);}}
The above constructs an absolutely unique instance name by concatenating "service name", "ip address" and "port number", so that each service instance can be correctly registered with Consul.
Thank you for reading! This is the end of this article on "how to deal with Consul multi-instance registration in the Spring Cloud Finchley version". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.