用Zookeeper作为Spring cloud的配置中心

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

原文地址:https://www.cnblogs.com/hellohero55/p/12680991.html