Dubbo入门第一例

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/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="consumer-demo" />


<dubbo:registry address="zookeeper://127.0.0.1:2181" />


<dubbo:reference id="demoService" interface="com.wind.provider.DemoService" />
</beans>

----|运行Consumer,控制台出现"Hello Macus"说明调用服务成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值