dubbo入门
一、下载zookeeper注册中心
- 下载zookeeper压缩包并解压,官网地址:http://www.apache.org/dyn/closer.cgi/zookeeper/
- 进入conf目录将 zoo_sample.cfg 改名为 zoo.cfg。
- 进入bin目录双击zkServer.cmd,若启动成功,则windows单机版zookeeper搭建成功。
二、配置dubbo-admin监控中心
- 下载dubbo-admin的代码,下载地址:https://github.com/apache/incubator-dubbo-ops。
- 下载后将其解压,进入如图目录下面。
3. 使用cmd进入该目录下面,然后执行 mvn package -Dmaven.test.skip=true(安装maven才有效),等待项目打包。
4. 执行完毕后进入 dubbo-admin目录下面的target目录,会有一个dubbo-admin-0.0.1-SNAPSHOT的jar包。
5. cmd进入到jar包所在目录 ,执行 java -jar dubbo-admin-0.0.1-SNAPSHOT.jar 命令(在这一步之前必须开启zookeeper)。
6. 运行完毕后在浏览器输入 http://127.0.0.1:7001,输入用户名 root 密码 root即可登陆。登陆完成页面如下。
三、创建dubbo的maven项目。
- 创建一个maven父工程,本工程只有一个pom文件,内容如下。
<?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>com.dobbu</groupId> <artifactId>dobbutest</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-provider</module> <module>dubbo-consumer</module> </modules> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring.version>4.2.5.RELEASE</spring.version> <slf4j.version>1.7.18</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </dependencyManagement> </project>
2.在上一个maven项目下面见一个maven moudle,这个maven moudle只定义接口。
3.在创建一个maven moudle来作为 提供者,其目录结构如下。
DemoServiceImpl.java代码如下
public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return name; } }
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 address="zookeeper://127.0.0.1:2181"/> <!--使用dubbo协议暴露端口--> <dubbo:protocol name="dubbo" port="20880"/> <!--暴露需要暴漏的接口--> <dubbo:service interface="com.dobbu.DemoService" ref="demoService" /> </beans>
springmvc.xml文件内容
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" > <bean id="demoService" class="com.dobbu.DemoServiceImpl"/> <import resource="dubbo-provider.xml"/> </beans>
AppTest.java文件内容
public class AppTest { @Test public void shouldAnswerWithTrue() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:springmvc.xml"); context.start(); System.out.println("Dubbo provider start....."); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
4.创建一个maven moudle来充当消费者。其目录结构如下:
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="zookeeper://127.0.0.1:2181"/> <dubbo:reference interface="com.dobbu.DemoService" id="demoService"/> </beans>
springmvc.xml文件内容如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd" default-autowire="byName"> <import resource="dubbo-consumer.xml"/> </beans>
AppTest.java内容如下
public class AppTest { /** * Rigorous Test :-) */ @Test public void shouldAnswerWithTrue() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:springmvc.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); System.out.println(demoService.sayHello("hhh")); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
四、测试项目
首先运行dubbo-provider项目的AppTest的测试方法,执行后可以在dobbu-admin监控页面看见该提供者。如图则表示服务已经注册。
运行dubbo-consumer的AppTest类的方法。会在控制台输出如下消息,并且在消费者里面可以查看到该服务。