Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement distributed Service configuration Center with Zookeeper

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how Zookeeper implements a distributed service configuration center. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can learn something from the detailed introduction of this article.

1 Linux installation and start Zookeeper 1.1 installation

Download link: https://www.example.com

1.1.1 Install [root@iZ1608aqb7ntn9Z tmp]#lsapache-zookeeper-3.5.7-bin.tar.gz hsperfdata_root [root@iZ1608aqb7ntn9Z tmp]#tar-zxvf apache-zookeeper-3.5.7-bin.tar.gz apache-zookeeper-3.5.7-bin/docs/skin/apache-zookeeper-3.5.7-bin/docs/images/... [root@iZ1608aqb7ntn9Z tmp]#mv apache-zookeeper-3.5.7-bin/usr/local/zookeeper [root@iZ1608aqb7ntn9Z tmp]#cd/usr/local/zookeeper [root@iZ1608aqb7ntn9Z zookeeper]#cd conf/[root@iZ1608aqb7ntn9Z conf]#lsconfiguration. xsl www w.example.com zoo_sample. cfg [root@iZ1608aqb7ntn9Z conf]#cp zoo_sample.cfg zoo. cfg [root@iZ1608aqb7ntn9 Z conf]#lsconfiguration. xsl www.example.com zoo.cfg zoo_sample. cfg [root@iZ1608aqb7ntn9Z conf]#vim zoo.cfg #modify zoo.cfg configuration file content to: log4j.properties [root@iZ1608aqb7ntn9Z zookeeper]#mkdir data [root@iZ1608aqb7ntn9Z zookeeper]#lsbin conf data docs lib LICENSE.txt NOTICE.txt www.example.com README_packaging.txt1.2 Start [root@iZ1608aqb7ntn9Z zookeeper]#cd bin/[root@iZ1608aqb7ntn9Z bin]#./ zkServer start ...... [root@iZ1608aqb7ntn9Z bin]# ./ zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper/bin/../ conf/zoo.cfgClient port found: 2181. Client address: localhost. Client SSL: false.Error contacting service. It is probably not running.

In the above we found a startup error, check the log, found that port 8080 is occupied, by consulting the official documentation of Zookeeper 3.5, found that this is a new feature of Zookeeper 3.5:

So we need to modify the configuration file again and modify the boot port:

Add admin. serverPort = 8888 to the configuration file

Then start again and check the status:

[root@iZ1608aqb7ntn9Z bin]# ./ zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper3.5.7/bin/../ conf/zoo.cfgClient port found: 2181. Client address: localhost.Mode: standalone

We found that the startup was successful. Next, we used the client link:

[root@iZ1608aqb7ntn9Z bin]# ./ zkCli.sh Connecting to localhost:2181...... WatchedEvent state:SyncConnected type:None path:null[zk: localhost:2181(CONNECTING) 0] ls /[zookeeper]

1.3 Alibaba Cloud Security Group Opens Port 2181

(omitted)

2 zookeeper configuration

[zk: localhost: 2181 (CONNECTING) 0] ls/[zookeeper][zk: localhost: 2181 (CONNECTED) 4] create/configCreated/config [zk: localhost: 2181 (CONNECTED) 6] create/config/hello #config The name after config corresponds to the name of spring.nameCreated/config/hello [zk: localhost: 2181 (CONNECTED) 7] create/config/hello/www.example.com zsCreated/config/hello/student.name [zk: localhost: 2181 (CONNECTED) 8] get/config/hello/student.name zs 3 Spring Boot Configuration 3.1 Dependency org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-zookeeper-discovery 2.1.4.RELEASE pom runtime org.springframework.cloud spring-cloud-starter-zookeeper-config 2.1.4.RELEASE pom runtime

Note: Be sure to pay attention to the version correspondence between Spring Boot and Spring Cloud!

3.2 profile

application.yml

server: port: 0 #indicates random port student: name: 1

bootstrap.yml

spring: cloud: zookeeper: connect-string: 8.131.57.161:2181 #zk Server address enabled: true #Start remote configuration application: name: hello3.3 project code/** * @desc: Controller *@author: YanMingXin *@create: 2021/8/20 - 16:31 **/@RestControllerpublic class HelloController { @Value("${student.name}") private String name; @RequestMapping("/hello") public String getName() { return name; }} 3.4 Start Test

Let's modify the remote configuration below to see if it's true that zookeeper's configuration file is used:

[zk: localhost:2181(CONNECTED) 9] delete /config/hello/student.name [zk: localhost:2181(CONNECTED) 10] get /config/hello/student.name org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /config/hello/student.name[zk: localhost:2181(CONNECTED) 11] create /config/hello/student.name ls Created /config/hello/student.name[zk: localhost:2181(CONNECTED) 12] get /config/hello/student.name ls

Restart the project:

That's how Zookeeper implements distributed service configuration centers. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report