一、说明
第一篇文章,先将一个最简单的例子运行起来,让大家对dubbo有一个直观的感受。
插入几个dubbo常用官方网址:
1、中文官网
http://dubbo.apache.org/zh-cn/docs/user/preface/architecture.html
2、apache官网
http://dubbo.apache.org/en-us/docs/user/preface/architecture.html
3、官网文档(供学习、参考)
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
4、github地址
https://github.com/apache/dubbo
二、环境准备
1、下载并安装zookeeper
dubbo需要一个注册中心,dubbo的服务提供方和消费方都需要在注册中心注册。dubbo的注册中心可以选择redis、zookeeper等等,本示例使用zookeeper作为dubbo的注册中心。
实验室环境安装单机zookeeper即可。
zookeeper的下载及安装参照我的另一篇博文:https://blog.csdn.net/mazhongjia/article/details/105013970
这里提醒:zookeeper启动后,请勿关闭!
本示例使用zk版本为zookeeper-3.4.13
2、建立项目工程
2.1 项目最终结构
父工程maven座标:
包括子模块如下:
2.2 项目搭建
1、IDEA新建父工程
本项目为maven管理的普通java工程
2、定义父工程座标
3、定义父工程名、路径
4、配置父工程dubbo-first的pom.xml文件
需要说明的是:排除dubbo中spring的依赖,统一管理spring版本,暂不清楚为什么要自己控制spring版本,这样自己组合肯定没有dubbo自己依赖的好啊,希望有读者评论留言帮忙解答~~~ 暂时认为原因是dubbo2.5.3依赖的spring版本为2.5.X,太低了,项目想使用其他spring功能,但又不需要dubbo版本太新。。。
<?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.mzj.dubbo</groupId>
<artifactId>dubbo-first</artifactId>
<version>1.0-SNAPSHOT</version>
<name>dubbo-first</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dubbo.version>2.5.3</dubbo.version>
<spring.version>4.3.6.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<!--排除dubbo中spring的依赖,统一管理spring版本,如果使用dubbo版本自己依赖的spring,dubbo2.6.5依赖的spring版本是4.3.16,暂不清楚为什么要自己控制spring版本,这样自己组合肯定没有dubbo自己依赖的好啊-->
<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>
<!-- spring相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</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-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>dubbo-api</module>
<module>dubbo-consumer</module>
<module>dubbo-provider</module>
</modules>
</project>
父工程pom.xml中定义项目组成的模块:
<module>dubbo-api</module>
<module>dubbo-consumer</module>
<module>dubbo-provider</module>
模块职责:
- dubbo-api:定义服务接口(注意服务提供方和消费方都需要依赖这个项目)
- dubbo-consumer:服务消费者
- dubbo-provider:服务提供者
5、在IDE中(我使用的IDEA)建立好上面三个模块并设置三个模块的:
- pom.xml
- demo-api模块
<?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>
<parent>
<groupId>com.mzj</groupId>
<artifactId>dubbo-first</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>demo-api</artifactId>
</project>
- dubbo-provider父亲为dubbo-first依赖dubbo-api
<?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>
<parent>
<groupId>com.mzj.dubbo</groupId>
<artifactId>dubbo-first</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.mzj.dubbo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- dubbo-comsumer父亲为dubbo-first也依赖dubbo-api
<?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>
<parent>
<groupId>com.mzj.dubbo</groupId>
<artifactId>dubbo-first</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.mzj.dubbo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 手工创建src/main/java、src/test/java、src/main/resources、src/test/resources并在IDE中设置
- 因为我们使用父工程统一设置项目依赖,并不会将源码放入父工程,因此删除父工程src目录
- 最终整理完工程效果如下:
2.3 在dubbo-api中定义服务接口
注意dubbo-api的命名不是很好,推荐定义服务接口不要携带服务实现(这里的定义携带了dubbod实现)
package com.mzj.service.first;
public interface DemoService {
String sayHello(String name);
}
注意这里定义的服务接口内容没有与服务实现耦合在一起,没有被服务实现侵入。
2.4 在dubbo-provider中实现服务提供方
- 项目结构
- 实现服务接口
package com.mzj.service.first;
import org.springframework.stereotype.Service;
@Service("demoService")
public class DemoServiceImpl implements DemoService{
@Override
public String sayHello(String name) {
return name;
}
}
- 在dubbo配置文件声明暴露服务
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" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.mzj.service.first.DemoService" ref="demoService" />
</beans>
- 在spring配置文件中扫描service注解并将dubbo-provider.xml中的相关的dubbo配置引入进来
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--在spring配置文件中扫描service注解并将dubbo-provider.xml中的相关的dubbo配置引入进来-->
<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">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.mzj.service.first" />
<import resource="classpath:dubbo-provider.xml" />
</beans>
- 加载Spring配置,启动服务:
package com.mzj.service.first;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
context.start();
System.out.println("Dubbo provider start...");
try {
System.in.read(); // 此处目的是不让进程立即退出,按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果如下:
Dubbo provider start...
2.5 在dubbo-comsumer中实现服务消费者
- 项目结构:
- 在dubbo配置文件声明要消费的服务
dubbo-comsumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 在dubbo-consumer.xml中声明所所需要消费的服务-->
<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" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.mzj.service.first.DemoService" />
</beans>
- 在spring配置文件中扫描service注解并将dubbo-consumer.xml中的相关的dubbo配置引入进来
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 在springmvc.xml中扫描service注解并将dubbo-consumer.xml中的相关的dubbo配置引入进来-->
<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">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.mzj.service.first"/>
<import resource="classpath:/dubbo-consumer.xml"/>
</beans>
- 加载Spring配置,调用服务:
package com.mzj.service.first;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:applicationContext.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println(demoService.sayHello("哈哈哈"));
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果:
哈哈哈
3、通过dubbo-admin管理后台可以看到服务的提供方与消费方
具体搭建可参考我的另一篇博客:XXXX