說明:大概是3年前在一個Saas平臺中接觸到dubbo,此博文是複習筆記,分享給需要的讀者。學習dubbo要有一定的java和maven基礎,因爲此演示全程都離不開maven。此演示側重dubbo原理圖中的Provider—>Registy<—>Consumer 實戰搭建,並且編寫演示代碼及dubbo平臺管理, 筆者將其稱爲” Dubbo第二炮”。(很久沒玩dubbo了,博文用於複習與交流)
此演示內容如下(這裏的演示相當於手把手教妹子):
1.dubbo環境搭建簡介
前言:Dubbo框架如何搭建?
有過SSH,SSMJ,PAFA,JALOR等框架經驗的道友,估計都有搭建框架的經驗,不論你是學習時還是工作時搭建的。它們都有一個共性,基本上離不開jar包導入和配置文件的編寫,當然咯,現在傻B(SpringBoot簡稱SB)和maven進行了大量的簡化,但是仍然離不開jar包和配置文件的管理。
搭建框架本質就是如何正確消費別人的提供的技術。這些技術構建成jar,庫等供大家使用。
此演示環境說明:
Eclipse : Version: Kepler Service Release 2
Tomcat 5.23
Jdk: java version “1.8.0_31”
Zookeeper服務器 zookeeper3.4.12
Maven服務器apache-maven-3.0.5 (看清楚,這裏指服務器不是指jar包版本)
2.dubbo工作原理圖
3.在eclipse中新建4個maven工程
這些工程分別是
user(代表父工程,管理和聚合所有的工程)
user-api(編寫接口和pojo或domain)
user-provider(服務提供者,對應dubbo原理圖中的Provider)
user-consumer(服務消費者,調用user-provider中提供/暴露的服務或接口)
(1) user工程中主要資源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">
<!-- 作爲maven父工程,管理公用的依賴資源和聚合相關聯的項目 -->
<modelVersion>4.0.0</modelVersion>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- maven中央工廠,如果本地倉庫settings配置了鏡像mirror則會覆蓋url,從中央資源工廠下載資源時通過Nexus私服管理 -->
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- maven聚合 -->
<modules>
<module>../user-api</module>
<module>../user-provider</module>
</modules>
<!-- 公共依賴管理 -->
<dependencyManagement>
<dependencies>
<!-- 配置Spring 相應的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-bean</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<!-- 配置註冊中心(zookeeper相關聯的依賴)所需要的jar -->
<dependency>
<groupId>zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<!-- 配置阿里兒子jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.10</version>
</dependency>
<!-- 測試依賴jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件管理 -->
<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>
</project>
(2) user-api工程
===>>> User.java 代碼
package org.yl.dubbo.user_api.pojo;
import java.io.Serializable;
public class User implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -1279924345353308732L;
private Long userId;
private String username;
private String password;
private String sex;
private String telephone;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{userId:").append(this.userId).append(" ,")
.append("username:").append(this.username).append(" ,")
.append("password:").append(this.password).append(" ,")
.append("sex:").append(this.sex).append(" ,")
.append("telephone:").append(this.telephone)
.append("}");
return sb.toString();
}
}
===>>>> UserService.java代碼
package org.yl.dubbo.user_api.service;
import org.yl.dubbo.user_api.pojo.User;
public interface UserService {
User findUserById(Long userId);
int addUser(User user);
}
===>>>> user-appi/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>
<!-- 繼承 -->
<parent>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 相對路徑 -->
<relativePath>../user/pom.xml</relativePath>
</parent>
<artifactId>user-api</artifactId>
<name>user-api</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</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>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3) user-provider工程
====>>>> UserProvider2Application.java代碼
package org.yl.dubbo.user.impl;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 初始化Spring容器,用於啓動user-provider項目來提供服務
* @author 拈花爲何不一笑
*
*此演示"dubbo環境搭建" 執行順序: zookeeper –->
* user-provider(運行UserProvider2Application.java)-->
* user-consumer(運行ConsumerUserService.java)
*/
public class UserProvider2Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-provider.xml");
context.start();
System.out.println("user-provider: " + context.getDisplayName());
System.out.println("===>>>> 啓動user-provider應用...");
try {
System.in.read();
} catch (IOException e) {
System.out.println("===>>>> UserProvider2Application異常...");
e.printStackTrace();
}
}
}
===>>>> UserServiceImpl.java代碼
package org.yl.dubbo.user.impl;
import org.yl.dubbo.user_api.pojo.User;
import org.yl.dubbo.user_api.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User findUserById(Long userId) {
System.out.println("===>>> 模擬查詢用戶");
User user = new User();
user.setUserId(userId);
user.setUsername("lulu");
user.setSex("female");
user.setTelephone("13656821236");
return user;
}
@Override
public int addUser(User user) {
System.out.println("===>>> 添加一個用戶");
return 0;
}
}
===>>>> application-provider.xml代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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="user-provider" owner="admin" organization="dubbox" />
<!-- 使用zookeeper註冊中心,註冊服務提供給其它項目或模塊使用即服務發佈 -->
<dubbo:registry address="zookeeper://localhost:2181" client="zkclient" />
<!-- 用dubbo協議暴露20880端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 服務提供者的接口暴露 -->
<dubbo:service interface="org.yl.dubbo.user_api.service.UserService" ref="userService" />
<!-- 服務提供者的接口實現的bean -->
<bean id="userService" class="org.yl.dubbo.user.impl.UserServiceImpl"/>
<!--
<dubbo:reference id="" interface="" check="false"/>
<bean id="userService" class="com.alibaba.dubbo.governance.service.impl.UserServiceImpl">
<property name="rootPassword" value="${dubbo.admin.root.password}"/>
<property name="guestPassword" value="${dubbo.admin.guest.password}"/>
</bean>
-->
</beans>
===>>>> user-provider/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>
<!-- 繼承 -->
<parent>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 相對路徑 -->
<relativePath>../user/pom.xml</relativePath>
</parent>
<artifactId>user-provider</artifactId>
<name>user-provider</name>
<!-- 依賴的jar -->
<dependencies>
<!-- 引入依賴項目user-api打成的jar包 -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- spring相關聯的jar(發現依賴父類工程user其中一個spring jar其它的也依賴進來了) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 依賴dobbo jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper相關的jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- 測試相關聯的jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 依賴的插件 -->
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</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>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(4) user-consumer工程
===>>>> ConsumerUserService.java代碼
package org.yl.dubbo.user_consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yl.dubbo.user_api.pojo.User;
import org.yl.dubbo.user_api.service.UserService;
/**
* 消費者,調用提供者提供的服務
* 即user-consumer工程調用user-provider工程(這裏採用dubbo提供的RPC)
* @author 拈花爲何不一笑
*
* 此演示"dubbo環境搭建" 執行順序: zookeeper –->
* user-provider(運行UserProvider2Application.java)-->
* user-consumer(運行ConsumerUserService.java)
*/
public class ConsumerUserService {
//通過Spring來啓動user-consumer工程
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-consumer.xml");
context.start();
System.out.println("user-consumer: " + context.getDisplayName());
System.out.println("===>>>> 啓動user-consumer應用...");
//獲取遠程服務代理(這玩意跟EJB中的JNDI很相似)
UserService userService = (UserService) context.getBean("userService");
// 調用遠程服務即user-provider提供的服務
User userResult = (User)userService.findUserById(1012L);
System.out.println("===>>>> userResult: " + userResult);
}
}
===>>>> application-consumer.xml代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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="user-consumer" owner="admin" organization="dubbox" />
<!-- 使用zookeeper註冊中心,用於訪問註冊中心註冊的的服務即服務訂閱 -->
<dubbo:registry address="zookeeper://localhost:2181" client="zkclient" />
<!-- 調用註冊的服務或接口 -->
<dubbo:reference id="userService" interface="org.yl.dubbo.user_api.service.UserService" check="false"/>
<!--
<dubbo:reference id="" interface="" check="false"/>
<bean id="userService" class="com.alibaba.dubbo.governance.service.impl.UserServiceImpl">
<property name="rootPassword" value="${dubbo.admin.root.password}"/>
<property name="guestPassword" value="${dubbo.admin.guest.password}"/>
</bean>
-->
</beans>
===>>>> user-consumer/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>
<!-- 繼承 -->
<parent>
<groupId>org.yl.dubbo</groupId>
<artifactId>user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 相對路徑 -->
<relativePath>../user/pom.xml</relativePath>
</parent>
<artifactId>user-consumer</artifactId>
<name>user-consumer</name>
<!-- 依賴的jar -->
<dependencies>
<!-- 引入依賴項目user-api打成的jar包 -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>user-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- spring相關聯的jar(發現依賴父類工程user其中一個spring jar其它的也依賴進來了) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- 依賴dobbo jar -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper相關的jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<!-- 測試依賴 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 依賴的插件 -->
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
</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>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.maven工程編譯,打包,安裝(complie, package, install)
關於maven怎麼使用,這裏不詳細,不會的自己去學習!貼一張圖,給上面四個工程編譯,打包和安裝….
- 啓動服務器和應用項目
順序:zookeeper–> tomcat --> user-provider工程中的UserProvider2Application.java程序—>user-consumer工程中的ConsumerUserService.java程序
這其中,有遇到的問題及解決方案,請仔細閱讀下面內容
(1)啓動zookeeper服務器
雙擊zkServer.cmd (windwos系統中), 不知道怎麼玩zookeeper看筆者另一篇博文“dubbo第一炮”
成功啓動後如下圖:
(2)啓動tomcat服務器
說明tomcat服務器中部署了dubbo-admin.war應用項目,可參考筆者另一篇博文”dubbo第一炮”
在tomcat安裝目錄\bin 中找到startup.bat 雙擊即可啓動(無需改監聽端口),成功啓動後如下圖:
(3)啓動應用項目user-provider
運行UserProvider2Application.java程序即可啓動user-provider工程
成功如下圖
遇到問題及解決方案(由於筆者落掉了配置導致),如下圖,具體解決方案在圖中已提供
(4) 啓動應用項目user-consumer
運行ConsumerUserService.java程序即可啓動user-consumer工程
成功如下圖
遇到問題及解決方案(由於筆者落掉了配置導致),如下圖,具體解決方案在圖中已提供
-
服務器和應用項目信息分析或展示
(1)zookeeper服務器與user-provider和user-consumer
a) Zookeeper與user-provider工程連接信息
b) Zookeeper與user-consumer工程連接信息
(2)tomcat服務器中dubbo-admin應用項目
希望與大家交流,探討。謝謝! @author;拈花爲何不一笑。敬請觀看即將出爐的 博文”dubbo第三炮”