前几天换工作,到新公司用到了阿里的Dubbo,花了两天的时间去学习,在网上找了很多教程,感觉都不是太详细,所以动手搭了下环境,写了这篇XX。
有关dubbo的介绍就不多说了,请查阅官方文档:http://dubbo.io/
本次环境搭建用到的工具:IDEA,maven,zookeeper
首先介绍下项目的大概情况,首先是一个maven父工程DubboTest,下面有三个子工程DubboConsumer,DubboProvider,DubboCommon。
DubboTest:总的项目,父工程,需要的依赖都在这里配置。
DubboConsumer:非web项目,dubbo的消费方。
DubboProvider:非web项目,dubbo服务提供方。
DubboCommon:非web项目,打成Jar包,是DubboConsumer和DubboProvider共享的包,里面定义的是公用的接口。
以上相关的概念就不多做解释了。
项目代码:https://git.oschina.net/dachengxi/DubboTest.git
搭建项目:
1.打开IDEA,New Project,选中Maven项目,不要勾选Create from archetype,点击next,填写GroupId等信息,然后再填写其他的相关信息,这个工程命名DubboTest,是父项目。
2.进入项目之后,选择新建模块,分别简历三个子项目,过程与上面类似。分别命名为DubboConsumer,DubboProvider,DubboCommon。
下面分别列出上面的pom文件
DubboTest pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cheng.xi</groupId> <artifactId>test.dubbo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>DubboConsumer</module> <module>DubboProvider</module> <module>DubboCommon</module> </modules> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </project>
DubboConsumer pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>test.dubbo</artifactId> <groupId>cheng.xi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo.consumer</artifactId> <dependencies> <dependency> <groupId>cheng.xi</groupId> <artifactId>dubbo.common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
DubboProvider pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>test.dubbo</artifactId> <groupId>cheng.xi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo.provider</artifactId> <dependencies> <dependency> <groupId>cheng.xi</groupId> <artifactId>dubbo.common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
DubboCommon pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>test.dubbo</artifactId> <groupId>cheng.xi</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo.common</artifactId> <packaging>jar</packaging> </project>
3.项目搭建完成之后,就可以开始写代码了。
首先在DubboCommon项目中编写公共的接口,代码如下:
package dubbo.common.hello.service; /** * Created by cheng.xi on 15/4/12. */ public interface HelloService { public void sayHello(); }
接着写DubboProvider项目的接口实现,代码如下:
package dubbo.provider.hello.service.impl; import dubbo.common.hello.service.HelloService; /** * Created by cmcc on 15/4/12. */ public class HelloServiceImpl implements HelloService { @Override public void sayHello() { System.out.println("这里是Provider"); System.out.println("HelloWorld Provider!"); } }
下面是DubboProvider中启动服务的代码:
package dubbo.provider.hello.main; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; /** * Created by cheng.xi on 15/4/12. */ public class StartProvider { public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"}); context.start(); System.out.println("这里是dubbo-provider服务,按任意键退出"); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
最后编写DubboConsumer下的调用代码,此处使用单元测试的方式调用,代码如下:
package dubbo.consumer.hello.main; import dubbo.common.hello.service.HelloService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Created by cheng.xi on 15/4/12. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/dubbo-consumer.xml") public class StartConsumer { @Autowired private HelloService helloService; @Test public void test(){ System.out.println("dubbo-consumer服务启动,调用!"); helloService.sayHello(); } }
4.上面代码已经写好,其中需要用的几个配置文件如下所示:
dubbo-consumer.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="dubbo-consumer" /> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <dubbo:reference id="helloService" interface="dubbo.common.hello.service.HelloService" /> </beans>
dubbo-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="dubbo-provider" /> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <bean id="helloService" class="dubbo.provider.hello.service.impl.HelloServiceImpl" /> <dubbo:service interface="dubbo.common.hello.service.HelloService" ref="helloService" /> </beans>
5.至此项目中的代码编写已经完成,下一步是安装和启动zookeeper,有关过程请自己研究下。
6.启动好了zookeeper之后,首先运行DubboProvider中的那个main方法,然后运行DubboConsumer中的test()方法。
这就ok了。