dubbo发布webservice服务

来源:互联网 发布:淘宝运营总监年薪 编辑:程序博客网 时间:2024/05/22 03:04

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

下面将介绍demo步骤:

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

2、新建接口DubboService

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.enson.webservice.service;  
  2.   
  3. public interface DubboService {  
  4.       
  5.     public String printWord(String word);  
  6.   
  7. }  
3、新建接口实现类DubboServiceImpl

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.enson.webservice.service.impl;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. import com.enson.webservice.service.DubboService;  
  7.   
  8. public class DubboServiceImpl implements DubboService {  
  9.   
  10.     @Override  
  11.     public String printWord(String word) {  
  12.         String outWord = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS")  
  13.                 .format(new Date()) + word;  
  14.   
  15.         System.out.println(outWord);  
  16.   
  17.         return outWord;  
  18.     }  
  19.   
  20. }  
4、新建配置文件spring\dubbo.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://code.alibabatech.com/schema/dubbo  
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd  
  8.         ">  
  9.   
  10.     <!-- 提供方应用信息,用于计算依赖关系 -->  
  11.     <dubbo:application name="dubbo-webservice-app" />  
  12.   
  13.     <!-- 使用zookeeper注册中心暴露服务地址 -->  
  14.     <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
  15.   
  16.     <!-- 用webservice协议在8080端口暴露服务 -->  
  17.     <dubbo:protocol name="webservice" port="8080" server="servlet" />  
  18.   
  19.     <!-- 声明需要暴露的服务接口 -->  
  20.     <dubbo:service interface="com.enson.webservice.service.DubboService"  
  21.         ref="dubboService" />  
  22.   
  23.     <!-- 和本地bean一样实现服务 -->  
  24.     <bean id="dubboService" class="com.enson.webservice.service.impl.DubboServiceImpl" />  
  25.   
  26. </beans>  

4、配置web.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.   
  7.         <display-name>dubbo-webservice</display-name>  
  8.   
  9.         <welcome-file-list>  
  10.         <welcome-file>index.jsp</welcome-file>  
  11.     </welcome-file-list>  
  12.       
  13.         <!-- 定义资源访问路径 -->  
  14.     <context-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>classpath:spring/*.xml</param-value>  
  17.     </context-param>  
  18.       
  19.         <!--spring的配置-->  
  20.     <listener>  
  21.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  22.     </listener>  
  23.   
  24.         <!--dubbo 服务根路径-->  
  25.     <servlet>  
  26.         <servlet-name>dubbo</servlet-name>  
  27.         <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>  
  28.         <load-on-startup>1</load-on-startup>  
  29.     </servlet>  
  30.     <servlet-mapping>  
  31.         <servlet-name>dubbo</servlet-name>  
  32.         <url-pattern>/services/*</url-pattern>  
  33.     </servlet-mapping>  
  34. </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的错误。

0 0