很多人可能会说,dubbo协议的rpc方式十分高效,为什么要用webservice方式呢,在有些场景利用dubbo协议并不是万能的,比如比较复杂的内网隔离,只能暴露特定端口给你用的时候,webservice就会体现它的优势,在已经规划整体架构用了dubbo后,就不可能重新替换为类似cxf,xfire这种的webservice架构。
一、配置webservice提供者
需要配置在原先介绍的项目中针对pom.xml加入如下配置,主要用cxf实现:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
</exclusion>
</exclusions>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.0</version>
</dependency>
dubbo-provider.xml的配置内容为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
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="dubbo-ws-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.43.33:2181" file="${catalina.home}/dubbo-registry/dubbo-gw-service-user.properties"/>
<!-- http://localhost:8080/dubbo-webservice/services/com.ws.facade.UserFacade?wsdl -->
<!-- 用dubbo协议在webservice端口暴露服务 -->
<dubbo:provider protocol="webservice" port="8080" server="servlet"/>
<bean id="userFacade" class="com.ws.server.UserFacadeImpl" />
<!-- 用户服务接口 -->
<dubbo:service interface="com.ws.facade.UserFacade" ref="userFacade" />
</beans>
主要核心就是:<dubbo:provider protocol="webservice" port="8080" server="servlet"/>
其他的跟之前的服务提供者基本相同,不需要怎么改动。
二、启动dubbo服务提供者
启动工程后,我们在浏览器中输入
http://localhost:8080/dubbo-webservice/services/com.ws.facade.UserFacade?wsdl
就可以看到熟悉的webservice报文,如下图: