因為老板給的畢業題目是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保存下來,生成客戶端文件進行測試,如下圖: