Dubbo zookeeper 初探
2015-12-11 15:44
323 查看
转:/article/11074844.html
建议参考资料:
/article/8504047.html
http://blog.csdn.net/goliathray/article/details/8565801
/content/2966869.html
/article/3877350.html
http://www.open-open.com/news/view/1442a5c
http://code.alibabatech.com/wiki/display/dubbo/Home
http://code.alibabatech.com/wiki/display/dubbo/User+Guide-zh
先把zookeeper在本地给安装好,
安装方法参考:/article/7578550.html
这里的话讲述了两个工程一个工程是提供服务的,一个工程是调用服务的,因为dubbo是跟spring进行无缝连接的,故功能配置在spring的配置文件中,跟spring进行整合开发
1、工程是以maven进行构建的,使用的jar包如下:
2、服务提供者的工程
a、dubbo-demo-api 定义接口
[java] view
plaincopy
public interface IProcessData {
public String deal(String data);
}
b、dubbo-demo-provider 服务提供者
[java] view
plaincopy
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;
}
}
c、applicationProvider.xml配置
[html] view
plaincopy
<?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.huangjie.dubbo_Service.service.IProcessData"
ref="demoService" executes="10" />
<!-- Default Protocol -->
<!--
<dubbo:protocol server="netty" />
-->
<!-- designate implementation -->
<bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />
</beans>
d、启动服务
[java] view
plaincopy
public class DubboProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("Press any key to exit.");
System.in.read();
}
}
3、服务调用者的工程
a、调用类
[java] view
plaincopy
public class ConsumerThd implements Runnable {
/*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationConsumer.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);
}
public static void main(String[] args) {
new Thread(new ConsumerThd()).start();
/**
* 输出结果:
* Thread-0 Finished:nihao
*/
}
}
b、applicationConsumer.xml配置
[html] view
plaincopy
<?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.huangjie.dubbo_Service.service.IProcessData" />
</beans>
4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行
这样整个调用就完成了,这样的好处是只要远程提供ip地址及端口号,以及对外调用的类,客户端就可以调用,客户端不必知道服务端的地址之类的
而且服务端可以开多个zookeeper服务,这样如果其中一个zookeeper 服务死掉了,其他服务还能正常运行
工程下载路径:http://download.csdn.net/detail/wxwzy738/6553431
建议参考资料:
/article/8504047.html
http://blog.csdn.net/goliathray/article/details/8565801
/content/2966869.html
/article/3877350.html
http://www.open-open.com/news/view/1442a5c
http://code.alibabatech.com/wiki/display/dubbo/Home
http://code.alibabatech.com/wiki/display/dubbo/User+Guide-zh
先把zookeeper在本地给安装好,
安装方法参考:/article/7578550.html
这里的话讲述了两个工程一个工程是提供服务的,一个工程是调用服务的,因为dubbo是跟spring进行无缝连接的,故功能配置在spring的配置文件中,跟spring进行整合开发
1、工程是以maven进行构建的,使用的jar包如下:
2、服务提供者的工程
a、dubbo-demo-api 定义接口
[java] view
plaincopy
public interface IProcessData {
public String deal(String data);
}
b、dubbo-demo-provider 服务提供者
[java] view
plaincopy
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;
}
}
c、applicationProvider.xml配置
[html] view
plaincopy
<?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.huangjie.dubbo_Service.service.IProcessData"
ref="demoService" executes="10" />
<!-- Default Protocol -->
<!--
<dubbo:protocol server="netty" />
-->
<!-- designate implementation -->
<bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />
</beans>
d、启动服务
[java] view
plaincopy
public class DubboProviderMain {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("Press any key to exit.");
System.in.read();
}
}
3、服务调用者的工程
a、调用类
[java] view
plaincopy
public class ConsumerThd implements Runnable {
/*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationConsumer.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);
}
public static void main(String[] args) {
new Thread(new ConsumerThd()).start();
/**
* 输出结果:
* Thread-0 Finished:nihao
*/
}
}
b、applicationConsumer.xml配置
[html] view
plaincopy
<?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.huangjie.dubbo_Service.service.IProcessData" />
</beans>
4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行
这样整个调用就完成了,这样的好处是只要远程提供ip地址及端口号,以及对外调用的类,客户端就可以调用,客户端不必知道服务端的地址之类的
而且服务端可以开多个zookeeper服务,这样如果其中一个zookeeper 服务死掉了,其他服务还能正常运行
工程下载路径:http://download.csdn.net/detail/wxwzy738/6553431
相关文章推荐
- 指针常量与常量指针的区别[转帖]
- 遇到无法引用res文件的解决办法2
- 使用了过滤条件的 ArrayAdapter,向源数据添加或删除数据后刷新时,界面不会同步更新
- androidstudio 编辑器使用过程中的问题收集
- 在OpenStack里怎样配置Neutron,让虚拟机访问外网
- 如何使用jmeter来实现更大批量的并发的解决方案
- ubuntu 图形界面卡死,没有出来登录界面怎么办
- PHP下载网页图片
- dedecms 模板文件不存在 无法解析文档!问题定位方法!
- Android 开发总结
- MVC vs. MVP vs. MVVM
- C#获取网络图片
- php中json_decode返回数组或对象
- PC和树莓派socket通信 控制LED
- CSS控制图片大小不变形
- 我的PHP之旅
- 【FAQ】MAC,ping IP能通,ping域名不通,为何?
- 功能很全的PHP分页类
- MySQL集群简介与配置详解
- js 几种弹窗