Maven + Zookeeper + Dubbo 搭建簡單分佈式項目

前言:對於第一次接觸分佈式dubbo框架的小夥伴,可先自行了解一下dubbo的相關原理和運行機制。博主推薦一篇不錯的博文,小夥伴們可以閱覽後,再學習本文dubbo分佈式項目搭建。

dubbo基本原理:https://blog.csdn.net/en_joker/article/details/89946034

本文借鑑以下博客:Dubbo入門---搭建一個最簡單的Demo框架

1. Zookeeper 安裝配置

下載地址:https://zookeeper.apache.org/releases.html

官網安裝教程:http://zookeeper.apache.org/doc/r3.5.5/zookeeperStarted.html

百度網盤鏈接:https://pan.baidu.com/s/141kQcKae7ay7-O7WVAORsg   提取碼:jaij 

下載解壓後,將bin下的zoo_sample.cfg文件複製一份並修改文件名爲zoo.cfg,編輯該文件配置信息如下所示:

# The number of milliseconds of each tick  心跳間隔 毫秒每次
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting anacknowledgement
syncLimit=5
# the directory where the snapshot isstored.  //鏡像數據位置
dataDir=D:\\layduo\\dubbo\\data
#日誌位置
dataLogDir=D:\\layduo\\dubbo\\logs
# the port at which the clients willconnect  客戶端連接的端口
clientPort=2181

完成以上配置後,啓動Zookeeper服務,進入安裝目錄bin下,輸入以下命令:

zkServer.cmd

重新cmd開啓另一個窗口,輸入以下命令查看啓動狀態:

jps -l -v

進入安裝目錄bin下,輸入以下命令連接Zookeeper:

zkCli.cmd 127.0.0.1:2181

2. dubbo-admin安裝配置 

由於dubbo被阿里捐獻給了apache,這次安裝admin時,參考網上的資料,地址還是停留在之前的鏈接,踩了不少坑,這裏記錄下。

dubbo-admin下載地址:

地址一:https://github.com/apache/incubator-dubbo/releases 該地址2.6版本以上的包中沒有dubbo-admin ,2.5x版本的有。

地址二:https://github.com/apache/incubator-dubbo-ops 該地址中的dubbo-admin(默認端口7001)模塊被單獨拎出來了,springboot方式啓動,可以直接運行main方法,或者使用 java -jar 方式啓動,很方便,有github賬號的可以fork一下。

現成的dubbo-admin.war百度網盤鏈接:https://pan.baidu.com/s/1ZeD_FJbwA58ji1msfVz41A  提取碼:evgq 

下載後把dubbo-admin通過mvn命令行將dubbo-admin打成war包,進入dubbo-admin目錄下,通過以下命令打包: 

mvn package -Dmaven.skip.test=true

 接着將打包好war包放在tomcat的webapps目錄下,啓動tomcat,war包將會自動解壓。然後修改WEB-INF下的dubbo.properties文件,初始root和guest賬戶密碼:

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

最後測試dubbo-admin訪問是否能成功,瀏覽器訪問http://ip:端口/dubbo-admin,輸入賬號密碼進入主頁面說明配置成功。

3. 項目搭建(開發環境jdk1.7 + maven + spring + dubbo + zookeeper + idea)

項目結構如下所示:其中dubbo是父級項目,裏面的三個都是子項目,dubbo-api是接口管理,dubbo-provider是服務提供方,dubbo-consumer是服務消費方。

 

 

 dubbo項目根路徑下的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>

    <groupId>com.layduo</groupId>
    <artifactId>dubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>

        <!-- spring版本號 -->
        <spring.version>4.2.5.RELEASE</spring.version>

        <!-- mybatis版本號 -->
        <mybatis.version>3.2.8</mybatis.version>

        <!-- mysql驅動版本號 -->
        <mysql-driver.version>5.1.29</mysql-driver.version>

        <!-- log4j日誌包版本號 -->
        <slf4j.version>1.7.18</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <dependencies>
        <!-- 添加junit4依賴 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <!-- 指定範圍,在測試時纔會加載 -->
            <scope>test</scope>
        </dependency>
        <!-- 添加jstl依賴 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <!-- 添加spring核心依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</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-oxm</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-jdbc</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-context</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-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 添加mybatis依賴 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- 添加mybatis/spring整合包依賴 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 添加mysql驅動依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-driver.version}</version>
        </dependency>
        <!-- 添加數據庫連接池依賴 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 添加fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.22</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>
        <!-- log end -->
        <!-- 映入JSON -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</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>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>dubbo</finalName>
    </build>
