1. dubbo简介
dubbo是阿里巴巴旗下研发的分布式系统,由于配置和使用相对灵活广受企业使用。
http://blog.csdn.net/lovesummerforever/article/details/48138393以上网址详细介绍,本人就不一一说了,上代码,亲测。
2. 项目搭建
前期准备
zookeeper安装请看相关jar包
我采用的是maven下面放上依赖关系。
dubbo-admin
非必须,用这个可以查看dubbo服务注册的pom.xml
pom.xml文件主要分三部分dubbo+zookeeper+zkclient
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
- 新建源目录(source)存放dubbo 生产者消费者配置文件如图:
-新建TestService.java
package com.test.dubbo.api;
public interface TestService {
void sayhello(String name);
}
新建TestServiceImpl.java
package com.test.dubbo.api;
public class TestServiceImpl implements TestService{
public void sayhello(String name) {
System.out.println(name+"hello");
}
}
生产者
package com.test.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.test.dubbo.api.TestService;
/**
* CallbackConsumer
*
* @author william.liangf
*/
public class TestConsumer {
public static void main(String[] args) throws Exception {
String path = "classpath:consumer.xml";
GenericXmlApplicationContext xml = new GenericXmlApplicationContext(path);
xml.start();
System.out.println("------------");
TestService test = (TestService) xml.getBean("testService");
for(int i=0;i<100;i++){
test.sayhello(i+"");
}
System.in.read();
}
}
生产者对应xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="test-provider"/>
<dubbo:registry address="zookeeper://xxx.xxx.xxx.xxx:2181" />
<dubbo:protocol name="dubbo" port="20881"/>
<bean id="testService" class="com.test.dubbo.api.TestServiceImpl"></bean>
<dubbo:service interface="com.test.dubbo.api.TestService"
ref="testService"/>
</beans>
消费者示例
package com.test.dubbo;
import java.io.IOException;
import org.springframework.context.support.GenericXmlApplicationContext;
public class TestProvider {
public static void main(String[] args) throws IOException {
String path = "classpath:provider.xml";
GenericXmlApplicationContext xml = new GenericXmlApplicationContext(path);
xml.start();
System.out.println("------------");
System.in.read();
}
}
消费者xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="async-consumer"/>
<dubbo:registry address="zookeeper://xxx.xxx.xxx.xxx:2181" />
<dubbo:reference id="testService" interface="com.test.dubbo.api.TestService"/>
</beans>
- 启动
先运行TestProvider.java,如图所示启动注册成功
运行TestConsumer.java消费注册的服务出现图示代表成功
ps:源代码下载地址http://pan.baidu.com/s/1c2kzI84