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文件,增加以下依赖
2.创建相关服务接口
新建一个DemoService接口
3.实现相关服务
实现DemoService接口
4.创建测试服务提供者和消费者
创建服务提供者:
创建消费者
5.通过xml文件来配置
在resources下创建provider.xml
在resources下创建consumer.xml
6.测试
先保证zookeeper已经启动,在通过provider.java启动服务,最后运行consumer.java来测试。测试输出:
这是一个开源项目,项目地址: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
相关文章推荐
- spring 整合dubbo 测试搭建
- 搭建dubbo测试环境
- dubbo测试项目搭建
- ELK实践系列-测试环境环境搭建
- maven dubbo zookeeper 项目搭建(有效)jar包非war测试
- 搭建dubbo+使用zookeeper作为注册中心+测试小案例
- 实践作业2:黑盒测试实践——搭建被测web系统Day 4
- Docker学习总结(23)——Docker搭建大规模测试环境的实践
- 搭建dubbo测试环境
- Appium测试环境搭建实践
- Dubbo服务,后台管理,监控中心搭建的简单实践
- fltk在Ubuntu Linux里面的搭建和测试--<<C++程序设计原理与实践>>Chapter12:一个显示模型 环境构建篇
- 本地Windows环境Dubbo搭建测试
- Web 渗透测试入门:环境搭建、流程与实践
- vue组件测试用例实践一(搭建vue测试框架)
- android单元测试最佳实践一:android studio测试环境搭建
- Dubbo + zookeeper 测试环境的搭建
- Robotium在AndroidStudio中搭建及参数化测试实践
- dubbo测试搭建 --- 使用maven设计模块(二)
- Dubbo入门,搭建Dubbo 测试服提供与消费