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.0modelVersion>
    <parent>
        <groupId>com.lhhgroupId>
        <artifactId>mywebartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <artifactId>myweb-centerartifactId>
    <dependencies>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>3.1.3.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.jdomgroupId>
            <artifactId>jdomartifactId>
            <version>1.1.3version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.3version>
        dependency>
        <dependency>
            <groupId>commons-dbcpgroupId>
            <artifactId>commons-dbcpartifactId>
            <version>1.4version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.18version>
        dependency>
        <dependency>
            <groupId>com.lhhgroupId>
            <artifactId>myweb-commonartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    
            com.54chen
            paoding-rose-jade
            1.1
            
                
                    org.springframework
                    spring
                

            

        

        
            com.54chen
            bmwutils
            0.0.2
        
dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-pluginartifactId> <version>2.2-beta-5version> <executions> <execution> <id>pack-centerid> <phase>packagephase> <goals> <goal>singlegoal> goals> <configuration> <descriptors> <descriptor>/pack-center.xmldescriptor> 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>deployid>
    <formats>
        <format>tarformat>
    formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}directory>
            <excludes>
                <exclude>applicationContext.xmlexclude>
                <exclude>**/*.classexclude>
            excludes>
            <outputDirectory>/confoutputDirectory>
        fileSet>
        <fileSet>
            <directory>${project.basedir}/src/shelldirectory>
            <outputDirectory>/outputDirectory>
        fileSet>
    fileSets>
    
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>trueuseProjectArtifact>
            <outputDirectory>liboutputDirectory>
            <scope>runtimescope>
        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项目一样了。

   至此,整个项目配置完成。