dubbo:webservice基于servlet容器的实现demo

来源:互联网 发布:制冷系统自动设计软件 编辑:程序博客网 时间:2024/06/03 18:24

原文地址:http://dtbuluo.com/blog/archives/124

前面介绍了dubboframework基于dubbo协议的demo,这比给大家介绍另一种协议——webservice,其实它是基于http协议的实现,暴露wenservice的标准化接口,使用到apache-cxf的实现。其实dubbo还有其他很多种协议的实现方式,如rmi,hessian,redis,普通http等等。

下面将介绍demo步骤:

1、新建dynamic web project ,名称:dubbo-webservice,如图:

2、新建接口DubboService

package com.enson.webservice.service;public interface DubboService {public String printWord(String word);}
3、新建接口实现类DubboServiceImpl

package com.enson.webservice.service.impl;import java.text.SimpleDateFormat;import java.util.Date;import com.enson.webservice.service.DubboService;public class DubboServiceImpl implements DubboService {@Overridepublic String printWord(String word) {String outWord = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(new Date()) + word;System.out.println(outWord);return outWord;}}
4、新建配置文件spring\dubbo.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="dubbo-webservice-app" />    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 用webservice协议在8080端口暴露服务 -->    <dubbo:protocol name="webservice" port="8080" server="servlet" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="com.enson.webservice.service.DubboService"        ref="dubboService" />    <!-- 和本地bean一样实现服务 -->    <bean id="dubboService" class="com.enson.webservice.service.impl.DubboServiceImpl" /></beans>

4、配置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="WebApp_ID" version="2.5">        <display-name>dubbo-webservice</display-name>        <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>        <!-- 定义资源访问路径 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/*.xml</param-value></context-param>        <!--spring的配置--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>        <!--dubbo 服务根路径--><servlet><servlet-name>dubbo</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>dubbo</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping></web-app>

5、将项目部署到tomcat上

注意:dubbo必须使用的servlet-api为2.5版本,tomcat默认优先加载2.3版本,找到tomcat安装路径中的lib文件夹,将servlet-api替换成servlet-api-2.5.jar

6、先启动zookeeper,再启动tomcat

访问http://localhost:8080/dubbo-webservice/services/com.enson.webservice.service.DubboService?wsdl

注意:地址缺少“?wsdl”将报cxf的错误。

原创粉丝点击