Srping Cloud Zookeeper Config
該項目通過自動配置並綁定到Spring環境,為Spring Boot應用程序提供Zookeeper集成。Zookeeper提供了一個分層命名空間,允許客戶端存儲任意數據,如配置數據。Spring Cloud Zookeeper Config是Config Server和Client的替代方案。
兩者的比較
Spring Cloud Config 通過文件系統,git/svn倉庫來管理配置文件。包含客戶端、服務端和git/svn倉庫。通過git/svn特性可以達到版本控制
Spring Cloud Zookeeper Config 通過Zookeeper分級命名空間來儲存配置項數據,另外Zookeeper可以實時監聽節點變化和通知機制。
一、下載地址
http://apache.fayea.com/zookeeper
二、安裝
下載解壓后如圖
三、單機配置
1. 修改 config 下的配置文件
打開 conf 目錄下 zoo_sample.cfg 將其名字改為 zoo.cfg,對其進行如下修改,如下
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 修改的地方
dataDir=D:/JAVA/zookeeper-3.4.10/data
dataLogDir=D:/JAVA/zookeeper-3.4.10/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
2. 參數說明
tickTime:基本事件單元,以毫秒為單位,用來控制心跳和超時,默認情況超時的時間為兩倍的tickTime
dataDir:數據目錄.可以是任意目錄.
dataLogDir:log目錄, 同樣可以是任意目錄. 如果沒有設置該參數, 將使用和dataDir相同的設置.
clientPort:監聽client連接的端口號.
maxClientCnxns:限制連接到zookeeper的客戶端數量,並且限制並發連接數量,它通過ip區分不同的客戶端。
minSessionTimeout和maxSessionTimeout:最小會話超時時間和最大的會話超時時間,在默認情況下,最小的超時時間為2倍的tickTime時間,最大的會話超時時間為20倍的會話超時時間,系統啟動時會顯示相應的信息。默認為-1
initLimit:參數設定了允許所有跟隨者與領導者進行連接並同步的時間,如果在設定的時間段內,半數以上的跟隨者未能完成同步,領導者便會宣布放棄領導地位,進行另一次的領導選舉。如果zk集群環境數量確實很大,同步數據的時間會變長,因此這種情況下可以適當調大該參數。默認為10
syncLimit:參數設定了允許一個跟隨者與一個領導者進行同步的時間,如果在設定的時間段內,跟隨者未完成同步,它將會被集群丟棄。所有關聯到這個跟隨者的客戶端將連接到另外一個跟隨着。
3. 啟動 Zookeeper
在bin目錄下,雙擊zkServer.cmd即可啟動Zookeeper。
如果啟動閃退,可能是配置文件有問題,可以使用默認配置文件看是否可以啟動。
四、加入環境依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> </dependency>
五、zookeeper的可視化工具
百度網盤下載地址 : https://pan.baidu.com/s/1wJJ5c1CKJi-FUd1Z_IbX1Q
CSDN 資源下載地址 : https://download.csdn.net/download/rongbaojian/10627428
解壓完畢以后啟動該工具:
六、bootstarp配置和application配置
spring:
application:
name: producer
cloud:
zookeeper:
enabled: true # true:開啟zookeeper外部化配置, false:讀取本地配置;
connect-string: localhost:2181
config:
root: /myspace #根目錄
enabled: false
watcher:
enabled: true
server:
port: 8883
spring:
application:
name: producer
management:
endpoints:
web:
exposure:
include: "*"
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
instance:
instance-id: producer_8883 #服務名稱的別名
prefer-ip-address: true #顯示IP地址
info:
app.name: producer_8883
company: supers
build.artifactId: $project.artifactId$
build.version: $project.version$
七、測試讀取zookeeper的文件信息
八、若要動態改變值 則 Bean上添加@RefreshScope