Dubbo與Zookeeper、SpringMVC整合和使用


作為dubbo框架初學者,能讓框架跑起來非常不容易,非常感謝網上諸多大神提供的文章,本人參考文章地址是:https://my.oschina.net/xshuai/blog/891281

不過別人的記錄終究不適合自己,所以還是按照自己的風格簡單記錄下學習dubbo整合的步驟。

windows環境介紹:

  myeclipse 10

  jdk1.6

  tomcat 6.0.35  

一、安裝Zookeeper

  1.通過鏈接下載對應的包 http://www.apache.org/dist/zookeeper/

  2.Zookeeper下載后解壓即可,見下圖

  

  3.進入到conf里面,會看到zoo_sample.cfg文件。將zoo_sample.cfg改成bak文件,並復制一個修改為zoo.cfg,修改相關配置內容,注意修改的日志文件夾需要自己手動創建

   

  4.進入D:\zookeeper-3.4.6\bin,雙擊zkServer.cmd,見到如下界面,就表示zookeeper已啟動成功

  

二、安裝dubbo

  1.本人使用的是dubbo-admin-2.5.3.war,下載地址:http://pan.baidu.com/s/1eSnuqEQ

  2.拷貝一個新的tomcat,並將tomcat/webapps里面的ROOT文件夾刪掉

  3.將dubbo-admin-2.5.3.war重命名為ROOT.war,並拷貝到tomcat/webapps目錄下

  4.啟動tomcat

   

    出現上述問題表示你沒有啟動zookeeper

   5.dubbo發布成功后,輸入http://localhost:8889/dubbo-admin-2.5.3/,此時出現登錄頁面,輸入root/root進行登錄,登錄成功后如圖

    

三、創建服務提供服務(provider)

  項目結構如下圖

  

  相應的類中的代碼如下:

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>    
  
  <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>

    <servlet>
        <servlet-name>provider</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:applicationContext.xml,classpath:applicationContext-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>provider</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- 字符過濾器 -->
    <filter>
        <filter-name>Set Character Encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  DemoService.java  

package com.provider;

public interface DemoService {
    
    String sayHello(String name);
}

  DemoServiceImpl.java  

package com.provider;

import org.springframework.stereotype.Service;

@Service(value="demoService")
public class DemoServiceImpl implements DemoService{

    public String sayHello(String name) {
        return "Hello Dubbo,Hello " + name;
    }

}

  applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd          
      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-3.1.xsd          
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
    >

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

    <!-- 引入服務提供者配置文件 -->
    <import resource="dubbo-provider.xml" /> 
</beans>

  dubbo-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:application name="hello-world-provider"  />
 
    <!-- 使用multicast廣播注冊中心暴露服務地址 -->
   <!--  <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
 
     <!-- 使用zookeeper注冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 聲明需要暴露的服務接口 -->
    <dubbo:service interface="com.provider.DemoService" ref="demoService" />
 
    <!-- 和本地bean一樣實現服務 -->
    <!-- 
    <bean id="demoService" class="com.provider.DemoServiceImpl" />
     -->
</beans>

  applicationContext-servlet.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd          
      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-3.1.xsd          
      http://www.springframewor.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    default-autowire="byName">

    <!-- 默認的注解映射的支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
             <bean class ="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name ="supportedMediaTypes">  
                     <list>  
                         <value>text/plain;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="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
    </bean>

    <!-- 加載靜態資源 -->
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/images/**" location="/images/" />
</beans>

四、創建調用服務(customer)

  customer服務架構和provider一致,拷貝一個即可

  相應的代碼如下:

  applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="          
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd          
      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-3.1.xsd          
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
    >

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

    <!-- 引入消費者配置文件 -->
    <import resource="dubbo-consumer.xml" /> 
</beans>

  dubbo-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:application name="hello-world-customer"/>
    <!-- 使用multicast廣播注冊中心暴露發現服務地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>
    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="demoService" interface="com.provider.DemoService" check="false"/>
</beans>

  applicationContext-servlet.xml 和provider中的代碼一致,就不貼出來了,本項目中其實這個文件可以不用

  CustomerAction.java測試類  

package com.customer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.provider.DemoService;

@Controller
@RequestMapping(value="/customerTest")
public class CustomerAction {
    
    @Resource(name="demoService")
    private DemoService demoService;

    @RequestMapping(value="/test.do")
    public ModelAndView test(HttpServletRequest request,HttpServletResponse response){
        System.out.println("成功");
        String result = demoService.sayHello("world");
        System.out.println(result);
        return null;
    }
    
}

 

五、共享服務

  注意:需要將服務提供的接口打成jar包,放入customer中

六、測試

  步驟:1、先啟動zookeeper服務

     2、啟動dubbo服務

     3、啟動provider服務

     4、啟動customer服務

  正常會出現如下界面

  

  錯誤總結:

  若按照上述測試步驟分別在不同的tomcat中啟動服務,應該一切正常

  若2、3、4這三步放在同一個tomcat中啟動,會出現如下錯誤。

  

看下關鍵異常:No provider available for the service

此異常是由於服務沒有可以使用的提供者,就是說在zookeeper注冊中心(zookeeper-url)中沒有可供消費者調用的url,消費者訪問提供者就失敗了。

具體原因分析:由於dobbo在啟動的時候會去檢查各服務之間的依賴關系,由於啟動的時候消費者沒有檢查到提供者提供的服務(此時可能提供者還沒啟動),所以報錯

在消費者配置文件中,需要將<dubbo:registry address="zookeeper://127.0.0.1:2181"/>修改為<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>

由於dubbo在注冊的時候是默認會檢查服務的依賴關系的

 

  

 


免責聲明!

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



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