上一章节介绍了dubbo的开源项目自带的例子,但是这些例子都是直接运行main方法来跑代码,实际应用中我们更多地是需要使用web应用来支撑,接下来介绍一下如何搭建dubbo的web应用,我们将用的spring mvc来搭建。
一、服务提供者
在eclipse上搭建一个maven的web工程,具体如何搭建,这里不做详细介绍,我的工程名称叫做dubbo-ws-server,在这个工程中,pom.xml配置的内容如下:
<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的配置应用:
</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相关的包,这样一来可能会冲突。
这里说明一下:以下服务端的代码是复制了网上的例子,但是例子十分简单,应该也不知道有什么抄袭之嫌:)
这里暴露的服务代码是放在另外一个工程里,pom.xml相对应的代码如下:
<dependency>
<groupId>com.ws</groupId>
<artifactId>dubbo-ws-facade</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
实现的接口类代码,如下:
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);
}
这个接口工程代码等下同样要引用到。
1.实现提供者的代码有两个一个是User实体类的Java Bean,其代码如下:
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>
这样基本就完成一个简易服务提供者的搭建。