Zookeeper如何实现分布式服务配置中心详解
目录
- 1 Linux安装并启动Zookeeper
- 1.1 安装
- 1.1.1 安装
- 1.2 启动
- 3 Spring Boot配置
- 3.1 依赖
- 3.2 配置文件
- 3.3 项目代码
- 3.4 启动测试
- 总结
1 Linux安装并启动Zookeeper
1.1 安装
下载链接:https://archive.apache.org/dist/zookeeper/
1.1.1 安装
[root@iZ1608aqb7ntn9Z tmp]# lsapache-zookeeper-3.5.7-bin.tar.gzhsperfdata_root[root@iZ1608aqb7ntn9Z tmp]# tar -zxvf apache-zookeeper-3.5.7-bin.tar.gz apache-zookeeper-3.5.7-bin/docs/apache-zookeeper-3.5.7-bin/docs/skin/apache-zookeeper-3.5.7-bin/docs/images/......[root@iZ1608aqb7ntn9Z tmp]# mv apache-zookeeper-3.5.7-bin /usr/local/zookeeper[root@iZ1608aqb7ntn9Z tmp]# cd /usr/local/zookeeper[root@iZ1608aqb7ntn9Z zookeeper]# cd conf/[root@iZ1608aqb7ntn9Z conf]# lsconfiguration.xsllog4j.propertieszoo_sample.cfg[root@iZ1608aqb7ntn9Z conf]# cp zoo_sample.cfg zoo.cfg[root@iZ1608aqb7ntn9Z conf]# lsconfiguration.xsllog4j.propertieszoo.cfgzoo_sample.cfg[root@iZ1608aqb7ntn9Z conf]# vim zoo.cfg # 修改zoo.cfg配置文件 内容为:tickTime=2000initLimit=10syncLimit=5dataDir=/usr/local/zookeeper/dataclientPort=2181# 保存退出[root@iZ1608aqb7ntn9Z conf]# cd ..[root@iZ1608aqb7ntn9Z zookeeper]# mkdir data[root@iZ1608aqb7ntn9Z zookeeper]# lsbinconfdatadocslibLICENSE.txtNOTICE.txtREADME.mdREADME_packaging.txt
1.2 启动
[root@iZ1608aqb7ntn9Z zookeeper]# cd bin/[root@iZ1608aqb7ntn9Z bin]# ./zkServer start ......[root@iZ1608aqb7ntn9Z bin]# ./zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper/bin/../conf/zoo.cfgClient port found: 2181. Client address: localhost. Client SSL: false.Error contacting service. It is probably not running.
在上边我们发现了启动错误,查看日志,发现8080端口被占用,通过查阅Zookeeper3.5的官方文档,发现这是Zookeeper3.5的新特性:
文章图片
所以我们需要再次修改配置文件,修改启动端口:
在配置文件中加入
admin.serverPort=8888
然后再次启动,查看状态:
[root@iZ1608aqb7ntn9Z bin]# ./zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper3.5.7/bin/../conf/zoo.cfgClient port found: 2181. Client address: localhost.Mode: standalone
发现启动成功,接下来我们用客户端链接:
[root@iZ1608aqb7ntn9Z bin]# ./zkCli.sh Connecting to localhost:2181......WatchedEvent state:SyncConnected type:None path:null[zk: localhost:2181(CONNECTING) 0] ls /[zookeeper]
1.3 阿里云安全组放开2181端口
(略)
【Zookeeper如何实现分布式服务配置中心详解】2 zookeeper配置
[zk: localhost:2181(CONNECTING) 0] ls /[zookeeper][zk: localhost:2181(CONNECTED) 4] create /configCreated /config[zk: localhost:2181(CONNECTED) 6] create /config/hello # config后的名称要与spring.name的名称对应Created /config/hello[zk: localhost:2181(CONNECTED) 7] create /config/hello/student.name zsCreated /config/hello/student.name[zk: localhost:2181(CONNECTED) 8] get /config/hello/student.name zs
3 Spring Boot配置
3.1 依赖
org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-zookeeper-discovery2.1.4.RELEASE pom runtimeorg.springframework.cloud spring-cloud-starter-zookeeper-config2.1.4.RELEASE pom runtime
注意:一定要注意Spring Boot和Spring Cloud的版本对应!
3.2 配置文件
application.yml
server:port: 0 # 表示随机端口student:name: 1
bootstrap.yml
spring:cloud:zookeeper:connect-string: 8.131.57.161:2181# zk服务端地址enabled: true# 启动远程配置application:name: hello
3.3 项目代码
/** * @desc: 控制器 * @author: YanMingXin * @create: 2021/8/20-16:31 **/@RestControllerpublic class HelloController {@Value("${student.name}")private String name; @RequestMapping("/hello")public String getName() {return name; }}
3.4 启动测试
文章图片
下面我们来修改下远程配置,看下是不是真的使用了zookeeper的配置文件:
[zk: localhost:2181(CONNECTED) 9] delete /config/hello/student.name [zk: localhost:2181(CONNECTED) 10] get /config/hello/student.name org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /config/hello/student.name[zk: localhost:2181(CONNECTED) 11] create/config/hello/student.namels Created /config/hello/student.name[zk: localhost:2181(CONNECTED) 12] get /config/hello/student.name ls
重新启动项目:
文章图片
总结 到此这篇关于Zookeeper如何实现分布式服务配置中心的文章就介绍到这了,更多相关Zookeeper分布式服务配置中心内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 如何寻找情感问答App的分析切入点
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- 如何在Mac中的文件选择框中打开系统隐藏文件夹