前面介绍了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?
- package com.enson.webservice.service;
-
- public interface DubboService {
-
- public String printWord(String word);
-
- }
3、新建接口实现类DubboServiceImpl
[java] view plain copy print?
- 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
[html] view plain copy print?
- <?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" />
-
-
- <dubbo:registry address="zookeeper://127.0.0.1:2181" />
-
-
- <dubbo:protocol name="webservice" port="8080" server="servlet" />
-
-
- <dubbo:service interface="com.enson.webservice.service.DubboService"
- ref="dubboService" />
-
-
- <bean id="dubboService" class="com.enson.webservice.service.impl.DubboServiceImpl" />
-
- </beans>
4、配置web.xml
[html] view plain copy print?
- <?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>
-
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
- <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的错误。