官方网站
http://dubbo.io/
开发工具 eclipse
环境 windows 7 、安装的 JDK 1.8 (我这里实际项目中配置的Jre是 JavaSE 1.7)
如果提示 java.lang.NoClassDefFoundError: org/I0Itec/zkclient/IZkStateListener 说明缺少 zookeeper 客户端引用,本文已经添加引用。
注册中心使用 zookeeper
1、去官方网站下载 zookeeper,我使用的是 3.4.11 (自行度娘官网)
a、随便解压到某个磁盘中,然后找到conf 文件夹,将 zoo_sample.cfg 改为 zoo.cfg,zookeeper 默认读取的名字是zoo.cfg
b、到 bin 目录中双击 zkServer.cmd 来启动服务
c、启动成功的图如下,能看到默认端口号(2181),并且没有异常
2、首先建立maven工程
一个parent 项目,三个子项目,结构如图:
3、在父级pom.xml 中引入相关包,代码如下
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
4、根据官方例子操作
a、在 api 子项目中增加配置文件、编写接口(如图)
b、在服务生产子项目中引入上一步创建的接口,然后实现接口,pom 以及实现代码如下
<dependencies>
<dependency>
<groupId>com.ai8s</groupId>
<artifactId>demo-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
服务启动类如下:
/**
* @author Aaron·Li
* @date 2017年12月1日 下午5:20:20
*/
public class BootStart {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "provider.xml" });
context.start();
System.in.read(); // press any key to exit
}
}
服务实现类如下:
public class DemoServiceImpl implements IDemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
配置文件需要放到 maven 项目结构中的 resource 目中,配置信息如下(文件名 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="demo-provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="com.ai8s.demo.IDemoService"
ref="demoService" />
<bean id="demoService" class="com.ai8s.demo.provider.DemoServiceImpl" />
</beans>
c、消费端(调用者)配置如下
调用类
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"consumer.xml"});
context.start();
IDemoService demoService = (IDemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
String hello = demoService.sayHello("world"); // execute remote invocation
System.out.println(hello); // show the result
}
}
pom 配置
<dependencies>
<dependency>
<groupId>com.ai8s</groupId>
<artifactId>demo-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<?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="demo-consumer" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:reference id="demoService"
interface="com.ai8s.demo.IDemoService" />
</beans>
5、启动服务生产者,如果不出意外会提示如下信息
6、启动消费者,结果如下
最后有一个问题,不知道什么原因,我关掉服务生产者一段时间后,再重新打开等待一会儿会报异常(后来立刻关闭,再启动没有报错),这里记录一下问题,有时间研究一下怎么回事。