In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the process of springboot application access to zookeeper. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
The following explains how to quickly develop web services through springboot and read zookeeper, in preparation for the follow-up "deployment of dubbo services under docker". The operations related to this article are carried out on mac. There are some differences between some directories and windows in this article. Please pay attention to make corresponding changes to your own computers.
Local deployment of zookeeper
First, go to the official website to download the zookeeper package. It is recommended to use stable version 3.3.6.
After downloading, decompress it locally, enter the zookeeper-3.3.6/conf directory, rename the zoo_sample.cfg file to zoo.cfg, and open the file with a text editor. Change the contents of the following red box location to an existing directory that can be accessed locally:
Open a command line window, go to the zookeeper-3.3.6/bin directory, execute zkServer.cmd status,mac under windows and execute under linux. / zkServer.sh status, this command is to check the status of the current zookeeper. Since zookeeper has not been started yet, we will see the following error message:
Now start zookeeper, execute zkServer.cmd start,mac under windows and execute. / zkServer.sh start under linux. The startup information is as follows:
Enter zkServer.cmd status at this time, and the result is as follows:
You can see that zookeeper is running in standalone mode
Now let's try the service with the client command, execute zkCli.cmd under windows, execute. / zkCli.sh under mac and linux, and connect to the server:
Execute create / node1 abc to create a znode, and the corresponding value is "abc". Then execute get / node1 to view the value of / node_1, as shown below:
So much for the installation and verification of zookeeper, and then we start to develop applications to access the data of this zookeeper node.
Develop applications based on spring boot
Open IntelliJ IDEA CE, create a Maven project named zookeeperdemo, and specify a template, as shown below:
After the project is created, what you need to do is to modify the pom, add the dependency of the library, add the configuration file and write the java code. Please download the specific source code on my git and link.
The source code is located in the following location:
After downloading the source code, you can import it through Intellij IEDA. The options in the following red box need to be selected during import, so that you can import the maven project:
The whole project is very simple, the first is the pom file, and the dependent parent project uses spring-boot-starter-parent:
You also need to add dependencies on springboot and plug-ins corresponding to springboot when build:
Add application.properties file to the resource directory to configure some important parameters, such as log directory, web server port, etc., as shown below:
Finally, write the java code. Since it is only a demo, the code here is very simple, one entry, one controller, and the entry is as follows:
Controller is a simple rest response. Link zookeeper to get the value of the znode node and return it. Note that since RestController is declared, the returned content is not a page but a string:
At this point, the coding is over. Let's try it. Create a new command in the following figure:
Select maven:
The command content is clean package-U-Dmaven.test.skip=true spring-boot:run, as follows:
When the command is created, click "run" to execute:
Open the browser at this time, and type http://localhost:2222/zkget to get the value of / node_1 we set on zookeeper, as shown below:
At this point, access to zookeeper's demo development based on the spring boot framework and the end.
This is how the editor shares the process of accessing zookeeper for springboot applications. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow 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.