简单整合zookeeper与dubbo

一、dubbo

简单的说,dubbo是个分布式服务框架,分为服务提供方provider和消费者consumer

一张图说明:

这里使用zookeeper作为dubbo的注册中心

(这里可以下载dubbo的服务控制台,方便查看服务方和消费方的信息)

二、zookeeper

       下载地址 http://www.apache.org/dyn/closer.cgi/zookeeper/

解压后打开conf,复制一份zoo_sample.cfg改名为zoo.cfg,修改内容

# 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=D:\\CHAN\\zk\\tmp\\zookeeper
# 错误日志的存放位置  
dataLogDir=D:\\CHAN\\zk\\logs\\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
在bin目录下打开cmd,执行zkServer.cmd,zookeeper便启动了

三、spring整合dubbo的example

建立一个简单的maven工程引入spring,dubbo和zkClient的jar包依赖,

建立application-provider.xml,引入dubbo命名空间,内容如下

<?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
		">
	<!-- 具体实现bean -->
	<bean id="demoService" class="com.chan.dubbo.provider.impl.DemoServiceImpl" />
	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="chan_provider" />
	<!-- 使用zookeeper註冊中心暴露服務地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 声明需要报的服务接口 -->
	<dubbo:service interface="com.chan.dubbo.provider.DemoService"
		ref="demoService" />

</beans>
在服务提供方的配置中将接口暴露出去,并指定实现类

这里可以通过tomcat启动项目,也可以javaApplication模拟web启动

package com.chan.dubbo.provider.impl;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "application.xml" });  
        context.start();  
        System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟  
	}
}
运行之后,provider就处于一直运行中,此时可以建立消费方来调用服务提供方的接口;


在main/resources下建立application-consumer.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="chan_consumer" />
	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<!-- 生成远程服务代理,可以先使用本地bean一样使用demoService -->
	<dubbo:reference id="demoService"
		interface="com.chan.dubbo.provider.DemoService" />
</beans>
消费方可以注入使用服务方提供的DemoService的实现类,调用具体的方法:

package com.chan.dubbo.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chan.dubbo.provider.DemoService;

public class Consumer {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-consumer.xml"});
		context.start();
		
		
		DemoService demoService = (DemoService) context.getBean("demoService");
		System.out.println(demoService.sayHello("daishenglei"));
		System.out.println(demoService.getUsers());
	}
}

这样duboo使用zookeeper作为注册中心的简单实例便完成了~



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值