天天看點

Maven+SpringMVC+Dubbo+zookeeper 簡單的入門demo配置

參考:http://blog.csdn.net/aixiaoyang168/article/details/51362675

dubbo是一個分布式服務架構,緻力于提供高性能和透明化的rpc遠端服務調用方案,是阿裡巴巴soa服務化治理方案的核心架構,每天為2,000+個服務提供3,000,000,000+次通路量支援,并被廣泛應用于阿裡巴巴集團的各成員站點(其他的詳細介紹可以檢視dubbo的官網位址http://dubbo.io/,寫的很詳細!)。

一、軟體環境 

1、zookeeper 

2、springmvc (maven方式引入) 

3、dubbo (maven方式引入)

二、項目搭建 

這裡我搭建了三個項目,分别為dubbo-api,dubbo-consumer,dubbo-provider

dubbo-api:主要是接口定義,供dubbo-consumer調用,以及dubbo-provider的service層去實作該接口,分離這一層的好處就是前端項目調用接口時候,直接調用dubbo-api的接口即可,不需要關注後端如何實作;而service層來具體實作該接口,進行業務邏輯處理,不需要關注dubbo-consumer 的controller層如何調用。 

dubbo-consumer:主要controller層控制跳轉等。 

dubbo-provider:主要是service實作,結合資料層實作後端業務邏輯處理。

項目搭建完成之後,的結構圖如下:

Maven+SpringMVC+Dubbo+zookeeper 簡單的入門demo配置

三、項目配置 

1、dubbo-api 

maven配置:

<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/maven-v4_0_0.xsd">

    <modelversion>4.0.0</modelversion>

    <groupid>com.dubbo</groupid>

    <artifactid>dubbo-api</artifactid>

    <packaging>jar</packaging>

    <version>1.0-snapshot</version>

    <name>dubbo-api</name>

    <url>http://maven.apache.org</url>

    <build>

       <plugins>

           <plugin>

              <artifactid>maven-compiler-plugin</artifactid>

              <version>2.0.2</version>

              <configuration>

                  <source>1.5</source>

                  <target>1.5</target>

                  <fork>true</fork>

                  <meminitial>128m</meminitial>

                  <maxmem>512m</maxmem>

                  <encoding>utf-8</encoding>

              </configuration>

           </plugin>

              <artifactid>maven-eclipse-plugin</artifactid>

              <version>2.5.1</version>

                  <additionalprojectnatures>

                     <projectnature>

                         org.springframework.ide.eclipse.core.springnature

                     </projectnature>

                  </additionalprojectnatures>

                  <additionalbuildcommands>

                     <buildcommand>

                         org.springframework.ide.eclipse.core.springbuilder

                     </buildcommand>

                  </additionalbuildcommands>

                  <downloadsources>false</downloadsources>

                  <downloadjavadocs>false</downloadjavadocs>

                  <wtpversion>1.5</wtpversion>

           <!-- 要加上下面的一句,否則執行:mvn package -dmaven.test.skip=true的時候會報錯 -->

              <artifactid>maven-war-plugin</artifactid>

              <version>2.1.1</version>

       </plugins>

    </build>

    <properties>

       <project.build.sourceencoding>utf-8</project.build.sourceencoding>

       <spring.version>4.0.3.release</spring.version>

       <org.codehaus.jackson>1.9.13</org.codehaus.jackson>

    </properties>

    <dependencies>

       <!-- test -->

       <dependency>

           <groupid>junit</groupid>

           <artifactid>junit</artifactid>

           <version>4.7</version>

           <scope>test</scope>

       </dependency>

    </dependencies>

</project>

建立testservice接口類

package com.dubbo.service;

/**

 * dubbo學習位址:http://blog.csdn.net/aixiaoyang168/article/details/51362675

 */

public interface testservice {

    /**

     * 測試發消息

     * @param name

     * @return

     */

    public string sayhello(string name);

}

注意:這個項目最終打包成jar檔案,直接maven引入到另外兩個項目中

2、dubbo-consumer 

maven依賴:

    <artifactid>dubbo-consumer</artifactid>

    <packaging>war</packaging>

    <name>dubbo-consumer</name>

        <finalname>dubbo-consumer</finalname>

                     <projectnature>

              </configuration>

       <!-- spring -->

           <groupid>org.springframework</groupid>

           <artifactid>spring-core</artifactid>

           <version>${spring.version}</version>

           <artifactid>spring-beans</artifactid>

           <artifactid>spring-orm</artifactid>

           <artifactid>spring-tx</artifactid>

           <artifactid>spring-test</artifactid>

           <artifactid>spring-aspects</artifactid>

           <artifactid>spring-webmvc</artifactid>

           <artifactid>spring-jms</artifactid>

           <artifactid>spring-context</artifactid>

           <artifactid>spring-context-support</artifactid>

           <artifactid>spring-oxm</artifactid>

           <groupid>org.slf4j</groupid>

           <artifactid>slf4j-api</artifactid>

           <version>1.7.2</version>

           <groupid>log4j</groupid>

           <artifactid>log4j</artifactid>

           <version>1.2.17</version>

           <artifactid>slf4j-log4j12</artifactid>

            <groupid>com.thoughtworks.xstream</groupid>

            <artifactid>xstream</artifactid>

           <version>1.2.2</version>

        </dependency>

           <groupid>ant</groupid>

           <artifactid>ant</artifactid>

           <version>1.7.0</version>

           <groupid>aopalliance</groupid>

           <artifactid>aopalliance</artifactid>

           <version>1.0</version>

           <groupid>commons-dbcp</groupid>

           <artifactid>commons-dbcp</artifactid>

           <version>1.3</version>

           <groupid>commons-fileupload</groupid>

           <artifactid>commons-fileupload</artifactid>

       <!--

           <groupid>commons-logging</groupid>

           <artifactid>commons-logging</artifactid>

           <version>1.1.3</version>

       -->

           <groupid>commons-pool</groupid>

           <artifactid>commons-pool</artifactid>

           <version>1.5.4</version>

           <groupid>javax.servlet</groupid>

           <artifactid>jstl</artifactid>

           <version>1.1.2</version>

           <groupid>com.oracle</groupid>

           <artifactid>ojdbc14</artifactid>

           <version>10.2.0.3.0</version>

        -->

           <groupid>taglibs</groupid>

           <artifactid>standard</artifactid>

           <groupid>commons-codec</groupid>

           <artifactid>commons-codec</artifactid>

           <version>1.5</version>

           <groupid>commons-lang</groupid>

           <artifactid>commons-lang</artifactid>

           <version>2.1</version>

           <groupid>net.sf.ezmorph</groupid>

           <artifactid>ezmorph</artifactid>

           <version>1.0.6</version>

           <groupid>net.sf.json-lib</groupid>

           <artifactid>json-lib</artifactid>

           <version>2.3</version>

           <classifier>jdk15</classifier>

           <artifactid>servlet-api</artifactid>

           <version>2.5</version>

           <scope>provided</scope>

           <groupid>org.json</groupid>

           <artifactid>org.json</artifactid>

           <version>chargebee-1.0</version>

       <!-- json -->

           <version>2.4</version>

           <groupid>org.codehaus.jackson</groupid>

           <artifactid>jackson-core-lgpl</artifactid>

           <version>${org.codehaus.jackson}</version>

           <artifactid>jackson-mapper-lgpl</artifactid>

           <artifactid>json</artifactid>

           <version>20140107</version>

           <groupid>jaxen</groupid>

           <artifactid>jaxen</artifactid>

           <version>1.1.6</version>

        <dependency>

           <groupid>org.aspectj</groupid>

           <artifactid>aspectjrt</artifactid>

           <version>1.8.9</version>

           <artifactid>aspectjweaver</artifactid>

       <!-- <dependency>

           <groupid>commons-codec</groupid>

           <artifactid>commons-codec</artifactid>

           <version>1.9</version>

           <groupid>commons-logging</groupid>

           <artifactid>commons-logging</artifactid>

           <version>1.2</version>

           <groupid>mysql</groupid>

           <artifactid>mysql-connector-java</artifactid>

           <version>5.1.38</version>

       <!-- 阿裡巴巴的druid資料源配置 -->

           <groupid>com.alibaba</groupid>

           <artifactid>druid</artifactid>

           <version>1.0.18</version>

       <!-- 文檔處理所需的jar的依賴 -->

           <groupid>commons-io</groupid>

           <artifactid>commons-io</artifactid>

           <groupid>org.apache.httpcomponents</groupid>

           <artifactid>fluent-hc</artifactid>

           <version>4.5.2</version>

           <artifactid>httpclient</artifactid>

           <artifactid>httpclient-cache</artifactid>

           <artifactid>httpcore</artifactid>

           <version>4.4.4</version>

           <groupid>org.apache.httpcomponents</groupid>

           <artifactid>httpclient-win</artifactid>

           <version>4.5.2</version>

           <artifactid>httpmime</artifactid>

        <!-- dubbo -->

        <dependency>

            <groupid>com.alibaba</groupid>

            <artifactid>dubbo</artifactid>

            <version>2.5.3</version>

            <exclusions>

                <exclusion>

                    <artifactid>spring</artifactid>

                    <groupid>org.springframework</groupid>

                </exclusion>

            </exclusions>

        <dependency>

            <groupid>org.apache.zookeeper</groupid>

            <artifactid>zookeeper</artifactid>

            <version>3.4.6</version>

                    <groupid>log4j</groupid>

                    <artifactid>log4j</artifactid>

           <groupid>dom4j</groupid>

           <artifactid>dom4j</artifactid>

           <version>1.6.1</version>

          <groupid>com.github.sgroschupf</groupid>

          <artifactid>zkclient</artifactid>

          <version>0.1</version>

            <groupid>com.dubbo</groupid>

            <artifactid>dubbo-api</artifactid>

            <version>1.0-snapshot</version>

web.xml配置

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5">

  <display-name>spring_modle</display-name>

  <session-config>

    <session-timeout>30</session-timeout>

  </session-config>

  <context-param>

    <param-name>contextconfiglocation</param-name>

    <param-value>classpath:applicationcontext.xml</param-value>

  </context-param>

    <param-name>log4jconfiglocation</param-name>

    <param-value>classpath:log4j.properties</param-value>

  <filter>

    <filter-name>characterencodingfilter</filter-name>

    <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>utf-8</param-value>

    </init-param>

      <param-name>forceencoding</param-name>

      <param-value>true</param-value>

  </filter>

  <filter-mapping>

    <url-pattern>*.action</url-pattern>

  </filter-mapping>

    <url-pattern>*.jsp</url-pattern>

  <listener>

    <listener-class>org.springframework.web.context.request.requestcontextlistener</listener-class>

  </listener>

    <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>

  <servlet>

    <servlet-name>spring</servlet-name>

    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>

      <param-name>contextconfiglocation</param-name>

      <param-value>classpath:context-dispatcher.xml</param-value>

  </servlet>

  <servlet-mapping>

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

applicationcontext.xml的配置檔案:

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

    xmlns:p="http://www.springframework.org/schema/p"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:task="http://www.springframework.org/schema/task"

    xsi:schemalocation=" 

    http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 

    http://www.springframework.org/schema/tx  

    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://www.springframework.org/schema/mvc

    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

    http://www.springframework.org/schema/cache

    http://www.springframework.org/schema/cache/spring-cache-4.0.xsd

    http://www.springframework.org/schema/task

    http://www.springframework.org/schema/task/spring-task-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/aop

    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- 打開注解 -->

    <context:annotation-config/>

    <!-- 打開自動掃描 -->

    <context:component-scan base-package="com.dubbo"/>

    <!-- 定時器驅動 -->

    <task:annotation-driven/>

    <import resource="dubbo-config.xml"/>

</beans>

dubbo-config.xml

<?xml version="1.0" encoding="utf-8" ?>

    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="test_consumer" />

    <!-- 使用zookeeper注冊中心暴露服務位址 -->

    <dubbo:registry address="zookeeper://192.168.106.80:2181"/>

    <!-- 聲明需要暴露的服務接口 -->

    <dubbo:reference interface="com.dubbo.service.testservice" id="testservice" check="false" />

context-dispatcher.xml

<beans default-lazy-init="true"

    xmlns="http://www.springframework.org/schema/beans"

    xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

       http://www.springframework.org/schema/beans  

       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

       http://www.springframework.org/schema/mvc  

       http://www.springframework.org/schema/mvc/spring-mvc-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/aop

      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- 使用注解的包,包括子集 --><!-- http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd -->

    <context:component-scan base-package="com.dubbo" />

    <aop:aspectj-autoproxy/>

    <!-- 通過注解,把url映射到controller上,該标簽預設注冊defaultannotationhandlermapping和annotationmethodhandleradapter-->

    <mvc:annotation-driven>

       <mvc:message-converters>

           <bean class="org.springframework.http.converter.stringhttpmessageconverter">

              <!--  <constructor-arg value="utf-8" />-->

              <property name="supportedmediatypes">

       <list>

           <value>text/html;charset=utf-8</value>

       </list>

       </property>

           </bean>

       </mvc:message-converters>

    </mvc:annotation-driven>

    <!-- 視圖解析器 -->

    <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver">

       <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" />

       <property name="prefix" value="/web-inf/pages/" />

       <property name="suffix" value=".jsp"></property>

    </bean>

    <bean id="defaultjsonview" class="org.springframework.web.servlet.view.json.mappingjacksonjsonview"/>

    <bean id="multipartresolver"

        class="org.springframework.web.multipart.commons.commonsmultipartresolver" />

</beans> 

建立mycontroller類

package com.dubbo.controller;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.stereotype.controller;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.requestparam;

import org.springframework.web.bind.annotation.responsebody;

import com.dubbo.service.testservice;

@controller

public class mycontroller {

         @autowired

         private testservice testservice;

         /**

          * \brief 方法名與方法功能概述 

          * @param name

          * @return

          * @attention 通路方式:http://127.0.0.1:8080/dubbo-controller/test.action?name=toto

          * @author toto

          * @date 2017年5月24日

          * @note  begin modify by 修改人 修改時間   修改内容摘要說明

          */

         @requestmapping(value = "/test")

         @responsebody

         public string testsay(@requestparam(value = "name",defaultvalue = "") string name) {

                   stringbuffer sb = new stringbuffer();

                   sb.append("dubbo:").append(testservice.sayhello(name));

                   return sb.tostring();

         }

dubbo-consumer終打包成dubbo-consumer.war放在tomcat或其他容器中運作。

3、dubbo-provider

    <artifactid>dubbo-provider</artifactid>

    <name>dubbo-provider</name>

        <finalname>dubbo-provider</finalname>

                     </projectnature>

           <scope>test</scope>

           <groupid>javax.servlet</groupid>

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5">

applicationcontext.xml

    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

    http://code.alibabatech.com/schema/dubbo

    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方應用資訊,用于計算依賴關系 -->

    <dubbo:application name="test_provider" />

    <!-- 用dubbo協定在20880端口暴露服務位址 -->

    <dubbo:protocol name="dubbo" port="20880" />

    <dubbo:service interface="com.dubbo.service.testservice" ref="testservice" />

    <!-- 具體的實作bean -->

    <bean id="testservice" class="com.dubbo.service.impl.testserviceimpl" />

       http://www.springframework.org/schema/context 

       http://www.springframework.org/schema/context/spring-context-4.0.xsd

        <list>

建立testserviceimpl類

package com.dubbo.service.impl;

import com.dubbo.service.testservice;

public class testserviceimpl implements testservice {

    public string sayhello(string name) {

       return name + " service2 say hello word service2!";

    }

注意:這裡作為服務提供者名稱為test_provider,注釋很詳細,就不一一介紹了,這個項目最終打包成dubbo-service.war放在tomcat或其他容器中運作

好了,經過這麼配置之後,我們的項目編碼配置部分就大功告成了!

maven編譯下三個項目,将dubbo-provider和dubbo-consumer放到tomcat容器中,啟動tomcat容器! 出錯啦! 為啥容器不起來!那是因為我們還少了主要的一步,那就是配置啟動zookeeper,不然dubbo去zookeeper注冊中心暴露服務位址不成功啦!

四、配置啟動zookeeper 

最後要啟動zookeeper,關于zookeeper的安裝配置,可以參考:http://blog.csdn.net/tototuzuoquan/article/details/54003140

啟動zookeeper !

再次啟動tomcat容器,是不是就成功啦!我們來通路下http://192.168.106.80:8080//dubbo-consumer/test.action?name=toto,看下傳回結果是不是正确啦! 

Maven+SpringMVC+Dubbo+zookeeper 簡單的入門demo配置

其他dubbo相關的資料:

dubbo:官網http://dubbo.io/

開發指南:http://dubbo.io/user+guide-zh.htm

管理者指南:http://dubbo.io/administrator+guide-zh.htm