浅谈dubbo和Zookeeper分布式技术的使用

一、基本概念:


1)Dubbo是管理中间层的工具,在业务层到数据仓库间有非常多服务的接入和服务提供者需要调度,
  dubbo提供一个框架解决这个问题。这个框架要完成调度必须要有一个分布式的注册中心,
   存储所有服务的元数据,用到zookeeper。

2)zookeeper用来注册服务和负载均衡。
 

二、dubbo和zookeeper的使用:


1、引入相关依赖

下面的放在父工程下:

<!-- 集中定义依赖版本号 -->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>5.0.5.RELEASE</spring.version>
        <pagehelper.version>4.1.4</pagehelper.version>
        <servlet-api.version>2.5</servlet-api.version>
        <dubbo.version>2.6.0</dubbo.version>
        <zookeeper.version>3.4.7</zookeeper.version>
        <zkclient.version>0.1</zkclient.version>
        <mybatis.version>3.4.5</mybatis.version>
        <mybatis.spring.version>1.3.1</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <druid.version>1.0.9</druid.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <spring.security.version>5.0.5.RELEASE</spring.security.version>
    </properties>
 <!-- dubbo和Zookeeper相关 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
            <dependency>
                <groupId>javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.12.1.GA</version>
            </dependency>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
            </dependency>



下面的放在公共模块下:


<!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>

 
2、在公共模块的resources添加zk.properties配置文件。

zk.address=localhost:2181


3、在服务公共模块的resources添加服务公共模块applicationContext-dubbo.xml配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	<!--指定暴露服务的端口,如果不指定默认为20880-->
	<dubbo:protocol name="dubbo" port="${dubbo.port}"/>
	<dubbo:application name="${dubbo.application}" />
	<dubbo:registry protocol="zookeeper" address="${zk.address}" />
	<dubbo:annotation package="com.qingcheng.service" />
	<!--<context:annotation-config/>-->
</beans>


4、在web公共模块的resources添加web公共模块的applicationContext-dubbo.xml配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 引用dubbo 服务 -->
	<dubbo:application name="${dubbo.application}" />
	<dubbo:registry protocol="zookeeper" address="${zk.address}"/>
	<dubbo:annotation package="com.qingcheng.controller" />

</beans>


5、在服务子模块的resources添加服务子模块dubbo.properties配置文件。

dubbo.port=20881
dubbo.application=goods


6、在web子模块的resources添加web子模块的dubbo.properties配置文件。

dubbo.application=manager


7、解压zookeeper安包,在conf下添加zoo.cfg配置文件,要使用时,在bin下双击启动zkServer.cmd

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=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

zookeeper安装包如果有需要可以私聊!!!

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Dubbo是一个高性能的Java RPC框架,它提供了分布式服务框架的支持,可以让开发者轻松地构建分布式应用。而Zookeeper是一个分布式的协调服务,它可以用来管理和协调分布式系统中的各种资源,如配置信息、命名服务、分布式锁等。 当使用Dubbo框架时,我们可以将服务注册到Zookeeper中,这样其他的服务消费者就可以通过Zookeeper来发现和调用这些服务。同时,Zookeeper还可以用来监控服务的状态,当服务出现故障时,Zookeeper可以及时地通知其他服务消费者,从而保证整个分布式系统的稳定性和可靠性。 因此,DubboZookeeper的结合可以为分布式系统的开发和运维提供很好的支持,让开发者可以更加轻松地构建高性能、可靠的分布式应用。 ### 回答2: DubboZookeeper是两个分布式框架,结合起来可以帮助开发者构建高性能、高可用性的分布式应用。Dubbo提供了RPC远程过程调用框架,可以实现跨进程、跨语言的方法调用,支持负载均衡、容错、限流等特性。而Zookeeper是一个开源的分布式协调服务框架,可以协调和管理分布式应用的节点之间的通信。下面详细介绍DubboZookeeper分布式基础。 1、Dubbo Dubbo是一款高性能、轻量级的RPC框架,采用Java编写,支持多种协议(Dubbo协议、HTTP、Hessian、RMI等),通过注册中心连接提供者和消费者。Dubbo可以做到请求耗时的优化、负载均衡、集群容错、异步调用等特性,让开发者可以轻松地构建分布式应用。 Dubbo的工作流程如下:服务提供者(Provider)将服务注册到注册中心,服务消费者(Consumer)从注册中心订阅服务,当服务消费者需要调用服务时,会通过代理对象来远程调用服务提供者,完成方法调用并将结果返回。Dubbo自带了多种注册中心的实现,例如Zookeeper、Redis等。 2、Zookeeper Zookeeper是一个开源的分布式协调服务框架,可以用来协调和管理分布式应用的节点之间的通信。Zookeeper的主要作用是提供一个分布式的协调服务,例如分布式锁、配置管理、分布式队列等。Zookeeper可以减轻分布式应用的复杂性,让开发者可以专注于业务逻辑。 Zookeeper通过基于目录树结构的数据模型来保存数据,并提供了一套完整的API用于操作这些数据。Zookeeper的工作原理是基于主从架构,当Zookeeper集群中的节点出现故障时,可以自动选举出新的Leader节点以继续提供服务。 DubboZookeeper结合起来可以构建一个高可用性的分布式应用。Dubbo将服务注册到Zookeeper,服务消费者从Zookeeper订阅服务,同时Zookeeper提供了注册中心的高可用性和负载均衡等特性。当服务提供者出现故障时,Zookeeper可以自动重新分配服务提供者的角色,从而保证服务的高可用性。 ### 回答3: DubboZookeeper是目前流行的分布式系统基础组件,它们常常被一起使用来构建可靠的分布式服务。在现代复杂的分布式系统中使用基础组件可以极大地简化开发和部署工作,而测试和维护都变得更容易。 Dubbo是一种高性能的分布式服务框架,它提供了统一的服务管理和RPC调用机制。Dubbo的核心在于将服务提供方和服务消费方解耦,同时提供了动态负载均衡和服务治理等功能。Dubbo具有高性能、可靠、易扩展等优点,广泛应用于大型分布式系统中。 Zookeeper是一种开源的分布式配置管理和协调解决方案,可以实现分布式配置管理、命名服务、分布式锁、集群选主和消息队列等功能。Zookeeper具有高可用性、可靠性和易扩展性,可以轻松管理大型分布式系统的配置和状态信息。Zookeeper还提供了建立分布式协调基础设施的基础组件。 在实际应用中,Dubbo通常和Zookeeper一起使用。服务提供方将服务注册到Zookeeper中心,服务消费方从Zookeeper中获取服务地址,并进行RPC调用,通过Dubbo提供的高性能、高可靠性的RPC调用机制实现分布式服务的调用和管理。 总之,DubboZookeeper分布式系统基础构件,通常会共同使用来实现高性能、高可靠性的分布式服务。对于分布式开发者来说,掌握这些基础组件的使用和原理,可以对分布式开发和维护提供很大帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值