Dubbo介绍1- Hello World例子
介绍
dubbo是阿里巴巴的开源RPC框架。阿里巴巴Dubbo实现的源码分析?这篇文章介绍的挺好的。对应的主页是?http://code.alibabatech.com/wiki/display/dubbo/Home? 。看dubbo主要是想学习一下对应的ClassLoader的隔离机制。不过所有技术都总HelloWorld开始,那就mark一下吧。
?
正文我是通过zk做服务集群管理的。所以如果要跑下面这个程序,需要自己搭一个zk集群?
对应的接口
?
package demo.service;/** * User: zhenghui * Date: 14-1-13 * Time: 上午10:40 */public interface DemoService { public void sayHello();}
?实现类
package demo.service;/** * User: zhenghui * Date: 14-1-13 * Time: 上午10:41 */public class DemoServiceImpl implements DemoService { @Override public void sayHello() { System.out.println("hello 00!"); }}
?
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 "> <dubbo:application name="hello-world-app" /> <dubbo:registry protocol="zookeeper" address="10.125.195.174:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="demo.service.DemoService" ref="demoService" /> <!-- 和本地bean一样实现服务 --> <bean id="demoService" /></beans>
?
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * User: zhenghui * Date: 14-1-13 * Time: 下午1:54 */public class DubboProviderDemo { public static void main(String[] args) throws InterruptedException { new ClassPathXmlApplicationContext( new String[] {"demo/provider.xml"}); while (true){} }}
?
comsumer?
<?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="consumer-of-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry protocol="zookeeper" address="10.125.195.174:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="demo.service.DemoService" /></beans>
?
import demo.service.DemoService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * User: zhenghui * Date: 14-1-13 * Time: 下午1:54 */public class DubboComsumeDemo { public static void main(String[] args) throws InterruptedException { ApplicationContext factory = new ClassPathXmlApplicationContext( new String[] {"demo/comsumer.xml"}); DemoService demoService = (DemoService) factory.getBean("demoService"); demoService.sayHello(); }}
?
先运行?DubboProviderDemo 然后再运行DubboComsumeDemo ,然后就可以看到在DubboProviderDemo的console中打印 “hello 00!”
?
后面我会继续介绍dubbo,计划下一篇内容是它是如何启动provider的。