纯净、安全、绿色的下载网站

首页|软件分类|下载排行|最新软件|IT学院

当前位置:首页IT学院IT技术

Zookeeper分布式服务配置中心 Zookeeper怎样实现分布式服务配置中心详解

扯编程的淡   2021-11-21 我要评论
想了解Zookeeper怎样实现分布式服务配置中心详解的相关内容吗,扯编程的淡在本文为您仔细讲解Zookeeper分布式服务配置中心的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:zookeeper集群配置,zookeeper分布式事务,zookeeper分布式服务配置,下面大家一起来学习吧。

1 Linux安装并启动Zookeeper

1.1 安装

下载链接:https://archive.apache.org/dist/zookeeper/

1.1.1 安装

[root@iZ1608aqb7ntn9Z tmp]# ls
apache-zookeeper-3.5.7-bin.tar.gz  hsperfdata_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]# ls
configuration.xsl  log4j.properties  zoo_sample.cfg
[root@iZ1608aqb7ntn9Z conf]# cp zoo_sample.cfg zoo.cfg
[root@iZ1608aqb7ntn9Z conf]# ls
configuration.xsl  log4j.properties  zoo.cfg  zoo_sample.cfg
[root@iZ1608aqb7ntn9Z conf]# vim zoo.cfg 
# 修改zoo.cfg配置文件 内容为:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/data
clientPort=2181
# 保存退出
[root@iZ1608aqb7ntn9Z conf]# cd ..
[root@iZ1608aqb7ntn9Z zookeeper]# mkdir data
[root@iZ1608aqb7ntn9Z zookeeper]# ls
bin  conf  data  docs  lib  LICENSE.txt  NOTICE.txt  README.md  README_packaging.txt

1.2 启动

[root@iZ1608aqb7ntn9Z zookeeper]# cd bin/
[root@iZ1608aqb7ntn9Z bin]# ./zkServer start 
......
[root@iZ1608aqb7ntn9Z bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client 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 status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper3.5.7/bin/../conf/zoo.cfg
Client 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端口

(略)

2 zookeeper配置

[zk: localhost:2181(CONNECTING) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 4] create /config
Created /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 zs
Created /config/hello/student.name
[zk: localhost:2181(CONNECTED) 8] get /config/hello/student.name 
zs

3 Spring Boot配置

3.1 依赖

<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-discovery</artifactId>
        <version>2.1.4.RELEASE</version>
        <type>pom</type>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
        <version>2.1.4.RELEASE</version>
        <type>pom</type>
        <scope>runtime</scope>
    </dependency>
</dependencies>

注意:一定要注意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
 **/
@RestController
public 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.name  ls 
Created /config/hello/student.name
[zk: localhost:2181(CONNECTED) 12] get /config/hello/student.name 
ls

重新启动项目:

总结


相关文章

猜您喜欢

  • c语言实现扫雷 c语言实现含递归清场版扫雷游戏

    想了解c语言实现含递归清场版扫雷游戏的相关内容吗,Shany-Ming在本文为您仔细讲解c语言实现扫雷的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C语言实现扫雷游戏完整代码,c语言编写扫雷,c语言扫雷源代码,下面大家一起来学习吧。..
  • Vue之组件 Vue之组件详解

    想了解Vue之组件详解的相关内容吗,王同学要努力在本文为您仔细讲解Vue之组件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Vue详解,Vue组件,下面大家一起来学习吧。..

网友评论

Copyright 2020 www.uigame.net 【UI软件站】 版权所有 软件发布

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 点此查看联系方式