</project>

 dubbo-api下的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">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>com.layduo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api</artifactId>
    <packaging>war</packaging>

    <name>dubbo-api Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>dubbo-api</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_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-war-plugin</artifactId>
                    <version>3.2.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>
            </plugins>
        </pluginManagement>
    </build>
</project>

dubbo-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">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>com.layduo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.layduo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

dubbo-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">
    <parent>
        <artifactId>dubbo</artifactId>
        <groupId>com.layduo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.layduo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

注意:dubbo-provider和dubbo-consumer中pom.xml記得添加一下依賴:

<dependencies>
    <dependency>
        <groupId>com.layduo</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

api聲明接口,如下所示:

package com.layduo.dubbo.demo;

import java.util.List;

/**
 * Created by wy on 2017/4/13.
 */
public interface DemoService {
    List<String> getPermissions(Long id);
}

 provider提供服務,如下所示:

package com.layduo.dubbo.demo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.layduo.dubbo.demo.DemoService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("demoService")
@Service(version = "1.0")
public class DemoServiceImpl implements DemoService {
    @Override
    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.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-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:provider timeout="600000"/>

    <!--1、通過掃描servive註解注入實現類的bean-->
    <!--掃描dubbo的service註解,在接口實現類上使用註解注入bean -->
    <!-- 配置dubbo註解識別處理器,不指定包名的話會在spring bean中查找對應實例的類配置了dubbo註解的 -->
    <dubbo:annotation package="com.layduo.dubbo.demo.impl"/>

    <!--使用 dubbo 協議實現定義好的 api.PermissionService 接口-->
    <dubbo:service interface="com.layduo.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />

    <!--2、通過手動配置實現類的bean注入,每個接口實現類都得注入,不推薦這樣的寫法-->
    <!--具體實現該接口的 bean-->
    <!--<bean id="demoService" class="com.layduo.dubbo.demo.impl.DemoServiceImpl"/>-->
</beans>

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服務-->
    <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--設置超時時間-->
    <dubbo:consumer timeout="600000"/>
    <!--向 zookeeper 訂閱 provider 的地址,由 zookeeper 定時推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 協議調用定義好的 api.DemoService 接口-->
    <dubbo:reference id="demoService" interface="com.layduo.dubbo.demo.DemoService"/>
</beans>

調用遠程服務前,請確保服務先要啓動。

啓動遠程服務:

package com.layduo.dubbo.demo.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {

    private static final Logger logger = LoggerFactory.getLogger(Provider.class);

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        logger.info(context.getDisplayName() + ": here");
        context.start();
        logger.info("服務已經啓動...");
        System.in.read();
    }
}

啓動消費者調用遠程服務:

package com.layduo.dubbo.consumer;

import com.layduo.dubbo.demo.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {

    private static final Logger logger = LoggerFactory.getLogger(Consumer.class);

    //使用@Reference註解注入dubbo服務

    public static void main(String[] args){
        //測試常規服務
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        logger.info("consumer start");
        DemoService demoService = context.getBean(DemoService.class);
        logger.info("consumer");
        System.out.println(demoService.getPermissions(1L));
    }
}

 最後瀏覽器登錄dubbo-admin查看服務提供者和消費者的情況:

 需要項目源碼的可以用git從https://github.com/builthuLin/dubbo.git 克隆到自己本地運行~

發佈了33 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章