您的位置:首页 > 其它

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"说明调用服务成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: