您的位置:首页 > 其它

dubbo实践(一)搭建测试

2014-04-13 17:27 393 查看
最近在尝试使用soa框架dubbo,记录一些学习的过程,备忘。

这是一个开源项目,项目地址:https://github.com/alibaba/dubbo,中文文档地址目前是:http://alibaba.github.io/dubbo-doc-static/Home-zh.htm

大概阅读文档之后,就开始动手尝试了,主要是搭建一个基于zookeeper为注册中心的例子,关于zookeeper的安装,自行查找相关文档。

1.新建一个maven项目dubboTest,修改pom文件,增加以下依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.9</version>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>1.1.16</version>
</dependency>


  

2.创建相关服务接口

新建一个DemoService接口

package com.alibaba.dubbo.demo;

public interface DemoService {

String sayHello(String name);
}  


3.实现相关服务

实现DemoService接口

package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService{

public String sayHello(String name) {
return "Hello " + name;
}

}


  

4.创建测试服务提供者和消费者

创建服务提供者:

package com.alibaba.dubbo.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
context.start();
System.out.println("请按任意键退出");
System.in.read(); // 按任意键退出
}

}


创建消费者

package com.alibaba.dubbo.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {

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

DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String res = demoService.sayHello("world"); // 执行远程方法
System.out.println( res ); // 显示调用结果
}

}


5.通过xml文件来配置

在resources下创建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="dubbo-test-service"  />

<!-- 使用multicast广播注册中心暴露服务地址 -->
<!--  <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />

</beans>


在resources下创建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="dubbo-test-consumer"  />

<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<!--   <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />

</beans>


6.测试

先保证zookeeper已经启动,在通过provider.java启动服务,最后运行consumer.java来测试。测试输出:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hello world


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: