1. 程式人生 > >使用zookeeper作為服務註冊中心

使用zookeeper作為服務註冊中心

zookeeper作為服務註冊中心

下載安裝

zookeeper官網下載最新版,修改conf/zoo.cfg配置檔案,如果是windows系統必須指定dataDir具體路徑

執行bin/zkServer.cmd啟動zookeeper服務

注意,zookeeper版本必須高於工程依賴的zookeeper客戶端版本

工程配置

  • pom檔案新增依賴
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
  • 新增配置項
spring.cloud.zookeeper.connect-string: 127.0.0.1:2181

其他

  • 查詢註冊服務

執行bin/zkCli.cmd,輸入命令ls -s /services

[zk: localhost:2181(CONNECTED) 12] ls -R /services
/services
/services/admin
/services/spt-bas-report
/services/spt-bas-server
/services/web
/services/spt-bas-report/75026980-af9c-4c9f-b961-a1b5d6ed5f2f
/services/spt-bas-server/e71c5939-e2ff-4a1f-a736-894f12c06087
/services/web/d86cd254-3ebd-421f-bd67-4bd9be327343

zookeeper常用配置

spring.cloud.zookeeper.discovery.root: /services

Root Zookeeper folder in which all instances are registered

配置項 預設值 備註
spring.cloud.zookeeper.discovery.root /services Root Zookeeper folder in which all instances are registered
spring.cloud.zookeeper.discovery.instance-id Id used to register with zookeeper. Defaults to a random UUID.
spring.cloud.zookeeper.discovery.enabled true Is Zookeeper enabled
spring.cloud.zookeeper.discovery.register true Register as a service in zookeeper.
spring.cloud.zookeeper.discovery.enabled true
spring.cloud.zookeeper.connect-string localhost:2181 Connection string to the Zookeeper cluster