springcloud整合zookeeper简单应用

1.zookeeper 背景
zookeeper 作为dubbo注册与发现中心,采用cp理论。
springcloud整合zookeeper简单应用_第1张图片
2.环境准备
linux安装zookeeper 安装步骤省略。
启动zookeeper 服务端。
进去zookeeper安装路径下

[root@localhost ~]# cd /opt/zookeeper/
[root@localhost zookeeper]# cd bin
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost bin]# 


启动客户端 ,服务端是否启动成功

[root@localhost bin]# ./zkCli.sh
Connecting to localhost:2181
2022-03-30 20:29:39,038 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
2022-03-30 20:29:39,040 [myid:] - INFO  [main:Environment@98] - Client environment:host.name=localhost
2022-03-30 20:29:39,041 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=1.8.0_231
2022-03-30 20:29:39,042 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2022-03-30 20:29:39,042 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/usr/local/java/jdk1.8.0_231/jre
2022-03-30 20:29:39,042 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/opt/zookeeper/bin/../zookeeper-server/target/classes:/opt/zookeeper/bin/../build/classes:/opt/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/opt/zookeeper/bin/../lib/zookeeper-jute-3.7.0.jar:/opt/zookeeper/bin/../lib/zookeeper-3.7.0.jar:/opt/zookeeper/bin/../lib/snappy-java-1.1.7.7.jar:/opt/zookeeper/bin/../lib/slf4j-log4j12-1.7.30.jar:/opt/zookeeper/bin/../lib/slf4j-api-1.7.30.jar:/opt/zookeeper/bin/../lib/simpleclient_servlet-0.9.0.jar:/opt/zookeeper/bin/../lib/simpleclient_hotspot-0.9.0.jar:/opt/zookeeper/bin/../lib/simpleclient_common-0.9.0.jar:/opt/zookeeper/bin/../lib/simpleclient-0.9.0.jar:/opt/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-transport-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-resolver-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-handler-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-common-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-codec-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/netty-buffer-4.1.59.Final.jar:/opt/zookeeper/bin/../lib/metrics-core-4.1.12.1.jar:/opt/zookeeper/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/bin/../lib/jline-2.14.6.jar:/opt/zookeeper/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/jetty-util-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/jetty-servlet-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/jetty-server-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/jetty-security-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/jetty-io-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/jetty-http-9.4.38.v20210224.jar:/opt/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/opt/zookeeper/bin/../lib/jackson-databind-2.10.5.1.jar:/opt/zookeeper/bin/../lib/jackson-core-2.10.5.jar:/opt/zookeeper/bin/../lib/jackson-annotations-2.10.5.jar:/opt/zookeeper/bin/../lib/commons-cli-1.4.jar:/opt/zookeeper/bin/../lib/audience-annotations-0.12.0.jar:/opt/zookeeper/bin/../zookeeper-*.jar:/opt/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/bin/../conf:.:/usr/local/java/jdk1.8.0_231/lib/dt.jar:/usr/local/java/jdk1.8.0_231/lib/tools.jar:/usr/local/java/jdk1.8.0_231/jre/lib
2022-03-30 20:29:39,042 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2022-03-30 20:29:39,042 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2022-03-30 20:29:39,042 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=<NA>
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=3.10.0-1062.el7.x86_64
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=root
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/root
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/opt/zookeeper/bin
2022-03-30 20:29:39,043 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=11MB
2022-03-30 20:29:39,044 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=247MB
2022-03-30 20:29:39,044 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=15MB
2022-03-30 20:29:39,061 [myid:] - INFO  [main:ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7d4793a8
2022-03-30 20:29:39,068 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2022-03-30 20:29:39,076 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2022-03-30 20:29:39,102 [myid:] - INFO  [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
2022-03-30 20:29:39,108 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181.
2022-03-30 20:29:39,108 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2022-03-30 20:29:39,186 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:36668, server: localhost/0:0:0:0:0:0:0:1:2181
2022-03-30 20:29:39,251 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, session id = 0x100002061620000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 

出现此日志 说明zookeeper 启动成功。

2.创建maven项目
步骤省略。重点在于application.yaml配置文件

server:
  port: 8004
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: :xxx.xx.xxx.x:2181

重点 启动类加注解

@EnableDiscoveryClient

查看zookeeper 客户端

[zk: localhost:2181(CONNECTED) 0] ls /services
[]
[zk: localhost:2181(CONNECTED) 1] ls /services
[cloud-provider-payment]

你可能感兴趣的