SpringBoot ZooKeeper集成

1.下载ZooKeeper

官网:https://zookeeper.apache.org/

下载链接:https://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.1/apache-zookeeper-3.6.1-bin.tar.gz

2.解压配置

解压后目录为:

SpringBoot ZooKeeper集成_第1张图片

将conf目录下的zoo_sample.cfg文件,复制一份,重命名为zoo.cfg,
修改zoo.cfg配置文件,将dataDir=/tmp/zookeeper修改成zookeeper安装目录所在的data文件夹(我的目录:dataDir=E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\data)这里可手动添加,也可启动自动生成,再添加一条添加数据日志的配置,如下图:SpringBoot ZooKeeper集成_第2张图片

3.启动zookeeper

SpringBoot ZooKeeper集成_第3张图片
启动后如下图:

E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin>call "D:\Program Files\Java\jdk1.8.0_161"\bin\java "-Dzookeeper.log.dir=E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\logs" "-Dzookeeper.root.logger=INFO,CONSOLE" "-Dzookeeper.log.file=zookeeper-admin-server-LAPTOP-1UCMET42.log" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%p /t /f" -cp "E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\build\classes;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\build\lib\*;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\*;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\*;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg"
2020-05-15 18:49:58,397 [myid:] - INFO  [main:QuorumPeerConfig@173] - Reading configuration from: E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg
2020-05-15 18:49:58,419 [myid:] - INFO  [main:QuorumPeerConfig@459] - clientPortAddress is 0.0.0.0:2181
2020-05-15 18:49:58,420 [myid:] - INFO  [main:QuorumPeerConfig@463] - secureClientPort is not set
2020-05-15 18:49:58,420 [myid:] - INFO  [main:QuorumPeerConfig@479] - observerMasterPort is not set
2020-05-15 18:49:58,422 [myid:] - INFO  [main:QuorumPeerConfig@496] - metricsProvider.className is org.apache.zookeeper.metrics.impl.DefaultMetricsProvider
2020-05-15 18:49:58,424 [myid:] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2020-05-15 18:49:58,424 [myid:] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2020-05-15 18:49:58,424 [myid:] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2020-05-15 18:49:58,424 [myid:] - WARN  [main:QuorumPeerMain@138] - Either no config or no quorum defined in config, running in standalone mode
2020-05-15 18:49:58,430 [myid:] - INFO  [main:ManagedUtil@44] - Log4j 1.2 jmx support found and enabled.
2020-05-15 18:49:58,491 [myid:] - INFO  [main:QuorumPeerConfig@173] - Reading configuration from: E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg
2020-05-15 18:49:58,492 [myid:] - INFO  [main:QuorumPeerConfig@459] - clientPortAddress is 0.0.0.0:2181
2020-05-15 18:49:58,492 [myid:] - INFO  [main:QuorumPeerConfig@463] - secureClientPort is not set
2020-05-15 18:49:58,492 [myid:] - INFO  [main:QuorumPeerConfig@479] - observerMasterPort is not set
2020-05-15 18:49:58,493 [myid:] - INFO  [main:QuorumPeerConfig@496] - metricsProvider.className is org.apache.zookeeper.metrics.impl.DefaultMetricsProvider
2020-05-15 18:49:58,493 [myid:] - INFO  [main:ZooKeeperServerMain@122] - Starting server
2020-05-15 18:49:58,555 [myid:] - INFO  [main:ServerMetrics@62] - ServerMetrics initialized with provider org.apache.zookeeper.metrics.impl.DefaultMetricsProvider@63d4e2ba
2020-05-15 18:49:58,559 [myid:] - INFO  [main:FileTxnSnapLog@124] - zookeeper.snapshot.trust.empty : false
2020-05-15 18:49:58,572 [myid:] - INFO  [main:ZookeeperBanner@42] -
2020-05-15 18:49:58,572 [myid:] - INFO  [main:ZookeeperBanner@42] -   ______                  _
2020-05-15 18:49:58,573 [myid:] - INFO  [main:ZookeeperBanner@42] -  |___  /                 | |
2020-05-15 18:49:58,573 [myid:] - INFO  [main:ZookeeperBanner@42] -     / /    ___     ___   | | __   ___    ___   _ __     ___   _ __
2020-05-15 18:49:58,574 [myid:] - INFO  [main:ZookeeperBanner@42] -    / /    / _ \   / _ \  | |/ /  / _ \  / _ \ | '_ \   / _ \ | '__|
2020-05-15 18:49:58,574 [myid:] - INFO  [main:ZookeeperBanner@42] -   / /__  | (_) | | (_) | |   <  |  __/ |  __/ | |_) | |  __/ | |
2020-05-15 18:49:58,576 [myid:] - INFO  [main:ZookeeperBanner@42] -  /_____|  \___/   \___/  |_|\_\  \___|  \___| | .__/   \___| |_|
2020-05-15 18:49:58,576 [myid:] - INFO  [main:ZookeeperBanner@42] -                                               | |
2020-05-15 18:49:58,576 [myid:] - INFO  [main:ZookeeperBanner@42] -                                               |_|
2020-05-15 18:49:58,577 [myid:] - INFO  [main:ZookeeperBanner@42] -
2020-05-15 18:50:07,598 [myid:] - INFO  [main:Environment@98] - Server environment:zookeeper.version=3.6.1--104dcb3e3fb464b30c5186d229e00af9f332524b, built on 04/21/2020 15:01 GMT
2020-05-15 18:50:07,599 [myid:] - INFO  [main:Environment@98] - Server environment:host.name=LAPTOP-1UCMET42
2020-05-15 18:50:07,600 [myid:] - INFO  [main:Environment@98] - Server environment:java.version=1.8.0_161
2020-05-15 18:50:07,601 [myid:] - INFO  [main:Environment@98] - Server environment:java.vendor=Oracle Corporation
2020-05-15 18:50:07,601 [myid:] - INFO  [main:Environment@98] - Server environment:java.home=D:\Program Files\Java\jdk1.8.0_161\jre
2020-05-15 18:50:07,602 [myid:] - INFO  [main:Environment@98] - Server environment:java.class.path=E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\build\classes;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\build\lib\*;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\*;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\audience-annotations-0.5.0.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\commons-cli-1.2.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\commons-lang-2.6.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jackson-annotations-2.10.3.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jackson-core-2.10.3.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jackson-databind-2.10.3.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\javax.servlet-api-3.1.0.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jetty-http-9.4.24.v20191120.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jetty-io-9.4.24.v20191120.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jetty-security-9.4.24.v20191120.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jetty-server-9.4.24.v20191120.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jetty-servlet-9.4.24.v20191120.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jetty-util-9.4.24.v20191120.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\jline-2.11.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\json-simple-1.1.1.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\log4j-1.2.17.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\metrics-core-3.2.5.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-buffer-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-codec-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-common-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-handler-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-resolver-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-transport-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-transport-native-epoll-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\netty-transport-native-unix-common-4.1.48.Final.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\simpleclient-0.6.0.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\simpleclient_common-0.6.0.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\simpleclient_hotspot-0.6.0.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\simpleclient_servlet-0.6.0.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\slf4j-api-1.7.25.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\slf4j-log4j12-1.7.25.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\snappy-java-1.1.7.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\zookeeper-3.6.1.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\zookeeper-jute-3.6.1.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\lib\zookeeper-prometheus-metrics-3.6.1.jar;E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin\..\conf
2020-05-15 18:50:07,603 [myid:] - INFO  [main:Environment@98] - Server environment:java.library.path=D:\Program Files\Java\jdk1.8.0_161\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\Xshell 6\;D:\Oracle19\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;D:\Program Files\Java\jdk1.8.0_161\bin;E:\apache-maven-3.6.1\bin;D:\nodejs\;D:\SVN\bin;D:\Erlang\erl10.5\bin;C:\Users\admin\AppData\Local\Microsoft\WindowsApps;;D:\vscode\Microsoft VS Code\bin;C:\Users\admin\AppData\Roaming\npm;.
2020-05-15 18:50:07,604 [myid:] - INFO  [main:Environment@98] - Server environment:java.io.tmpdir=C:\Users\admin\AppData\Local\Temp\
2020-05-15 18:50:07,604 [myid:] - INFO  [main:Environment@98] - Server environment:java.compiler=<NA>
2020-05-15 18:50:07,604 [myid:] - INFO  [main:Environment@98] - Server environment:os.name=Windows 10
2020-05-15 18:50:07,605 [myid:] - INFO  [main:Environment@98] - Server environment:os.arch=amd64
2020-05-15 18:50:07,605 [myid:] - INFO  [main:Environment@98] - Server environment:os.version=10.0
2020-05-15 18:50:07,606 [myid:] - INFO  [main:Environment@98] - Server environment:user.name=admin
2020-05-15 18:50:07,606 [myid:] - INFO  [main:Environment@98] - Server environment:user.home=C:\Users\admin
2020-05-15 18:50:07,607 [myid:] - INFO  [main:Environment@98] - Server environment:user.dir=E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\bin
2020-05-15 18:50:07,607 [myid:] - INFO  [main:Environment@98] - Server environment:os.memory.free=170MB
2020-05-15 18:50:07,608 [myid:] - INFO  [main:Environment@98] - Server environment:os.memory.max=2704MB
2020-05-15 18:50:07,608 [myid:] - INFO  [main:Environment@98] - Server environment:os.memory.total=184MB
2020-05-15 18:50:07,608 [myid:] - INFO  [main:ZooKeeperServer@128] - zookeeper.enableEagerACLCheck = false
2020-05-15 18:50:07,609 [myid:] - INFO  [main:ZooKeeperServer@136] - zookeeper.digest.enabled = true
2020-05-15 18:50:07,612 [myid:] - INFO  [main:ZooKeeperServer@140] - zookeeper.closeSessionTxn.enabled = true
2020-05-15 18:50:07,612 [myid:] - INFO  [main:ZooKeeperServer@1434] - zookeeper.flushDelay=0
2020-05-15 18:50:07,612 [myid:] - INFO  [main:ZooKeeperServer@1443] - zookeeper.maxWriteQueuePollTime=0
2020-05-15 18:50:07,613 [myid:] - INFO  [main:ZooKeeperServer@1452] - zookeeper.maxBatchSize=1000
2020-05-15 18:50:07,613 [myid:] - INFO  [main:ZooKeeperServer@241] - zookeeper.intBufferStartingSizeBytes = 1024
2020-05-15 18:50:07,615 [myid:] - INFO  [main:BlueThrottle@141] - Weighed connection throttling is disabled
2020-05-15 18:50:07,616 [myid:] - INFO  [main:ZooKeeperServer@1246] - minSessionTimeout set to 4000
2020-05-15 18:50:07,616 [myid:] - INFO  [main:ZooKeeperServer@1255] - maxSessionTimeout set to 40000
2020-05-15 18:50:07,618 [myid:] - INFO  [main:ResponseCache@45] - Response cache size is initialized with value 400.
2020-05-15 18:50:07,618 [myid:] - INFO  [main:ResponseCache@45] - Response cache size is initialized with value 400.
2020-05-15 18:50:07,623 [myid:] - INFO  [main:RequestPathMetricsCollector@111] - zookeeper.pathStats.slotCapacity = 60
2020-05-15 18:50:07,623 [myid:] - INFO  [main:RequestPathMetricsCollector@112] - zookeeper.pathStats.slotDuration = 15
2020-05-15 18:50:07,623 [myid:] - INFO  [main:RequestPathMetricsCollector@113] - zookeeper.pathStats.maxDepth = 6
2020-05-15 18:50:07,624 [myid:] - INFO  [main:RequestPathMetricsCollector@114] - zookeeper.pathStats.initialDelay = 5
2020-05-15 18:50:07,624 [myid:] - INFO  [main:RequestPathMetricsCollector@115] - zookeeper.pathStats.delay = 5
2020-05-15 18:50:07,625 [myid:] - INFO  [main:RequestPathMetricsCollector@116] - zookeeper.pathStats.enabled = false
2020-05-15 18:50:07,629 [myid:] - INFO  [main:ZooKeeperServer@1471] - The max bytes for all large requests are set to 104857600
2020-05-15 18:50:07,629 [myid:] - INFO  [main:ZooKeeperServer@1485] - The large request threshold is set to -1
2020-05-15 18:50:07,630 [myid:] - INFO  [main:ZooKeeperServer@329] - Created server with tickTime 2000 minSessionTimeout 4000 maxSessionTimeout 40000 clientPortListenBacklog -1 datadir E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\log\version-2 snapdir E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\data\version-2
2020-05-15 18:50:07,654 [myid:] - INFO  [main:Log@169] - Logging initialized @9499ms to org.eclipse.jetty.util.log.Slf4jLog
2020-05-15 18:50:07,741 [myid:] - WARN  [main:ContextHandler@1520] - o.e.j.s.ServletContextHandler@79be0360{/,null,UNAVAILABLE} contextPath ends with /*
2020-05-15 18:50:07,742 [myid:] - WARN  [main:ContextHandler@1531] - Empty contextPath
2020-05-15 18:50:07,759 [myid:] - INFO  [main:Server@359] - jetty-9.4.24.v20191120; built: 2019-11-20T21:37:49.771Z; git: 363d5f2df3a8a28de40604320230664b9c793c16; jvm 1.8.0_161-b12
2020-05-15 18:50:07,795 [myid:] - INFO  [main:DefaultSessionIdManager@333] - DefaultSessionIdManager workerName=node0
2020-05-15 18:50:07,795 [myid:] - INFO  [main:DefaultSessionIdManager@338] - No SessionScavenger set, using defaults
2020-05-15 18:50:07,798 [myid:] - INFO  [main:HouseKeeper@140] - node0 Scavenging every 600000ms
2020-05-15 18:50:07,807 [myid:] - INFO  [main:ContextHandler@825] - Started o.e.j.s.ServletContextHandler@79be0360{/,null,AVAILABLE}
2020-05-15 18:50:08,726 [myid:] - INFO  [main:AbstractConnector@330] - Started ServerConnector@589838eb{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2020-05-15 18:50:08,727 [myid:] - INFO  [main:Server@399] - Started @10572ms
2020-05-15 18:50:08,728 [myid:] - INFO  [main:JettyAdminServer@178] - Started AdminServer on address 0.0.0.0, port 8080 and command URL /commands
2020-05-15 18:50:08,733 [myid:] - INFO  [main:ServerCnxnFactory@169] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory
2020-05-15 18:50:08,734 [myid:] - WARN  [main:ServerCnxnFactory@309] - maxCnxns is not configured, using default value 0.
2020-05-15 18:50:08,736 [myid:] - INFO  [main:NIOServerCnxnFactory@666] - Configuring NIO connection handler with 10s sessionless connection timeout, 1 selector thread(s), 8 worker threads, and 64 kB direct buffers.
2020-05-15 18:50:08,739 [myid:] - INFO  [main:NIOServerCnxnFactory@674] - binding to port 0.0.0.0/0.0.0.0:2181
2020-05-15 18:50:08,753 [myid:] - INFO  [main:WatchManagerFactory@42] - Using org.apache.zookeeper.server.watch.WatchManager as watch manager
2020-05-15 18:50:08,754 [myid:] - INFO  [main:WatchManagerFactory@42] - Using org.apache.zookeeper.server.watch.WatchManager as watch manager
2020-05-15 18:50:08,754 [myid:] - INFO  [main:ZKDatabase@132] - zookeeper.snapshotSizeFactor = 0.33
2020-05-15 18:50:08,755 [myid:] - INFO  [main:ZKDatabase@152] - zookeeper.commitLogCount=500
2020-05-15 18:50:08,763 [myid:] - INFO  [main:SnapStream@61] - zookeeper.snapshot.compression.method = CHECKED
2020-05-15 18:50:08,764 [myid:] - INFO  [main:FileTxnSnapLog@470] - Snapshotting: 0x0 to E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\data\version-2\snapshot.0
2020-05-15 18:50:08,768 [myid:] - INFO  [main:ZKDatabase@289] - Snapshot loaded in 13 ms, highest zxid is 0x0, digest is 1371985504
2020-05-15 18:50:08,768 [myid:] - INFO  [main:FileTxnSnapLog@470] - Snapshotting: 0x0 to E:\apache-zookeeper-3.6.1-bin\apache-zookeeper-3.6.1-bin\data\version-2\snapshot.0
2020-05-15 18:50:08,770 [myid:] - INFO  [main:ZooKeeperServer@519] - Snapshot taken in 1 ms
2020-05-15 18:50:08,781 [myid:] - INFO  [main:RequestThrottler@74] - zookeeper.request_throttler.shutdownTimeout = 10000
2020-05-15 18:50:08,797 [myid:] - INFO  [main:ContainerManager@83] - Using checkIntervalMs=60000 maxPerMinute=10000 maxNeverUsedIntervalMs=0
2020-05-15 18:50:08,799 [myid:] - INFO  [main:ZKAuditProvider@42] - ZooKeeper audit is disabled.

zookeeper默认端口为:2181

4 创建项目

进入https://start.spring.io/

具体可查看:https://blog.csdn.net/qq_45556336/article/details/106120133

SpringBoot ZooKeeper集成_第4张图片
把项目下载下来导入即可(IDEA此方法应该不行)。

修改项目名再次创建consumer 下载,导入如下图。
在这里插入图片描述

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.7.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.qifan</groupId>
	<artifactId>zk-producer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>zk-producer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
	</properties>

	<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-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

5.编写代码

zk-producer项目:

在application.property添加如下配置:

server.port=8000
spring.application.name=zktest
spring.cloud.zookeeper.connect-string=127.0.0.1:2181

创建控制类

package com.qifan.zkproducer.controller;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pro")
@EnableDiscoveryClient
public class ProController {
	
	@RequestMapping("/test")
	public String test() {
		return "hello-zookepper!";
	}
}

zk-consumer项目:

在application.property添加如下配置:

server.port=8001
spring.application.name=zktest2
spring.cloud.zookeeper.connect-string=127.0.0.1:2181

编写启动类

添加@EnableFeignClients,这里我使用RestTemp进行调用(也可使用Feign),创建RestTemplate实例
@LoadBalanced一定要加

package com.qifan.zkconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableFeignClients
public class ZkConsumerApplication {

	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(ZkConsumerApplication.class, args);
	}

}

创建控制类

注入RestTemplate实例

package com.qifan.zkconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("con")
@EnableDiscoveryClient
public class ConController {

	@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("test")
	public String test() {
		String s = restTemplate.getForObject("http://zktest/pro/test", String.class);
		return s;
	}
}

6.启动项目

分别运行启动类
SpringBoot ZooKeeper集成_第5张图片

SpringBoot ZooKeeper集成_第6张图片
运行zkClient.cmd可以查看项目服务
SpringBoot ZooKeeper集成_第7张图片

SpringBoot ZooKeeper集成_第8张图片
项目启动完成

7.浏览器访问

SpringBoot ZooKeeper集成_第9张图片
可以看到我们访问的8001端口调用到8000端口的test方法。

你可能感兴趣的:(springboot,zookeeper)