1. 程式人生 > >SSH專案實戰OA-釋出dubbo服務

SSH專案實戰OA-釋出dubbo服務

釋出dubbo服務

在上一篇文章中,我們已經搭建好dao層,service層和controller層了,所以下面讓我們來發布dubbo服務.

在此之前我們需要先把dubbo相關的包給引進來,在OA-system-service工程中新增依賴

<!-- dubbo相關 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <!-- 排除依賴 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
        </exclusion>
    </exclusions>
</dependency>

      <!-- zookeeper的客戶端,你要連線zookeeper,需要把以下兩個jar包加進來 -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
</dependency>


如此一來,當前taotao-manager-service工程的pom.xml檔案的全部內容如下:這兒,大家要注意,dubbo-2.5.3.jar包下面依賴了一個spring-2.5.6.SEC03.jar包,要知道我們整個工程現在所使用的spring都是4.2.4版本的,這兒冒出個2.5.6版本,這就有可能會產生衝突,那麼這個2.5.6版本的spring包是從哪兒來的呢?其實是依賴傳遞過來的。所以我們應在dubbo中去掉對spring-2.5.6的依賴。同理,我們還要在dubbo中去掉對netty-3.2.5的依賴

<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.QEcode</groupId>
    <artifactId>OA-system</artifactId>
    <version>0.0.1-SNAPSHOT</version>
   </parent>
    <artifactId>OA-system-service</artifactId>
    <packaging>war</packaging>
	<dependencies>
		<dependency>
	  		<groupId>com.QEcode</groupId>
	  		<artifactId>OA-system-pojo</artifactId>
	  		<version>0.0.1-SNAPSHOT</version>
	  	</dependency>
	  	<dependency>
	  		<groupId>com.QEcode</groupId>
	  		<artifactId>OA-system-dao</artifactId>
	  		<version>0.0.1-SNAPSHOT</version>
	  	</dependency>
	  	<dependency>
	  		<groupId>com.QEcode</groupId>
	  		<artifactId>OA-system-interface</artifactId>
	  		<version>0.0.1-SNAPSHOT</version>
	  	</dependency>
		<!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
		<!-- dubbo相關 -->
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>dubbo</artifactId>
		    <!-- 排除依賴 -->
		    <exclusions>
		        <exclusion>
		            <groupId>org.springframework</groupId>
		            <artifactId>spring</artifactId>
		        </exclusion>
		        <exclusion>
		            <groupId>org.jboss.netty</groupId>
		            <artifactId>netty</artifactId>
		        </exclusion>
		    </exclusions>
		</dependency>
		
		      <!-- zookeeper的客戶端,你要連線zookeeper,需要把以下兩個jar包加進來 -->
		<dependency>
		    <groupId>org.apache.zookeeper</groupId>
		    <artifactId>zookeeper</artifactId>
		</dependency>
		<dependency>
		    <groupId>com.github.sgroschupf</groupId>
		    <artifactId>zkclient</artifactId>
		</dependency>
		
		
  </dependencies>
  
</project>

現在我們開始釋出服務,我們在OA-system-service工程的applicationContext-service.xml檔案中釋出服務。 
首先我們需要在檔案頭部新增對dubbo的引用及約束,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
    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://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

	<!-- 配置spring容器建立時要掃描的包 -->
    <context:component-scan base-package="com.QEcode.OA.service.impl"></context:component-scan>
	
</beans>

 

如果dubbo約束報錯,那麼應該是你沒在eclipse配置約束.

下面我們在applicationContext-service.xml檔案中新增如下配置:

<!-- 使用dubbo釋出服務 -->

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

<dubbo:application name="OA-system" />

<dubbo:registry protocol="zookeeper" address="${dubbo.address}" />

<!-- 用dubbo協議在20880埠暴露服務 -->

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

<!-- 宣告需要暴露的服務介面 -->

<dubbo:service interface="com.QEcode.OA.service.UserService" ref="userServiceImpl" />

其中<dubbo:application name="OA-system" />是用來配置在註冊中心的名字,標識我們當前應用的一個名稱,你可以隨便起,但是最好不要跟其他的應用重複,最好跟你的工程名相對應。

從上面可以看到,${dubbo.address}需要我們在配置檔案中配置,所以我們要在resource目錄下新建一個resource.properties檔案,用以存放一些配置.

resource.properties內容如下:

#dubbo地址

dubbo.address=192.168.43.170:2181

至此,我們就釋出了一個服務。

 

===============================================================================================

在寫部落格的時候,可能在專案中有一些問題沒有被發現,在我修改後,忘記寫到部落格上,所以我將這個專案上傳到github上,大家可以在github上獲取專案的程式碼

下面是github地址,大家Fork and Star

OA-Reconsitution