一、說明
第一篇文章,先將一個最簡單的例子運行起來,讓大家對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