WebService相信大家都很熟悉了,但是通过OSGI中利用Camel发布这个WebService服务给别人调用,我相信很少人知道,下面就由我来分享一下其中的原理以及实现方法。
由于blueprint.xml文件是我们OSGI环境下的桥梁,所以一切入手由它开始,需要符合规范,否则此文件不会生效。
一、创建一个blueprint.xml文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:cxfcore="http://cxf.apache.org/blueprint/core"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">
注意,我们会发现命名空间中有
xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:cxfcore="http://cxf.apache.org/blueprint/core"
这两个东西正是camel的命名空间,就好像我们java程序中import作用是一样的,需要用到它,那么就需要进行import进去,否则会找不到命名空间。
二、blueprint.xml文件有了,那么我们就需要对此进行配置了,我们目的是要发布一个webservice服务出来,首先:
把我们需要给人家知道的调用地址,那么我们就用CM把他设为常量
当然,我们不一定把这个CM设在blueprint文件中,我们可以另起一个 xxxx.Common.test.cfg文件,把地址设在文件中,并把文件放在ServiceMix的etc文件根目录下即可,到时候会先在etc目录下找是否存在此文件,此文件是否存在该常量地址,有则使用该文件下的地址,没有的话会寻找blueprint文件中的常量,并使用它。
三、
地址设好了之后,那么我们需要做的是把我们需要发布的服务接口(或者服务实现)给引进来,
四、
既然我们用到了camel,那么Processor也是必不可少的了,创建一个Processor,然后把我们刚才引进来的服务注入进去,这样子,引进来的服务在Processor下面就可以自由的使用了
五、
把服务注入进Processor之后,然后我们就可以准备发布一个WebService服务了,首先需要把camel-cxf给引进来
address="${SOA.User.ServiceAddress}"
serviceClass="xxxx.Common.Contract.IUserService"
bindingId="http://www.w3.org/2003/05/soap/bindings/HTTP/"
>
六、
到此步,我们的服务倒是发布出来了,但当别人请求的时候,我们的服务需要去到对应的接口实现倒还不知道呢,所以别人虽然请求了,但是我们的cxf还不能够准确的给他分配到他希望请求的服务,所以下面这步是关键
xsi:schemaLocation="http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
${in.header.operationName} == 'AddUser'
七、整个过程就是这样了,一个成功的服务就发布成功了。
完毕!