dubbo學習筆記 第四章構建dubbo的web應用 上

上一章節介紹了dubbo的開源項目自帶的例子,可是這些例子都是直接運行main方法來跑代碼,實際應用中咱們更多地是須要使用web應用來支撐,接下來介紹一下如何搭建dubbo的web應用,咱們將用的spring mvc來搭建。html

1、服務提供者java

在eclipse上搭建一個maven的web工程,具體如何搭建,這裏不作詳細介紹,個人工程名稱叫作dubbo-ws-server,在這個工程中,pom.xml配置的內容以下:web

<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.ws</groupId>
  <artifactId>dubbo-web-client</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>dubbo-web-client Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   <!-- 單元測試用的包,僅測試時使用,不發佈(scope爲test) -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.5.RELEASE</version>
			<scope>test</scope>
		</dependency>
        <dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<!-- 加密相關包 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.10</version>
		</dependency>
		<!-- json轉換須要的包 -->
		<dependency>
			<groupId>com.fasterxml.jackson.jaxrs</groupId>
			<artifactId>jackson-jaxrs-base</artifactId>
			<version>2.6.1</version>
		</dependency>

		<!-- 單元測試用的包,僅測試時使用,不發佈(scope爲test) -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		
		<!-- jsp包,不適用jsp時可刪除此依賴,由容器提供,不發佈 (scope爲provided) -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
			<scope>provided</scope>
		</dependency>
		<!-- servlet包,不適用jsp和servlet時可刪除此依賴,由容器提供,不發佈 (scope爲provided) -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
			<exclusions>
				<exclusion>
					<groupId>org.apache.geronimo.specs</groupId>
					<artifactId>geronimo-servlet_2.5_spec</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.4</version>
		</dependency>

		<!-- Oracle-jdbc驅動 -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc14</artifactId>
			<version>10.2.0.4.0</version>
		</dependency>
		<!-- Hibernate5 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.0.0.Final</version>
		</dependency>
		<!-- spring持久層 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<!-- 阿里巴巴的數據庫鏈接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.18</version>
		</dependency>
		<!-- 事務控制相關 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.6</version>
		</dependency>

		<!-- spring-redis -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.7.1.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.8.1</version>
		</dependency>
		<!-- spring-session -->
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session</artifactId>
			<version>1.2.1.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.2</version>
		</dependency>

		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.9.2</version>
		</dependency>

	   <dependency>
		<groupId>commons-httpclient</groupId>
		<artifactId>commons-httpclient</artifactId>
		<version>3.1</version>
	  </dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
	<dependency>
			<groupId>jdom</groupId>
			<artifactId>jdom</artifactId>
			<version>1.1</version>
		</dependency>

		<!-- dubbo包 -->
		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty</artifactId>
			<version>3.7.0.Final</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
				<exclusion>
					<artifactId>slf4j-log4j12</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>2.5.0</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>16.0.1</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>com.esotericsoftware.kryo</groupId>
			<artifactId>kryo</artifactId>
			<version>2.24.0</version>
		</dependency>
		<dependency>
			<groupId>de.javakaffee</groupId>
			<artifactId>kryo-serializers</artifactId>
			<version>0.26</version>
		</dependency>
			<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.6</version>
			<exclusions>   
                <exclusion>  
                    <artifactId>spring</artifactId>  
                    <groupId>org.springframework</groupId>  
                </exclusion>  
            </exclusions>  
		</dependency>
	 <dependency>
            <groupId>com.ws</groupId>
            <artifactId>dubbo-ws-facade</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>dubbo-web-client</finalName>
  </build>
</project>

其中dubbo的配置應用:redis

</dependency>
			<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.6</version>
			<exclusions>   
                <exclusion>  
                    <artifactId>spring</artifactId>  
                    <groupId>org.springframework</groupId>  
                </exclusion>  
            </exclusions>  
		</dependency>
	 <dependency>

要把spring相關引用去除掉,由於dubbo 2.5.6這個版本好像用的是spring 2.5相關的包,咱們上面有引入spring相關的包,這樣一來可能會衝突。spring

這裏說明一下:如下服務端的代碼是複製了網上的例子,可是例子十分簡單,應該也不知道有什麼抄襲之嫌:)數據庫

這裏暴露的服務代碼是放在另一個工程裏,pom.xml相對應的代碼以下:apache

<dependency>
            <groupId>com.ws</groupId>
            <artifactId>dubbo-ws-facade</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

實現的接口類代碼,以下:json

package com.ws.facade;

import com.ws.entity.User;

/**
 * Description: com.ws.facade
 * Author dobby | dennisit@163.com
 * Date 2015/5/30 23:50
 * Copyright Corporation 2015.05
 * Version 1.0
 */
public interface UserFacade {

    public int insert(User user);

    public int delete(int id);

    public User select(int id);

}

這個接口工程代碼等下一樣要引用到。api

1.實現提供者的代碼有兩個一個是User實體類的Java Bean,其代碼以下:spring-mvc

package com.ws.entity;

import java.io.Serializable;

/**
 * Description: com.ws.entity
 * Author dobby | dennisit@163.com
 * Date 2015/5/30 23:50
 * Copyright Corporation 2015.05
 * Version 1.0
 */
public class User implements Serializable{

    private long id;
    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

值得注意的是相似這種的java要實現Serializable來序列化,不然在dubbo調用中可能會出現問題。

2.實現提供者服務的要暴露出來的接口實現代碼:

package com.ws.server;

import com.ws.entity.User;
import com.ws.facade.UserFacade;

/**
 * Description: com.ws.server
 * Author dobby | dennisit@163.com
 * Date 2015/5/30 23:50
 * Copyright Corporation 2015.05
 * Version 1.0
 */
public class UserFacadeImpl implements UserFacade{

    public int insert(User user) {

        return 1;
    }

    public int delete(int id) {
        return 1;
    }

    public User select(int id) {
        User user = new User();
        user.setName("zhangssan");
        return user;
    }
}

既然用到sping web,還要把提供者相應的dubbo-provider.xml配置在spring-context.xml中,具體配置文件以下:

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


	<!-- <context:annotation-config /> -->


<context:property-placeholder location="classpath:jdbc.properties,classpath:service.properties" />

	<!-- proxy-target-class默認"false",更改成"ture"使用CGLib動態代理 -->
	

	<import resource="dubbo-provider.xml" />
	
<!-- 	<import resource="spring-db.xml" />
	<import resource="spring-datasource.xml" /> -->



</beans>

dubbo-provider.xml的內容以下:

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


	<!-- 提供方應用信息,用於計算依賴關係 -->
	<dubbo:application name="dubbo-provider" />
	<!-- 使用zookeeper註冊中心暴露服務地址 -->
	<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" file="${catalina.home}/dubbo-registry/dubbo-gw-service-user.properties"/>

	<!-- 用dubbo協議在20881端口暴露服務 -->
	<dubbo:protocol name="dubbo" port="20881" />

    <bean id="userFacade" class="com.ws.server.UserFacadeImpl" />
	<!-- 用戶服務接口 -->
	<dubbo:service interface="com.ws.facade.UserFacade" ref="userFacade" />

</beans>

其中file屬性咱們是自定義了dubbo服務緩存目錄,等程序運行起來,能夠本身觀察到相應路徑下會生成緩存文件。

整體相應的文檔目錄截圖以下:

最後爲了web啓動是加載spring 相應的配置,咱們要在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="gw-service-merchant" version="2.5">

    <display-name>dubbo-ws-server</display-name>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context.xml</param-value>
    </context-param>
</web-app>

這樣基本就完成一個簡易服務提供者的搭建。

相關文章
相關標籤/搜索