Dubbo zookeeper 初探

1.zookeeper

http://zookeeper.apache.org/ 官网下载,然后安装

启动时,需要将 zoo_sample.cfg 改成 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=D:\\Zookeeper-3.4.5\\data
# 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

2. dubbo-demo-api  定义接口

public interface IProcessData {
	public String deal(String data);
}

3.dubbo-demo-provider 服务提供者

public class ProcessDataImpl implements IProcessData {

	/* 
	 * @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String)
	 */
	@Override
	public String deal(String data) {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "Finished:" + data;
	}
}

provider.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
        ">

	<!-- Application name -->
	<dubbo:application name="hello-world-app" />

	<!-- registry address, used for service to register itself -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<!-- expose this service through dubbo protocol, through port 20880 -->
	<!--
	<dubbo:protocol name="dubbo" port="20880" />
	
	<dubbo:protocol name="dubbo" port="9090" server="netty"
		client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8"
		threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192"
		accepts="1000" payload="8388608" />
		-->
	<!-- Service interface   Concurrent Control  -->
	<dubbo:service interface="com.bestpay.dubbo.provider.IProcessData"
		ref="demoService" executes="10" />

	<!-- Default Protocol -->
	<!--
	<dubbo:protocol server="netty" />
	-->

	<!-- designate implementation -->
	<bean id="demoService" class="com.xxx.dubbo.provider.ProcessDataImpl" />

</beans>

启动服务
public class DubboProviderMain {

	/**
	 * @Title main
	 * @Description TODO
	 * @Author weizhi2018
	 * @param args
	 * @throws
	 */

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[]{"provider.xml"});
		context.start();

		System.out.println("Press any key to exit.");
		System.in.read();
	}
}

引用jar


3.dubbo-demo-consumer

public class ConsumerThd implements Runnable {

	/* 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[]{"consumer.xml"});
		context.start();

		IProcessData demoService = (IProcessData) context.getBean("demoService"); // get
																				// service
																				// invocation
		// proxy
		String hello = demoService.deal("nihao"); // do invoke!

		System.out.println(Thread.currentThread().getName() + " "+hello);
	}

}

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
        ">

	<!-- consumer application name -->
	<dubbo:application name="consumer-of-helloworld-app" />

	<!-- registry address, used for consumer to discover services -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
	<dubbo:consumer timeout="5000"/>
	<!-- which service to consume? -->
	<dubbo:reference id="demoService" interface="com.xxx.dubbo.provider.IProcessData" />
</beans>

4.dubbo-admin

下载dubbo-admin项目,部署到tomcat6下面,启动tomcat ,在浏览器打开:http://localhost:8080/dubbo-admin, 输入用户名/密码:root/root




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值