1.安装zookeeper
1.1下载解压
1.2修改zoo_sample.cfg为zoo.cfg 配置内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/mq/zookeeper/data
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>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<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
instance-host: ${spring.cloud.client.ip-address}
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
instance-host: ${spring.cloud.client.ip-address}
root: /sms
完美,结束
|