9.5.5 远程服务调用-Webservice协议
远程服务框架可以采用webservice协议来实现rpc调用,目前支持apache cxf webservice框架。apache cxf webservice的详细资料可以参考其官方网站【http://cxf.apache.org/index.html】 Aop框架中已经很好地集成了apache cxf webservice框架,用户可以通过bboss aop框架来发布cxf的webservice服务。 ? 用来执行远程服务调用rpc webservice服务rpc.webservice.RPCCall就是通过bboss aop框架发布的。Webservice rpc调用示意图如下: ? ? 下面具体来介绍webservice 协议的配置和使用方法。 1.1.1.1???????????? Webservice协议配置对应的配置文件为: /bbossaop/resources/org/frameworkset/spi/manager-rpc-webservices.xml 文件在/bbossaop/resources/org/frameworkset/spi/manager-rpc-service.xml中导入。 <!-- ?????????? 导入webservice服务配置 ?????? ?--> ?????? <managerimport file="org/frameworkset/spi/manager-rpc-webservices.xml" /> Webservice协议配置包含ws rpc协议配置、发布的webservice服务配置、cxf webservice客服端连接参数配置四部分。分别介绍如下。 1.1.1.1.1?????? 发布webservice服务servlet配置rpc 服务器端还需要在web.xml文件中配置发布webservice服务的servlet: <servlet> ?????? <display-name>cxf</display-name> ?????? <servlet-name>cxf</servlet-name> ?????? <servlet-class>org.apache.cxf.transport.servlet.RPCCXFServlet</servlet-class> ?????? <load-on-startup>1</load-on-startup> ??? </servlet> ??? <servlet-mapping> ?????? <servlet-name>cxf</servlet-name> ?????? <url-pattern>/cxfservices/*</url-pattern> ??? </servlet-mapping> 1.1.1.1.2????? Cxf webservice服务控制开关 ???? <property name="rpc.webservice.enable" value="true"/>?? 标记是否启用webservice服务,为false时,启动cxf的serverlet将不会加载和发布配置的webservice 服务(这些webservice服务配置在cxf.webservices.config属性中)。如果采用webservice协议作为远程服务调用的协议时,??? rpc.webservice.enable开关必须配置为true。 1.1.1.1.3?????? webservice rpc服务请求和响应处理组件??? <property name="rpc.webservice.RPCServerIoHandler" singlable="true" class="org.frameworkset.spi.remote.webservice.RPCWebserviceIOHandler">??? ?????? <construction> ?????????? <property name="name" ????????????? ????? value="RPCWebserviceIOHandler" class="String"/>??????????? ?????????? <property name="request_handler" ????????????? ????? refid="attr:rpc.request_handler" />?? ?????????? <property name="webservice_server_type" ????????????? ????? value="cxf" />????? ????? ????????? ?????????? <property name="localaddress" ????????????? ????? value="http://localhost:8080/WebRoot/cxfservices" />?? ????? ?????? </construction>????????? ??? </property>?????? 这里的需要说明的是localaddress属性,这个属性对应于rpc webservice服务的访问地址,用来识别远程服务调用是本地调用还是远程调用,例如程序中发起以下远程服务调用: RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::http://localhost:8080/WebRoot/cxfservices)/rpc.test?user"=admin&password=123456"); testInf.getCount(); 如果http://localhost:8080/WebRoot/cxfservices与本身应用中localaddress属性值相同的话,就相当于是本地服务调用,否则就是远程服务调用。 1.1.1.1.4?????? webservice服务配置??? ??? <property name="cxf.webservices.config"> ?????? <list> ?????? <!-- ?????????? webservice RPC服务,用来实现业务组件之间的远程服务调用 ?????????? 属性说明: ?????????? name 服务的唯一标识名称 ?????????? singleable 服务组件的单列模式 ?????????? servicePort 发布的服务端口 ?????????? class 服务的实现类 ?????????? mtom 服务是否支持附件传输 ????????????? true:支持,false:不支持????? ?????? ?--> ?????????? <property name="rpc.webservice.RPCCall" ????????????????? ? singlable="true" ????????????????? ? servicePort="RPCCallServicePort"??????????? ? ????????????????? ? class="org.frameworkset.spi.remote.webservice.RPCCall"/>? ?????? </list> ??? </property> 1.1.1.1.5?????? Cxf客服端连接参数配置??? <property name="cxf.client.config" enable="true"> ?????? <map> ?????????? <property label="connectionTimeout " name="connectionTimeout" ????????????? value="30000" class="long"> ????????????? <description> <![CDATA[Specifies the amount of time,in milliseconds,that the client will attempt to establish a connection before it times out. The default is 30000 (30 seconds). 0 specifies that the client will continue to attempt to open a connection indefinitely. ]]></description> ?????????? </property> ?????????? <property label="receiveTimeout" name="receiveTimeout" ????????????? value="60000" class="long"> ????????????? <description> <![CDATA[Specifies the amount of time,that the client will wait for a response before it times out. The default is 60000. 0 specifies that the client will wait indefinitely. ]]></description> ?????????? </property> ?????????? <property label="autoRedirect" name="autoRedirect" ????????????? value="false" class="boolean"> ????????????? <description> <![CDATA[Specifies if the client will automatically follow a server issued redirection. The default is false. ]]></description> ?????????? </property> ?????????? <property label="maxRetransmits" name="maxRetransmits" ????????????? value="-1" class="int"> ????????????? <description> <![CDATA[Specifies the maximum number of times a client will retransmit a request to satisfy a redirect. The default is -1 which specifies that unlimited retransmissions are allowed. ]]></description> ?????????? </property> ?????????? <property label="allowChunking" name="allowChunking" ????????????? value="true" class="boolean"> ???????????? <description> <![CDATA[Specifies whether the client will send requests using chunking. The default is true which specifies that the client will use chunking when sending requests. Chunking cannot be used used if either of the following are true: http-conf:basicAuthSupplier is configured to provide credentials preemptively. AutoRedirect is set to true. In both cases the value of AllowChunking is ignored and chunking is disallowed. See note about chunking below. ]]></description> ?????????? </property> 1.1.1.1.6?????? 网络代理服务参数配置<!--????????? <property label="proxyServer" name="proxyServer"--> <!--????????????? value="172.16.17.1" class="String">--> <!--????????????? <description> <![CDATA[Specifies the URL of the proxy server through which requests are routed. ]]></description>--> <!--????????? </property>--> <!--????????? <property label="proxyServerPort" name="proxyServerPort"--> <!--????????????? value="808" class="int">--> <!--????????????? <description> <![CDATA[Specifies the port number of the proxy server through which requests are routed. ]]></description>--> <!--????????? </property>--> <!--????????? <property label="proxyServerType" name="proxyServerType"--> <!--????????????? value="SOCKS" >--> ????????????? <!-- ????????????????? 指定属性注入时的属性编辑和转换器 ????????????? ????????????? ?--> <!--????????????? <editor class="org.frameworkset.spi.remote.webservice.ProxyServerTypeEditor"/>--> <!--????????????? <description> <![CDATA[Specifies the type of proxy server used to route requests. Valid values are: --> <!--????????????? HTTP(default) --> <!--????????????? SOCKS --> <!--????????????? ]]></description>--> <!--????????? </property>--> ?????? </map> ??? </property> 1.1.1.1.7?????? Ssl参数配置-没有经过测试??? <property? name="ws.ssl.config" enable="false"> ?????? <map> ?????????? <property name="keyManagers" keyPassword="password"> ????????????? <list> ????????????????? <property name="keyStore1" type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Morpit.jks"/> ????????????? </list> ?????????? </property> ?????????? ?????????? <property name="trustManagers" keyPassword="password"> ????????????? <list> ????????????????? <property name="keyStore1" type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Truststore.jks"/> ????????????? </list> ?????????? </property> ?????????? ?????????? <property name="cipherSuitesFilter"> ????????????? <list> ????????????????? <property value=".*_EXPORT1024_.*"/> ?????????? ??????? <property value=".*_WITH_DES_.*"/> ?????????? ??????? <property value=".*_WITH_NULL_.*"/> ?????????? ??????? <property value=".*_DH_anon_.*"/> ????????????? </list> ?????????? </property> ?????????? ?????????? <property name="userName" value="Betty"/> ????????????? ?????????? <property name="password" value="password"/> ?????? <!-- ?????????? <http:tlsClientParameters> ??? ????? <sec:keyManagers keyPassword="password"> ??? ?????????? <sec:keyStore type="JKS" password="password" ??? ??????????????? file="src/test/java/org/apache/cxf/systest/http/resources/Morpit.jks"/> ??? ????? </sec:keyManagers> ??? ????? <sec:trustManagers> ??? ????????? <sec:keyStore type="JKS" password="password" ??? ?????????????? file="src/test/java/org/apache/cxf/systest/http/resources/Truststore.jks"/> ??? ?? ???</sec:trustManagers> ??? ????? <sec:cipherSuitesFilter>--> ??? ??????? <!-- these filters ensure that a ciphersuite with ??? ????????? export-suitable or null encryption is used, ??? ????????? but exclude anonymous Diffie-Hellman key change as ??? ????????? this is vulnerable to man-in-the-middle attacks --> ??? ? <!--????? <sec:include>.*_EXPORT_.*</sec:include> ??? ??????? <sec:include>.*_EXPORT1024_.*</sec:include> ??? ??????? <sec:include>.*_WITH_DES_.*</sec:include> ??? ??????? <sec:include>.*_WITH_NULL_.*</sec:include> ??? ??? ????<sec:exclude>.*_DH_anon_.*</sec:exclude> ??? ????? </sec:cipherSuitesFilter> ??? ? </http:tlsClientParameters> ??? ? <http:authorization> ??? ???? <sec:UserName>Betty</sec:UserName> ??? ???? <sec:Password>password</sec:Password> ??? ? </http:authorization> ?????????? ?????? ?--> ?????? ??? <property> ?????????? </property> ?????? </map> ??? </property> 1.1.1.2???????????? Webservice协议使用方法1.1.1.2.1?????? 配置服务服务器Webservice 框架必须部署在web application应用中,因此先在ws rpc服务器端配置webservice框架servlet: <servlet> ?????? <display-name>cxf</display-name> ?????? <servlet-name>cxf</servlet-name> ?????? <servlet-class>org.apache.cxf.transport.servlet.RPCCXFServlet</servlet-class> ?????? <load-on-startup>1</load-on-startup> ??? </servlet> ??? <servlet-mapping> ?????? <servlet-name>cxf</servlet-name> ?????? <url-pattern>/cxfservices/*</url-pattern> ??? </servlet-mapping> Webservice 本地地址配置: <property name="localaddress" ????????????? ????? value="http://localhost:8080/WebRoot/cxfservices" /> 1.1.1.2.2?????? 服务组件配置客服端和服务器端配置组件: <property name="rpc.test" singlable="true" class="org.frameworkset.spi.remote.RPCTest"/> 1.1.1.2.3?????? 启动部署webservice rpc服务的web应用启动部署webservice rpc服务的web应用,假如启动后的webservice访问的地址上下文为(servlet的映射地址,对应于web.xml中的cxf servlet的servlet-mapping配置) http://localhost:8080/WebRoot/cxfservices ? 1.1.1.2.4?????? 获取远程组件实例RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::http://localhost:8080/WebRoot/cxfservices)/rpc.test"); RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::http:// 172.16.17.218:8080/WebRoot/cxfservices;http://172.16.17.216:8080/WebRoot/cxfservices)/rpc.test"); RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::all)/rpc.test"); ? RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice:: http://localhost:8080/WebRoot/cxfservices?user=admin&password=123456"); RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::http:// 172.16.17.218:8080/WebRoot/cxfservices;http://172.16.17.216:8080/WebRoot/cxfservices)/rpc.test?user=admin&password=123456"); RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::all)/rpc.test?user=admin&password=123456"); 1.1.1.2.5?????? 远程方法调用Object count = testInf.getCount(); 1.1.1.2.6?????? 调用结果处理单点调用的结果就是服务接口返回的值,无需处理。如果方法调用失败,系统将抛出具体的远程异常。 多点调用和组播调用的结果处理方法一样,以多点调用为例: RPCTestInf testInf = (RPCTestInf)BaseSPIManager.getBeanObject("(webservice::http:// 172.16.17.218:8080/WebRoot/cxfservices;http://172.16.17.216:8080/WebRoot/cxfservices)/rpc.test?User=admin&password=123456"); Object count = testInf.getCount(); Object count = testInf.getCount(); 获取192.168.11.102:1186返回的结果: Object ret = testInf.getCount(); ??????????? Object ret_8080 = BaseSPIManager.getWSRPCResult("http:// 172.16.17.218:8080/WebRoot/cxfservices ",ret); ??????????? Object ret_8088 = BaseSPIManager.getWSRPCResult("http://172.16.17.216:8080/WebRoot/cxfservices",ret); //等价的调用方法 Object ret_8080 = BaseSPIManager.getRPCResult("http:// 172.16.17.218:8080/WebRoot/cxfservices ",ret,Target.BROADCAST_TYPE_WEBSERVICE); Object ret_8088 = BaseSPIManager.getRPCResult("http://172.16.17.216:8080/WebRoot/cxfservices",Target.BROADCAST_TYPE_WEBSERVICE); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |