Dubbo入门第一例
2016-07-28 10:24
295 查看
1、环境准备
----|eclipse----|zookeeper-3.4.8
----|jar包
-------->spring-framework-3.1.3.RELEASE/dist下所有包
-------->dubbo及其依赖jar包:
+commons-logging-1.1.1.jar
2、Provider端
----|新建java项目Provider,加入响相应jar包。----|Provider/src目录下新建包com.wind.provider。
----|com.wind.provider包下新建接口DemoService:
package com.wind.provider;
public interface DemoService {
String sayHello(String name);
}
----|com.wind.provider包下新建DemoServide的实现类DemoServiceImpl:
package com.wind.provider;
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
----|Provider/src目录下新建配置文件applicationContext-dubbo.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="provider-demo" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:service interface="com.wind.provider.DemoService"
ref="demoService" />
<bean id="demoService" class="com.wind.provider.DemoServiceImpl" />
</beans>
----|com.wind.provider包下新建服务测试类Provider:
package com.wind.provider;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext-dubbo.xml");
context.start();
System.out.println("provider run.");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
----|此时可先运行zookeeper,再运行Provider.java,若控制台出现“provider run.”说明服务端配置成功。
3、消费端
----|新建java项目Consumer----|导入相关jar包
----|新建包com.wind.consumer
----|com.wind.consumer包下新建消费者测试类Consumer:
package com.wind.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wind.provider.DemoService;
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
context.start();
DemoService demoService=(DemoService) context.getBean("demoService");
String destStr = demoService.sayHello("Marcus");
System.out.println(destStr);
}
}
----|在src目录下新建applicationContext-dubbo.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schem
4000
a/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="consumer-demo" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:reference id="demoService" interface="com.wind.provider.DemoService" />
</beans>
----|运行Consumer,控制台出现"Hello Macus"说明调用服务成功。
相关文章推荐
- 剑指Offer——数组的整数次方pow
- tomcat宕机自动重启和每日定时启动tomcat【工具包系列】
- Development cannot be enabled while your device is locked.
- 关于view的Touch事件的一些理解
- U_boot 的 bootcmd 和bootargs参数详解
- 级联下拉菜单的实现
- App Thinning研究
- sql查询数据库表中重复记录方法
- Android 使用MediaStore.Images和 Cursor查询本地图片和图片缩略图
- MySQL 5.7并行复制时代
- tyvj 1059 过河 dp
- Mysql行号
- Perl参考函数
- POJ - 3311 Hie with the Pie
- VS2008快捷键总结
- UIScrollView的循环滚动+UIPageControll
- UVA 11149 Power of Matrix(矩阵+二分)
- Android开发笔记(-)初识Android中的px,sp,dp
- GitHub相关
- Pro JPA2读书笔记系列(十二)-第十一章(高级主题)