初学ServiceMix

因为老板给的毕业题目是ESB相关,需要学下ServiceMix(版本7.0.1)

但是SOA这东西技术上比较旧,加上主要是企业在用,个人学习的不多,所以资料比较少

CSDN上看到篇文章不错但是有些地方没有讲出来,自己摸索着试验成功以后把步骤记下来防止忘了2333

先附上参考资料:https://blog.csdn.net/iteye_15360/article/details/82680564

1.安装与启动

Windows下安装ServiceMix很简单,直接解压即可,没有任何其他操作,安装组件或者设置环境变量都不是必需的

启动方法:运行SERVICEMIX_HOME/bin/servicemix.bat

tips:

1)ServiceMix有热部署机制,会把deploy文件夹下的jar包自动部署为bundle,如果自己写的程序有问题想下线,只把deploy的jar包删掉是没用的(jar包已经被打包成bundle放在cache文件夹里了),必须用 bundle:uninstall <bundle_name> 命令卸载掉你的jar包

2)ServiceMix正常启动后,active的bundle数和已安装的bundle数必须一致,否则实际上启动失败,如果一直失败,可以试试解压到其他位置(我就遇到过这种情况,换个位置就好了)

2.编写简单的WebService

以IDEA为例

首先新建Project,选择 Java - Java EE - Web Application - WebServices

Version无所谓,Axis需要的包少一些,先选择这个。然后一路Next。

项目建立后自带一个HelloWorld

运行项目需要Tomcat等容器,以Tomcat为例:

点击右上角 Add Configuration… - 弹出窗口左上角+号 - Tomcat Server - Local ,如果此前在IDEA里配置过Tomcat,IDE会帮你自动填写信息,否则需要选定你的Tomcat安装路径

此时会提示No artifacts configured之类的错误,选择Fixed,可以自动创建(建议这里把Application context设置为根路径,即"/")

然后进入菜单: File - Project Structure -Artifacts,会提示Library is missing 啥的,同样点Fix

在弹出的菜单中选择第一项:Add 'JAX-WS-Apache Axis' to the artifact

然后就可以运行程序了。Tomcat启动后,可以进入 localhost:8080/services查看WebService列表(8080是默认端口,上下文路径之前配置为根路径,否则应该是 localhost:port/<application_context>/services)

然后就可以使用IDEA自带的功能生成WSDL了,方法很简单:(这里不需要生成)

在HelloWorld.java上右键 Webservices - Generate wsdl from Java Code ,在WebService URL一栏填入HelloWorld的地址(点击上图中最下面的wsdl,在新标签页中复制url,并把末尾的"?wsdl"去掉即可)

3.编写Camel应用

新建一个空Maven项目,在resource文件夹下建立META-INF/spring/camel-config.xml文件(没有就自己建立resource目录,记得标记为资源目录)

内容:

<?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:camel="http://camel.apache.org/schema/spring"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">

    <cxf:cxfEndpoint id="reportIncident"
                     address="http://0.0.0.0:8186/IHello"
                     serviceName="s:HelloWorldService"
                     endpointName="s:HelloWorld"
                     wsdlURL="http://localhost:8080/services/HelloWorld?wsdl"
                     xmlns:s="http://example"/>
    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <endpoint id="callRealWebService" uri="http://localhost:8080/services/HelloWorld?bridgeEndpoint=true"/>
        <route>
            <from uri="cxf:bean:reportIncident?dataFormat=MESSAGE"/>
            <setHeader headerName="SOAPAction" >
                <constant>FooSync</constant>
            </setHeader>
            <convertBodyTo type="String" />
            <to ref="callRealWebService"/>
        </route>
    </camelContext>

</beans>

cxfEndpoint的address属性可以根据喜好修改

如果上一步使用HelloWorld示例的话,该配置可以直接套用

endpoint的uri和webservice uri一致,必须添加"?bridgeEndpoint=true",意思是打开桥接端点(否则无法转发)

这里IDEA可能会提示 “Application context not configured for this file”,不用管它

配置完直接使用 mvn install 打包即可

4.部署到ServiceMix

由于这里使用了http协议的服务,需要先安装camel-http插件,否则直接部署的话会报错:

No componentfound with scheme: http

方法:启动ServiceMix,在控制台输入 feature:install camel-http

然后把maven打包好的jar包放进SERVICEMIX_HOME/deploy目录下,控制台如果没报错就说明已经正常部署了(可以用bundle:list看看是否部署)

在浏览器输入 http://localhost:8186/IHello?wsdl 能够成功访问即说明配置成功

也可以把wsdl保存下来,生成客户端文件进行测试,如下图:

猜你喜欢

转载自blog.csdn.net/u010670411/article/details/85097218