Dubbo、Zookeeper集群搭建及Rose使用心得(一)


  接觸這個兩三月了,是時候總結一下使用的方法以及心得體會了。我是一個菜鳥,下面寫的如有錯誤,還請各位前輩指出。廢話不多說,正式開始。

一、簡介

  Dubbo是Alibaba開源的分布式服務框架,它最大的特點是按照分層的方式來架構,使用這種方式可以使各個層之間解耦合(或者最大限度地松耦合)。 從服務模型的角度來看,Dubbo采用的是一種非常簡單的模型,要么是提供方提供服務,要么是消費方消費服務,所以基於這一點可以抽象出服務提供方 (Provider)和服務消費方(Consumer)兩個角色。其他具體的架構我也不是很熟悉,有興趣可以在網上自行查找。

二、環境

   Win8.1,Eclipse,zookeeper。

三、配置

  1.新建簡單的maven工程,如圖所示(其中Packaging 選擇pom形式,然后點擊finish)

   

  2.打開剛剛新建工程的pom.xml文件,在組件 下 點擊新建,如圖所示(組件名稱按照功能來分,一般新建三個組件)

   

  3.然后點擊next,如圖所示(這里需要注意的事Packaging,這個根據需要選擇,如果是web,就選擇以war格式打包,如果是一些依賴就選擇以jar打包)

   

  4.重復步驟2和3,新建三個項目組件,各自成為三個新項目,一個項目的大的框架就達成了,但是還沒有把dubbo加到項目。如圖所示(ps:這里項目名稱改成了myweb,我之前的項目,圖方便而已),這里,我新建的三個組件分別是

    a)mywebs-center(用於處理數據操作,打包方式是jar)

    b)mywebs-web(處理web端數據顯示等,最后也是發布這個項目,所以打包方式是war)

    c)mywebs-common(放置一些公用的接口和bean,a和b都依賴於這個。所以,打包方式是jar)

    

  5.配置center層

    

    a)打開pom.xml文件,添加一些架包,代碼如下,

    (PS:除了一些外源的包,還需要引入自己的common層的包,build那塊是一些編譯設置)

<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.lhh</groupId>
        <artifactId>myweb</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>myweb-center</artifactId>
    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.18</version>
        </dependency>
        <dependency>
            <groupId>com.lhh</groupId>
            <artifactId>myweb-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
     <dependency>
            <groupId>com.54chen</groupId>
            <artifactId>paoding-rose-jade</artifactId>
            <version>1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.54chen</groupId>
            <artifactId>bmwutils</artifactId>
            <version>0.0.2</version>
        </dependency>
</dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <executions> <execution> <id>pack-center</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptors> <descriptor>/pack-center.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>

 

 

    b)我把編譯中部分配置寫到了pack-center.xml中,所以,下面是pack-center的代碼

 

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>deploy</id>
    <formats>
        <format>tar</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
            <excludes>
                <exclude>applicationContext.xml</exclude>
                <exclude>**/*.class</exclude>
            </excludes>
            <outputDirectory>/conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/shell</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

 

 

     c)新建applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.lhh.myweb.center.**" />

    <import resource="classpath:dubbo-provider.xml" />

</beans>

 

 

 

     d)新建dubbo-provider.xml文件(服務提供),代碼如下

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 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:service interface="com.lhh.myweb.common.test.interf.Test" ref="test"/>
</beans>

 

 

 

      e)新建dubbo.properties配置文件,代碼如下

 

dubbo.container=log4j,spring

dubbo.application.name=myweb-center
dubbo.application.owner=jw
dubbo.application.logger=log4j

dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.monitor.protocol=registry

dubbo.protocol.name=dubbo
dubbo.protocol.threads=32
dubbo.protocol.port=20893
dubbo.provider.serialization=java
dubbo.service.loadbalance=random

dubbo.log4j.file=logs/myweb-center.log
dubbo.log4j.level=DEBUG
dubbo.registry.file=logs/myweb-center-registry.properties

 

 

     PS:其他的配置文件可以自行配置

    f)新建dubbo啟動器,即DubboStarter.java  代碼如下:

 

package com.lhh.myweb.center;

import net.paoding.rose.scanning.context.RoseAppContext;

public class DubboStarter {

    private static void startDubbo(String[] args) {
        RoseAppContext context = new RoseAppContext();
        context.start();
        com.alibaba.dubbo.container.Main.main(args);
    }

    public static void main(String[] args) {
        startDubbo(args);
    }

}

 

 

 

    每次啟動center就是運行這個 java文件。這里引用的是RoseAppContext,是因為,我項目中用到的rose框架。至此,center配置完畢,但是現在還沒有啟動,每次啟動前,都要先啟動zookeeper.

    這個文件可以在網上搜索下載。

  6.common層,這個沒什么特別要配置的,就是pom文件引入一些架包。這個根據需要而定。

  7.web層配置。這個就比較多了。

    a)在webapp下新建WEB-INF文件夾,然后新建,web.xml配置文件,具體內容具體對待。

    b)pom.xml文件配置,和center層差不多。可以把build項刪掉

    c)其他的和center大同小異,不需要配置數據庫。在dubbo-client.xml中,需要特別注意一下引入服務的方式,代碼如下: 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util" xmlns="http://www.springframework.org/schema/beans"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/util 
          http://www.springframework.org/schema/util/spring-util-2.5.xsd
          http://code.alibabatech.com/schema/dubbo
          http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:reference id="test" interface="com.lhh.myweb.common.test.interf.Test" />
    
</beans>

 

   之后就和一般的web項目一樣了。

   至此,整個項目配置完成。  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM