zookeeper和dubbo的使用

本文介绍了Zookeeper的单机与集群安装步骤,并详细讲述了如何配置Zookeeper集群。接着,文章阐述了Zookeeper在项目中的整合使用,包括创建项目、设置watcher以及ZkDemo类的内容。最后,文章提供了Dubbo的入门案例,包括服务提供者和服务消费者的创建与配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Zookeeper的安装

单机版安装

1)安装java

2) wget http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz

3)修改zoo_sample.conf 为zoo.conf(保险起见使用cp命令复制zoo_sample.conf文件)

修改配置文件的类容

1. # vim conf/zoo-1.cfg  

2. dataDir=/tmp/zookeeper //最好切进此目录查看是否有data目录   

3. clientPort=2181

4)启动服务(在bin下调用此命令)

./zkServer.sh start
其它:
./zkServer.sh stop
./zkServer.sh restart
./zkServer.sh status

5)启动客户端(在bin下调用此命令)

./zkCli.sh
客户端命令:
查看目录        ls /
创建节点        create /name    value
查看节点        get /name
修改          set /name value
删除          delete /name
退出          quit

启动客户端后默认里面有一个data节点可以通过set方法设置节点类容,再通过get方法得到设置的节点类容,至此一个单体zookeeper就完成了。

集群安装

Step1:配置JAVA环境,检验环境:java -version

Step2:下载并解压zookeeper

  1. # cd /usr/local

  2. # wget http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz

  3. # tar -zxvf zookeeper-3.4.12.tar.gz

  4. # cd zookeeper-3.4.12

Step3:重命名 zoo_sample.cfg文件

# cp conf/zoo_sample.cfg conf/zoo-1.cfg

Step4:修改配置文件zoo-1.cfg,原配置文件里有的,修改成下面的值,没有的则加上

   1. # vim conf/zoo-1.cfg
   2. dataDir=/tmp/zookeeper-1
   3. clientPort=2181
   4. server.1=127.0.0.1:2888:3888
   5. server.2=127.0.0.1:2889:3889
   6. server.3=127.0.0.1:2890:3890

Step4:再从zoo-1.cfg复制两个配置文件zoo-2.cfg和zoo-3.cfg,只需修改dataDir和clientPort不同即可

   1. # cp conf/zoo-1.cfg conf/zoo-2.cfg
   2. # cp conf/zoo-1.cfg conf/zoo-3.cfg
   3. # vim conf/zoo-2.cfg
   4. dataDir=/tmp/zookeeper-2
   5. clientPort=2182
   6. # vim conf/zoo-2.cfg
   7. dataDir=/tmp/zookeeper-3
   8. clientPort=2183

Step5:标识Server ID

创建三个文件夹/tmp/zookeeper-1,/tmp/zookeeper-2,/tmp/zookeeper-2,在每个目录中创建文件myid 文件,写入当前实例的server id,即1.2.3

   1. \# cd /tmp/zookeeper-1
   2. \# vim myid
   3. 1
   4. \# cd /tmp/zookeeper-2
   5. \# vim myid
   6. 2
   7. \# cd /tmp/zookeeper-3
   8. \# vim myid
   9. 3

Step6:启动三个zookeeper实例

   1. \# bin/zkServer.sh start conf/zoo-1.cfg
   2. \# bin/zkServer.sh start conf/zoo-2.cfg
   3. \# bin/zkServer.sh start conf/zoo-3.cfg

Step7:bin/zkServer.sh status conf/zoo-1.cg(在zookeeper目录下运行此命令)

查看集群的状态

## zookeeper整合项目的使用

创建父级项目

父项目依赖如下

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hp</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>


    </dependencies>

创建子项目继承父项目

<parent>
        <groupId>com.hp</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

创建watcher包

创建ZkDemo类内容如下

package com.hp.demo.watcher;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.CountDownLatch;

public class ZkDemo implements Watcher {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static ZooKeeper zooKeeper = null;
    private static Stat stat = new Stat();

    public static void main(String[] args) throws Exception {
        String path = "/xray";
        zooKeeper = new ZooKeeper("192.168.48.216:2181",5000,new ZkDemo());
        countDownLatch.await();
        byte[] data = zooKeeper.getData(path, true, stat);
        System.out.println(new String(data));
        Thread.sleep(Integer.MAX_VALUE);
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if(Event.KeeperState.SyncConnected == watchedEvent.getState()){
            if(Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()){
                countDownLatch.countDown();
            }else if(Event.EventType.NodeDataChanged == watchedEvent.getType()){
                try {
                    System.out.println("发生修改:" + new String(zooKeeper.getData(watchedEvent.getPath(),true,stat)));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

注意此类中链接的ip地址和端口

启动项目

操作你启动的zookeeper客户端

该项目控制台中中会打印信息。

## dubbo入门案例

创建父项目

父项目依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.8.0</version>
</dependency>

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.13</version>
</dependency>

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>

新建通用项目保存接口,继承父项目

此项目只写一个接口

package com.hp.demo.service;


public interface HelloService {
    String hello(String name);
}

创建服务提供者

只有一个实现类

package com.hp.provider_service.service;


import com.hp.demo.service.HelloService;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0",interfaceClass = HelloService.class)
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello!!" + name;
    }
}

服务提供者配置文件如下

server.port=6666
spring.application.name=provider-service
#Dubbo
dubbo.application.name=provider-service
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://192.168.48.216:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#dubbo扫描接口的包
dubbo.scan.base-packages=com.hp.provider_service.service

创建服务消费者继承父项目

配置文件如下

server.port=7707
dubbo.application.name=consumer-service
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://192.168.48.216:2181

服务消费者创建一个controller类

package com.hp.consumer_service.controller;

import com.hp.demo.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Reference(version = "1.0.0")
    private HelloService helloService;

    @RequestMapping("/hello")
    public String hello(String name){
        return helloService.hello(name);
    }
}

浏览器访问地址如下

localhost:7707/hello?name=zhangsan

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值