zookeeper的安装与使用

本文介绍了如何下载和使用Zookeeper作为Dubbo服务的注册中心,包括安装步骤,以及如何在Dubbo框架下创建服务提供方和服务消费者,通过SpringBoot集成并进行简单的测试案例。
摘要由CSDN通过智能技术生成

一、下载zookeeper

  •     Zookeeper是Apacahe Hadoop的子项目,可以为分布式应用程序协调服务,适合作为Dubbo服务的注册中心,负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互。

   使用百度网盘分享链接

链接:https://pan.baidu.com/s/1hddTGBzEQGMFQYh28PUAsw?pwd=md5u 
提取码:md5u

解压后,进入bin目录 点击zkServer.cmd

然后可以看到自己的端口2181即可。

 二、dubbox框架介绍

    Dubbo(读音[ˈdʌbəʊ])是阿里巴巴公司开源的一个基于Java的高性能RPCRemote Procedure Call框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。后期阿里巴巴停止了该项目的维护,于是当当网在这之上推出了自己的Dubbox。

节点角色说明:

  • Provider: 暴露服务的服务提供方。
  • Container: 服务运行容器。
  • Registry: 服务注册与发现的注册中心。
  • Consumer: 调用远程服务的服务消费方。
  • Monitor: 统计服务的调用次调和调用时间的监控中心。
  • 调用关系说明:
  • 0. 服务容器负责启动,加载,运行服务提供者。
  • 1. 服务提供者在启动时,向注册中心注册自己提供的服务。
  • 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  • 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  • 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  • 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

 三、dubbox入门案例

3.1 创建父工程 ,引入依赖控制版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    
    <groupId>com.by</groupId>
    <artifactId>dubbox_parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- 由于使⽤了zookeeper作为注册中⼼,则需要加⼊zookeeper的客户端jar包: -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>
</project>

3.2 定义公共接口 

   里面只简单写一个service接口

  • HeflloService接口
package com.by.service;


public interface HeflloService {
    String hello();
}

 3.3 创建服务提供方模块

①pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.by</groupId>
        <artifactId>dubbox_parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>dubbox_provider</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
  <dependencies>
      <dependency>
          <groupId>com.by</groupId>
          <artifactId>dubbox_interface</artifactId>
          <version>1.0-SNAPSHOT</version>
      </dependency>
  </dependencies>

</project>

②创建service的实现类HelloServiceImpl.Java

package com.by.service;

import com.alibaba.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HeflloService{
    @Override
    public String hello() {
        return "hello,Dubbox.......";
    }
}

③application.properties

server.port=8080
#zookeeper??
dubbo.registry.address=zookeeper://127.0.0.1:2181
#???????,??????????????????
dubbo.application.name=dubbo-consumer
#??, Dubbo??????Dubbo?RMI?http?WebService
dubbo.protocol.name=dubbo

④创建启动类


package com.by;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboxConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(DubboxConsumerApp.class,args);
    }
}

3.4 定义服务消费方

①pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.by</groupId>
        <artifactId>dubbox_parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>dubbox_consumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.by</groupId>
            <artifactId>dubbox_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

 ②定义定义controller


package com.by.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.by.service.HeflloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class HelloController {
    @Reference
  private    HeflloService heflloService;
    @RequestMapping("/hello")
    @ResponseBody // 想给浏览器返回一个json
    public String hello(){
       return heflloService.hello();
    }

}

③ application.properties

server.port=8081
#zookeeper??
dubbo.registry.address=zookeeper://127.0.0.1:2181
#???????,??????????????????
dubbo.application.name=dubbo-consumer
#??, Dubbo??????Dubbo?RMI?http?WebService
dubbo.protocol.name=dubbo

④定义启动类DubboxConsumerApp.java


package com.by;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboxConsumerApp {
    public static void main(String[] args) {
        SpringApplication.run(DubboxConsumerApp.class,args);
    }
}

四、测试

首先打开zookeeper,然后先启动服务提供方,接着启动服务消费方(注意顺序) 

然后在浏览器中搜索服务消费方的网址即可。

  • 19
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: ZooKeeper是一个用于分布式应用程序的开源协调服务。它可以用作配置中心、高可用协调服务以及分布式锁的开发工具。在Windows环境下安装ZooKeeper可以按照以下步骤进行操作:首先,从官网下载ZooKeeper安装包,并解压进入bin目录。然后,双击执行zkServer.cmd文件,如果遇到闪退,可以修改命令文件,在最后添加暂停命令pause,再次双击执行。也可以通过命令行执行start ZooKeeper命令。接下来,需要创建zoo.cfg文件,可以直接复制一份进行使用。最后,启动ZooKeeper服务后,可以通过执行zkCli.cmd命令启动客户端进程,并进行简单的测试使用。\[2\]在Linux环境下安装ZooKeeper的步骤类似,需要上传并解压tar文件,配置文件,然后启动ZooKeeper服务。\[1\]总的来说,ZooKeeper安装使用相对简单,但是对于分布式应用程序的开发和理解来说,学习和熟练使用ZooKeeper是十分有必要的。\[3\] #### 引用[.reference_title] - *1* *2* [zookeeper安装使用(win+linux)](https://blog.csdn.net/suixinfeixiangfei/article/details/122147864)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [安装以及简单使用zookeeper](https://blog.csdn.net/qq_31960623/article/details/121201392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

今天的接口写完了吗?

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

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

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

打赏作者

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

抵扣说明:

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

余额充值