shenhp 发表于 2015-11-21 13:44:26

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=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:dubbo=&quot;http://code.alibabatech.com/schema/dubbo&quot;
xsi:schemaLocation=&quot;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
&quot;>
<!-- Application name -->
<dubbo:application name=&quot;hello-world-app&quot; />
<!-- registry address, used for service to register itself -->
<dubbo:registry address=&quot;zookeeper://127.0.0.1:2181&quot; />
<!-- expose this service through dubbo protocol, through port 20880 -->
<!--
<dubbo:protocol name=&quot;dubbo&quot; port=&quot;20880&quot; />
<dubbo:protocol name=&quot;dubbo&quot; port=&quot;9090&quot; server=&quot;netty&quot;
client=&quot;netty&quot; codec=&quot;dubbo&quot; serialization=&quot;hessian2&quot; charset=&quot;UTF-8&quot;
threadpool=&quot;fixed&quot; threads=&quot;100&quot; queues=&quot;0&quot; iothreads=&quot;9&quot; buffer=&quot;8192&quot;
accepts=&quot;1000&quot; payload=&quot;8388608&quot; />
-->
<!-- Service interface   Concurrent Control-->
<dubbo:service interface=&quot;com.bestpay.dubbo.provider.IProcessData&quot;
ref=&quot;demoService&quot; executes=&quot;10&quot; />
<!-- Default Protocol -->
<!--
<dubbo:protocol server=&quot;netty&quot; />
-->
<!-- designate implementation -->
<bean id=&quot;demoService&quot; class=&quot;com.xxx.dubbo.provider.ProcessDataImpl&quot; />
</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[]{&quot;provider.xml&quot;});
context.start();
System.out.println(&quot;Press any key to exit.&quot;);
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[]{&quot;consumer.xml&quot;});
context.start();
IProcessData demoService = (IProcessData) context.getBean(&quot;demoService&quot;); // get
// service
// invocation
// proxy
String hello = demoService.deal(&quot;nihao&quot;); // do invoke!
System.out.println(Thread.currentThread().getName() &#43; &quot; &quot;&#43;hello);
}
}


consumer.xml  
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:dubbo=&quot;http://code.alibabatech.com/schema/dubbo&quot;
xsi:schemaLocation=&quot;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
&quot;>
<!-- consumer application name -->
<dubbo:application name=&quot;consumer-of-helloworld-app&quot; />
<!-- registry address, used for consumer to discover services -->
<dubbo:registry address=&quot;zookeeper://127.0.0.1:2181&quot; />
<dubbo:consumer timeout=&quot;5000&quot;/>
<!-- which service to consume? -->
<dubbo:reference id=&quot;demoService&quot; interface=&quot;com.xxx.dubbo.provider.IProcessData&quot; />
</beans>


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



  


  
页: [1]
查看完整版本: Dubbo zookeeper 初探