In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to register micro-service to Zookeeper through Zookeeper's API. For this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
one。 Register with ZK
springboot project through the introduction of zk dependency, through annotations to register to zk, this online information is very much, not too long-winded, here is mainly given, through api registration to zk, what scenarios will be used? For example, if there are some springboot projects and some non-springboot projects in the environment, then springboot projects can be registered directly through annotations, other micro-services can be called through Feign, non-springboot projects can be annotated through api, and other services can be called through service names.
2. sprinboot project master needs to introduce related dependencies for registration of springboot project, which can be done through comments. Here is an one-off note: org.springframework.cloud spring-cloud-starter-zookeeper-discovery 1.1.3.RELEASE Note: @ EnableDiscoveryClient III. General Java project 1. Here I will take the springboot project as an example. However, the dependency in the second step is not introduced to implement registration and service invocation pom dependency org.apache.zookeeper zookeeper 3.4.11 pom Com.101tec zkclient 0.10 2. Introduction to the environment:
I first register two services to ZK, one zkClient and one provider, both of which are standard springboot projects. The component registration in the second step of integration shows that zkClient will call provider and the microservice zkServices to be registered through api in the interface, and an API will return all the instances. The simple code is as follows:
/ / call micro service @ RequestMapping ("/") public String test () {/ / invoke provider service String str1 = providerClientI.callProviderTwo (); / / invoke zkServices service String str2 = zkClientI.callProviderOne (); return str1 + "-" + str } / / get all microservice instances of zk @ GetMapping ("/ allServices") public List allServiceUrl () {List list = discoveryClient.getServices (); List serviceInstance = new ArrayList (); if (list! = null & & list.size () > 0) {for (String serviceId: list) {serviceInstance.add (discoveryClient.getInstances (serviceId)) }} return serviceInstance;} 3.ZK View:
goes to the directory of zk, use the command. / zkCli.sh-server 192.168.xx.xx:2181 to access zookeeper, then use ls / to view all the nodes, and then use ls / services to view the nodes under services. By default, the registered service is under the services node, when you can see provider, and zkClient. Check provider and zkClient and find that there is a node under it, which seems to be named after UUID. Then use the get command to view the data content in the following node, as follows:
Then we probably understand that when a micro-service instance registers to zk, it puts this data in the node. Let's start our work of registering through API.
4.API Registration:
, I'll start with a very simple code, all written to death. After testing that our method is feasible, we will elegantly modify the code. The package of the package, which should be configured as configuration, will be configured. The regeister method here is the core method, in which the node is created to the server through the create method of the Zookeeper class and the data is written to the node. In fact, what is written is the registration information of the service. After writing, other nodes can find this node and then call it.
@ Override public void register (String serviceName, String serviceAddress) {String registryPath = REGISTRY_PATH; try {zk.create ("/ services/zkServices", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); String uuid = "95b4f91c-96b9-4aa6-ab61-935a7f8be599" String data = "{\" name\ ":\" zkService\ ",\" id\ ":\" 95b4f91c-96b9-4aa6-ab61-935a7f8be599\ ",\" address\ ":\" 192.168.6.164\ ",\" port\ ": 9505,\" sslPort\ ": null,\" payload\ ": {\" @ class\ ":\" org.springframework.cloud.zookeeper.discovery.ZookeeperInstance\ ",\" id\ ":\" zkService:9505\ " \ "name\":\ "zkService\",\ "metadata\": {\ "instance_status\":\ "UP\"}},\ "registrationTimeUTC\": 1534763883884,\ "serviceType\":\ "DYNAMIC\",\ "uriSpec\": {\ "parts\": [{\ "value\":\ "scheme\",\ "variable\": true}, {\ "value\":\ "/ /\",\ "variable\": false}, {\ "value\":\ "address\" \ "variable\": true}, {\ "value\":\ ":\",\ "variable\": false}, {\ "value\":\ "port\",\ "variable\": true}]}} " Zk.create ("/ services/zkServices/" >
This code is simple and rough, that is, directly create to create / services/zkServices node, and then create to put data into it. The data is copied from the provider service, which is a json. I changed the relevant address and service name, then started the project, and then checked zk. Sure enough, the node was created, and the get command to check the data was also consistent with the creation, and then the two interfaces above accessing the zkClient service were successful. One correctly returns the response strings of zkServices and provider, and the other interface correctly displays the information of the three services, including zkClient,provider,zkServices, indicating that a service has been successfully registered through api.
four。 After the verification method of the ordinary Java project is correct, we simply encapsulate the code and configure the relevant parameters. In the next blog post, I will give you a simple explanation of the encapsulated code and the meaning of the related parameters. This is the answer to the question about how to register to Zookeeper through Zookeeper's API micro-service. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.