1.安装zookeeper
1.1下载解压
1.2修改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=/mq/zookeeper/data
# the port at which the clients will connect
clientPort=2181
server.1=192.168.201.148:2888:3888
server.2=192.168.201.151:2888:3888
server.3=192.168.201.152:2888:3888
1.3 启动三台服务器的zookeeper
进入bin目录
启动命令 ./zkServer.sh start
查询zookeeper 运行状态命令 ./zkServer.sh status
停止命令 .zkServer.sh stop
2.整合springcloud
2.1引入pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<!-- 与服务器安装的Zookeeper版本相同 -->
<version>3.6.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
2.2 配置文件yaml
spring:
cloud:
zookeeper:
connect-string: 192.168.201.148:2181,192.168.201.151:2181,192.168.201.152:2181
discovery:
enabled: true
register: true
#自动获取启动服务的ip注册到zk注册中心
instance-host: ${spring.cloud.client.ip-address}
#注册到zk之后的根节点的名称
root: /sms
2.3项目启动入口
添加 @EnableDiscoveryClient
@SpringCloudApplication
@EnableDiscoveryClient
public class PlatformCodeGenApplication {
public static void main(String[] args) {
SpringApplication.run(PlatformCodeGenApplication.class, args);
}
}
3.验证结果
启动zookeeper 客户端 ./zkCli.sh
查看节点运行命令
ls /
sms 就是配置文件中root 节点
spring:
cloud:
zookeeper:
connect-string: 192.168.201.148:2181,192.168.201.151:2181,192.168.201.152:2181
discovery:
enabled: true
register: true
#自动获取启动服务的ip注册到zk注册中心
instance-host: ${spring.cloud.client.ip-address}
#注册到zk之后的根节点的名称
root: /sms
完美,结束