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

原文地址: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 {

	@Override
	public 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的错误。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr-稻帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值