dubbo学习2:安装zookeeper 环境下的dubbo例子

 

 先要在自己的电脑上配置zookeeper注册中心   在百度上搜到的资料 :

 可以再http://hadoop.apache.org/zookeeper/来获取 

Zookeeper 的启动脚本在 bin 目录下,Windows 下的启动脚本是 zkServer.cmd。

在你执行启动脚本之前,还有几个基本的配置项需要配置一下,Zookeeper 的配置文件在 conf 目录下,这个目录下有 zoo_sample.cfg 和 log4j.properties,你需要做的就是将 zoo_sample.cfg 改名为 zoo.cfg,因为 Zookeeper 在启动时会找这个文件作为默认配置文件。下面详细介绍一下,这个配置文件中各个配置项的意义。

# 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=C:\\zookeeper-3.4.5\\data
dataLogDir=C:\\zookeeper-3.4.5\\log
# the port at which the clients will connect
clientPort=2181
#
# 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
  • tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
  • dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
  • dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录
  • clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

当这些配置项配置好后,你现在就可以启动 Zookeeper 了,启动后要检查 Zookeeper 是否已经在服务,可以通过 netstat – ano 命令查看是否有你配置的 clientPort 端口号在监听服务。

接下来就是修改服务端applicationProvider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="hello-world-app" />

	<!-- 使用zookeeper广播注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<!-- 使用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>

	<!-- 和本地服务一样实现远程服务 -->
	<bean id="demoService" class="cn.zto.service.impl.ProcessDataImpl"></bean>

	<!-- 保留远程服务配置 -->
	<dubbo:service interface="cn.zto.service.IProcessData" ref="demoService"></dubbo:service>


</beans>

最后是修改客户端的applicationConsumer.xml配置

<dubbo:registry address="multicast://224.5.6.7:1234" />

 改为

<dubbo:registry address="zookeeper://127.0.0.1:2181" />

 就行了   分别  启动  zkServer.cmd   服务端  客户端  OK

猜你喜欢

转载自a67474506.iteye.com/blog/2079606