您好,登錄后才能下訂單哦!
本篇文章為大家展示了Zookeeper如何實現分布式服務配置中心,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
下載鏈接:https://archive.apache.org/dist/zookeeper/
[root@iZ1608aqb7ntn9Z tmp]# ls apache-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/ 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]# ls configuration.xsl log4j.properties zoo_sample.cfg [root@iZ1608aqb7ntn9Z conf]# cp zoo_sample.cfg zoo.cfg [root@iZ1608aqb7ntn9Z conf]# ls configuration.xsl log4j.properties zoo.cfg zoo_sample.cfg [root@iZ1608aqb7ntn9Z conf]# vim zoo.cfg # 修改zoo.cfg配置文件 內容為: tickTime=2000 initLimit=10 syncLimit=5 dataDir=/usr/local/zookeeper/data clientPort=2181 # 保存退出 [root@iZ1608aqb7ntn9Z conf]# cd .. [root@iZ1608aqb7ntn9Z zookeeper]# mkdir data [root@iZ1608aqb7ntn9Z zookeeper]# ls bin conf data docs lib LICENSE.txt NOTICE.txt README.md README_packaging.txt
[root@iZ1608aqb7ntn9Z zookeeper]# cd bin/ [root@iZ1608aqb7ntn9Z bin]# ./zkServer start ...... [root@iZ1608aqb7ntn9Z bin]# ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Error contacting service. It is probably not running.
在上邊我們發現了啟動錯誤,查看日志,發現8080端口被占用,通過查閱Zookeeper3.5的官方文檔,發現這是Zookeeper3.5的新特性:
所以我們需要再次修改配置文件,修改啟動端口:
在配置文件中加入admin.serverPort=8888
然后再次啟動,查看狀態:
[root@iZ1608aqb7ntn9Z bin]# ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper3.5.7/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: standalone
發現啟動成功,接下來我們用客戶端鏈接:
[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 阿里云安全組放開2181端口
(略)
2 zookeeper配置
[zk: localhost:2181(CONNECTING) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 4] create /config Created /config [zk: localhost:2181(CONNECTED) 6] create /config/hello # config后的名稱要與spring.name的名稱對應 Created /config/hello [zk: localhost:2181(CONNECTED) 7] create /config/hello/student.name zs Created /config/hello/student.name [zk: localhost:2181(CONNECTED) 8] get /config/hello/student.name zs
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <version>2.1.4.RELEASE</version> <type>pom</type> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> <version>2.1.4.RELEASE</version> <type>pom</type> <scope>runtime</scope> </dependency> </dependencies>
注意:一定要注意Spring Boot和Spring Cloud的版本對應!
application.yml
server: port: 0 # 表示隨機端口 student: name: 1
bootstrap.yml
spring: cloud: zookeeper: connect-string: 8.131.57.161:2181 # zk服務端地址 enabled: true # 啟動遠程配置 application: name: hello
/** * @desc: 控制器 * @author: YanMingXin * @create: 2021/8/20-16:31 **/ @RestController public class HelloController { @Value("${student.name}") private String name; @RequestMapping("/hello") public String getName() { return name; } }
下面我們來修改下遠程配置,看下是不是真的使用了zookeeper的配置文件:
[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
重新啟動項目:
上述內容就是Zookeeper如何實現分布式服務配置中心,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。