IDEA +Tomcat + maven 搭建 Dubbo REST 项目
Github 项目地址:dubbo-provider-rest.git
(一)三个配置文件
很多使用外部 Tomcat(servlet 容器) 开发的项目都是为了实现 REST 调用。但是 Dubbo 项目的配置比较复杂,下面先给出三个主要的配置文件。分别是maven项目构建的依赖文件pom.xml、web项目常用的web.xml文件、Dubbo/Spring 的 context 配置文件。
pom.xml 添加的依赖如下,有很多的坑点,请确保每一个包都正确导入,有冲突的包都忽略。即使正确导入所有包也有可能因为版本对应不上的问题导致启动失败,请大家尽量使用下面的版本,少走弯路。
主要有Spring、Dubbo、REST支持、Tomcat、zookeeper、日志(重要)、JSON/XML序列化。
<dependencies>
<!-- 【begin Spring】 -->
...省略,详细请看项目pom.xml文件
<!-- 【end Spring】-->
<!-- 【begin dubbo】 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 去除Spring,防止冲突 -->
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
<version>2.6.0</version>
</dependency>
<!-- 【end dubbo】 -->
<!-- 【begin REST开发】-->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<!-- 【end REST开发】-->
<!-- 【begin tomcat】 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.0.11</version>
</dependency>
<!-- 【end tomcat】-->
<!-- 【begin zookeeper】 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<!-- 忽略这个包的导入,因为可能会报错 -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 【end zookeeper】 -->
<!-- 【begin 日志类】:不能缺少,会报错 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- 【end 日志类】-->
<!-- 【begin REST序列化】 -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.7.Final</version>
</dependency>
<!-- 【end REST序列化】-->
</dependencies>
Dubbo/Spring 配置文件 provider-context.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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 本应用的名称,唯一 -->
<dubbo:application name="demo-provider"/>
<!-- 注册到zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 暴露请求的端口,注意外部容器需要指定server为servlet,外部容器可以使用Tomcat。 <dubbo:protocol contextpath> 可以理解为请求路径前缀,如xxx表示localhost:9001/xxx/ -->
<dubbo:protocol name="rest" port="9001" threads="500" server="servlet" accepts="500" contextpath=""/>
<!-- 开启注解扫描,解析包路径下的注解,在这里是@Service,可以直接将对应的类作为服务发布出去。 也可以选择在本文件配置bean,不开启注解扫描,等价于下面的两个配置 -->
<dubbo:annotation package="org.xujiale.dubbodemo.service"/>
<!--<bean id="systemService" class="org.xujiale.dubbodemo.service.SystemServiceImpl"/>-->
<!--<dubbo:service interface="org.xujiale.dubbodemo.api.SystemService" ref="systemService"/>-->
</beans>
web 项目的 web.xml 配置文件:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!-- 配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 官方的demo中这个路径类似/WEB-INF/classes/xxx,其实和这个是等价的,只不过是build之后, 打开target文件夹就能找到对应的目录结构,编辑时IDE可能会报找不到文件,但运行时不会出错 -->
<param-value>classpath:provider-context.xml</param-value>
</context-param>
<!-- dubbo的监听器,需要定义在其他监听器之前 -->
<listener>
<listener-class>com.alibaba.dubbo.remoting.http.servlet.BootstrapListener</listener-class>
</listener>
<!-- spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 接收(拦截)所有请求路径 -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
这里再给出 IDEA 配置的 Tomcat:
(二)服务类代码
这里给出接口和实现,具体可以看 demo 的代码:
package org.xujiale.dubbodemo.api;
import org.xujiale.dubbodemo.bean.Response;
public interface SystemService {
Response getServerIpList();
}
package org.xujiale.dubbodemo.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import org.xujiale.dubbodemo.api.SystemService;
import org.xujiale.dubbodemo.bean.Response;
// 注意引入下面的包,参考pom文件,下面这几个注解和dubbo没有关系,不需要开启包扫描
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("system")
@Service // 只有这个是dubbo的注解,需要开启包扫描
@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML}) // 接受参数的序列化模式
@Produces({ ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8}) // 返回的序列化模式,支持json和xml,utf-8编码
public class SystemServiceImpl implements SystemService {
@GET
@Path("serverIpList")
public Response getServerIpList() {
return new Response("succeed", "本地地址:127.0.0.1");
}
}
(三)运行结果
默认 json 格式返回:
指定 xml 格式:
REST 开发本身并不复杂,Dubbo 支持得很好,相对来说开发环境的搭建反而比较复杂,特别是对初学者来说,希望这篇文章能帮到各位,Github上的项目注释很仔细,大家不妨本地试着运行一下。
如果要更了解 REST 开发或者 Dubbo 的话,建议大家去官方网站学习。网上知识太零散,学习这种大框架特别容易出问题。
- Dubbo 官方开发文档
- 在Dubbo中开发REST风格的远程调用(RESTful Remoting)
还没有评论,来说两句吧...