简单易懂dubbo入门实例
一、创建Maven多模块项目
项目结构如下
模块介绍:
dubbo-api ----API接口
dubbo-consumer ----消费者
dubbo-provider ----生产者
dubbo-api Service 提供的接口,provider 实现了 api 接口,并对外开放http端口,consumer依赖 api 工程,但是不会直接依赖provider工程。 这种可以让消费者仅仅依赖api工程,而不需要对provider工程的依赖,也是松耦合的经典。
(1)创建root项目
首先创建root项目,把一些通用的依赖全部放置到这里,步骤如下:
创建成功之后如下
把上面的src目录全部删除掉,只留下pom.xml
然后修改POM.XML如下成功如下:
- <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.fendo</groupId>
- <artifactId>Dubbo_demos</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>pom</packaging>
- <name>dubbo</name>
- <description>dubbo示例</description>
- <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版本号 -->
- <spring.version>4.2.5.RELEASE</spring.version>
- <!-- log4j日志包版本号 -->
- <slf4j.version>1.7.18</slf4j.version>
- <log4j.version>1.2.17</log4j.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <!-- 添加junit4依赖 -->
- <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>
- <!-- 添加日志相关jar包 -->
- <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>
- <!-- dubbo -->
- <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>
- <!-- Zookeeper zkclient-->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
(2)创建api项目
新建一个Module
输入module名,选择类型为java项目
点击"Finish",创建完成如下
他会自动在root目录下把dubbo-api这么module加入进来。而dubbo-api会自动继承root
(3)创建provider(生产者)
点击"Finish"生成如下
修改POM.XML为如下:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <artifactId>Dubbo_demos</artifactId>
- <groupId>com.fendo</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.fendo</groupId>
- <artifactId>dubbo-provider</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>dubbo-provider</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.fendo</groupId>
- <artifactId>dubbo-api</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </dependency>
- <!-- 添加日志相关jar包 -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- Zookeeper zkclient-->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- </dependency>
- </dependencies>
- </project>
(4)创建consumer(消费者)
创建成如下
修改POM.XML为如下:
- <?xml version="1.0"?>
- <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <artifactId>Dubbo_demos</artifactId>
- <groupId>com.fendo</groupId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.fendo</groupId>
- <artifactId>dubbo-consumer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>dubbo-consumer</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>com.fendo</groupId>
- <artifactId>dubbo-api</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- Zookeeper zkclient-->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- </dependency>
- </dependencies>
- </project>
创建完成后,整体结构如下
这种结构看起来不是很美观,把它修改成如下显示,一个项目下包含多个子模块
二、定义接口API、bo对象、公共类及方法,这些是要提供方和调用方都需要的。
在dubbo-api模块中创建一个接口对外开发
三、定义Dubbo服务service,实现dubbo-api中的api接口,书写业务逻辑及DB操作。通过spring暴露服务。
创建DemoService实现类DemoServiceImpl
- public class DemoServiceImpl implements DemoService {
- public List<String> getPermissions(Long id) {
- List<String> demo = new ArrayList<String>();
- demo.add(String.format("Permission_%d", id - 1));
- demo.add(String.format("Permission_%d", id));
- demo.add(String.format("Permission_%d", id + 1));
- return demo;
- }
- }
以及消费者启动类Provider
- public class Provider {
- public static void main(String[] args) throws IOException{
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
- System.out.println(context.getDisplayName() + ": here");
- context.start();
- System.out.println("服务已经启动...");
- System.in.read();
- }
- }
然后创建resources目录
在下新建两个xml文件
provider.xml是dubbo配置文件
- <?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
- <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
- <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
- <dubbo:registry address="zookeeper://localhost:2181"/>
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
- <dubbo:service interface="com.fendo.dubbo.DemoService" ref="demoService" protocol="dubbo" />
- <!--具体实现该接口的 bean-->
- <bean id="demoService" class="org.dubbo.provider.impl.DemoServiceImpl"/>
- </beans>
如果发现eclipse提示错误
解决方案,就是下载一个dubbo.xsd文件然后
windows->preferrence->xml->xmlcatalog
add->catalog entry ->file system 选择刚刚下载的文件路径
修改key值和配置文件的http://code.alibabatech.com/schema/dubbo/dubbo.xsd 相同,点击OK之后就不会报错了。。
dubbo.xsd文件地址:http://download.csdn.net/detail/u011781521/9914946
四、定义调用端也叫消费端,实现对应的控制层。通过spring获取服务。
和上面差不多
创建消费者运行类:
- public class Consumer
- {
- public static void main(String[] args) {
- //测试常规服务
- ClassPathXmlApplicationContext context =
- new ClassPathXmlApplicationContext("consumer.xml");
- context.start();
- System.out.println("consumer start");
- DemoService demoService = (DemoService) context.getBean(DemoService.class);
- System.out.println("consumer");
- System.out.println(demoService.getPermissions(1L));
- }
- }
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="demotest-consumer" owner="programmer" organization="dubbox"/>
- <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
- <dubbo:registry address="zookeeper://localhost:2181"/>
- <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
- <dubbo:reference id="permissionService" interface="org.dubbo.api.DemoService"/>
- </beans>
五、使用zookeeper注册中心。
运行zookeeper/bin目录下的zkServer.cmd
启动zookeeper
六、启动Dubbo的service端,即provider。
启动zookeeper之后,然后运行dubbo-provider中的Provider类
启动成功之后,如上所示,然后在dubbo-admin控制台会多出一个生产者
显示还没有消费者。。
七、启动portal端,即所谓的consumer。
然后启动dubbo-consumer中的Consumer类
会打印如上信息,表示消费成功!!!