springcloud整合zookeeper做注册中心(集群模式,亲测可用)

该博客介绍了如何在Spring Cloud应用中整合Zookeeper,包括Zookeeper的安装配置、Spring Cloud相关依赖排除与引入、配置文件设置以及服务启动与验证。通过这些步骤,实现了服务在Zookeeper注册中心的自动注册与发现。
摘要由CSDN通过智能技术生成

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

完美,结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小生有礼了哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值