以下的说明及用法,均以windows系统为例:
synapse
|--bin (命令行程序目录)
|--native (针对操作系统的类库目录)
|--install-synapse-service.bat (注册为windows服务)
|--synapse.bat(启动synapse服务器)
|--uninstall-synapse-service.bat(从windows服务列表中删除)
|--docs (说明文档目录)
|--lib (使用的类库目录)
|--logs (存放日志的目录)
|--repository (资源库的目录)
|--conf (synapse服务器配置文件目录)
|--sample (服务器配置例子的目录)
|--resources(wsdl文件的例子目录)
|--synapse_sample_0.xml (synapse配置的例子,命令行启动的时候,可以通过 “synapse -sample 0”, 进行调用,不再加载synapse.xml文件)
|--synapse_sample_1.xml(synapse配置的例子,命令行启动的时候,可以通过 “synapse -sample 1”, 进行调用,不再加载synapse.xml文件)
|--axis2.xml (Axis2服务器配置文件)
|--synapse.xml (Synapse服务配置文件)
|--synapse.properties (Synapse的启动参数,线程数等)
|--wrapper.conf(启动参数,JVM大小,目录结构,日记信息等)
|--modules (系统类库)
|--samples (例子的目录)
|--axis2Client (Axis2的客户端程序例子,这部分应该是可以生成的,通过Axis2的命令行工具)
|--axis2Server (Axis2的服务端程序的例子,启动Axis2服务器,发布Web Services)
|--work (服务器编译后的文件目录)
蓝色标记部分为目录,红色标记部分为经常使用的几个文件。
1. 修改synapse.xml文件
2.启动synapse.bat服务器
3. 通过synapse服务器的服务发布地址,生成应用程序客户端。
4. 连通性调试
(迭代过程)
5. 针对需求再次修改synapse.xml文件
6. 重启synapse.bat服务器
7. 终于实现ESB的功能
使用synapse的操作过程:
1. 发布一个Web Services工程。
2. 修改synapse.xml的配置文件,和刚才发布的Web Services服务建立关联。
3. 启动synapse服务器
4. 创建synapse发布的服务器的客户端程序。
5. 启动客户端程序,得到结果。Apache Synapse ESB初探(3)--具体的例子使用axis2和xfire的服务
下面说了一下使用SynapseESB的具体的例子。
使用synapse的操作过程:
1. 发布一个Web Services工程。
2. 修改synapse.xml的配置文件,和刚才发布的Web Services服务建立关联。
3. 启动synapse服务器
4. 创建synapse发布的服务器的客户端程序。
5. 启动客户端程序,得到结果。
先从他提供的例子说起吧:
官方参考网址:http://synapse.apache.org/Synapse_QuickStart.html
1. 发布一个Web Services工程。
1). 进入下面的目录,编译Web Services源文件
user@host :/opt/synapse-1.1.1/samples/axis2Server$ cd src/SimpleStockQuoteService/
user@host :/opt/synapse-
1.1.1/samples/axis2Server/src/SimpleStockQuoteService$ ant
2). 启动Axis2,发布服务
Now go to <synapse-home>/samples/axis2Server directory and start the server using the following command. This will start Axis2 server on port 9000 (http).
Linux / Unix: . axis2server.sh
Windows: axis2server.bat
2. 修改synapse.xml的配置文件,和刚才发布的Web Services服务建立关联。
Now it"s time to start Synapse. In this scenario we are starting Synapse using the sample configuration found in synapse_sample_0.xml (i.e. in repository/conf/sample) and listed below. It is configured to log and pass through, all the messages.
<definitions xmlns="http://ws.apache.org/ns/synapse"> <log level="full"/> <send/></definitions> 3. 启动synapse服务器 (启动synapse_sample_0.xml配置)
Go to <synapse-home>/bin directory and type the command given below. Synapse will be started on port 8280 (http) and 8243 (https - under JDK 1.5)
Linux / Unix: . synapse.sh -sample 0
Windows: synapse.bat -sample 0
4. 创建synapse发布的服务器的客户端程序。
例子中客户端程序已经写好了,通过ant编译 及 运行,与下一步一起进行。
5. 启动客户端程序,得到结果。
Now the final step, running the client. Go to <synapse-home>/samples/axis2Client directory and type the following command
user@host :/opt/synapse-1.1.1/samples/axis2Client$ ant stockquote -Daddurl=http://localhost:9000/soap/SimpleStockQuoteService -Dtrpurl=http://localhost:8280 -Dmode=quote -Dsymbol=IBM得到结果为:init: [mkdir] created dir: /opt/synapse-
1.1.1/samples/axis2Client/target/classescompile: [javac] Compiling 10 source files to /opt/synapse-
1.1.1/samples/axis2Client/target/classesstockquote: [java] Standard :: Stock price = $91.09641757880443小结一下:以上的过程,就完成了synapse的第一个最简单的应用。因为synapse.xml的文件里,没有配置任何的服务绑定,因此,在启动客户端程序的时候,需要,-Dtrpurl -Daddurl -Dmode -Dsymbol等启动参数,建立client --> esb --> real ws的连接。
下面一个例子,将通过synapse的代理功能,使配置都在synapse.xml的文件里进行。在这里,我不使用官方提供的例子了。使用xfire自己写服务端和客户端程序,通过synapse的代理服务进行集成。
1. 发布一个Web Services工程。
1) 通过MyEclipse写一个Hello的ws。
接口文件:IHello.java
实现文件:HelloImpl.java
XFire配置文件: services.xml
Web配置文件:web.xml接口文件:IHello.java package org.conan;//Generated by MyEclipsepublic interface IHello { public String sayHello(String name);}实现文件:HelloImpl.java package org.conan;//Generated by MyEclipsepublic class HelloImpl implements IHello {
public String sayHello(String name) {
return "Hello "+name; }
}XFire配置文件: services.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0"> <service>
<name>Hello</name> <serviceClass>org.conan.IHello</serviceClass> <implementationClass>org.conan.HelloImpl</implementationClass> <style>document</style>
<use>literal</use>
<scope>request</scope>
</service></beans>Web配置文件:web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet- class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</s ervlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping></web-app>2) 发布web工程到Tomcat服务器:Tomcat端口8088WS的访问地址为: http://localhost:8088/HelloESB1/services/Hello?wsdl2. 修改 synapse.xml的配置文件,和刚才发布的Web Services服务建立关联。 <definitions xmlns="http://ws.apache.org/ns/synapse">
<proxy name="Hello">
<target>
<endpoint>
<address uri="http://localhost:8088/HelloESB1/services/Hello"/>
</endpoint>
<outSequence>
<send/>
</outSequence>
</target>
<publishWSDL uri="http://localhost:8088/HelloESB1/services/Hello?wsdl"/>
</proxy></definitions>3. 启动synapse服务器Windows: synapse.bat我的SynapseESB启动的port是8080, 打开浏览器,访问: http://localhost:8080/soap/Hello?wsdl4. 创建synapse发布的服务器的客户端程序。通过MyEclipse,客户端生成工具:使用 http://localhost:8080/soap/Hello?wsdl生成客户端代码 因为代码都是生成的,只贴出main()调用代码:
public static void main(String[] args) {
HelloClient client = new HelloClient();
//create a default service endpoint
HelloPortType service0 = client.getHelloSOAP11port_https(); HelloPortType service1 = client.getHelloSOAP11port_http1();
String tmp = service1.sayHello("abc");
System.out.println(tmp);
}
5. 启动客户端程序,得到结果。 2008-6-28 12:09:20 org.apache.commons.httpclient.HttpMethodBase writeRequest信息: 100 (continue) read timeout. Resume sending the requestHello abc注:上面的提示信息为Tomcat6.0的NIO的原因。完全与我的程序解耦合,并且操作还是相当简单的。下面我打算试试,复杂点的集成。要好好研究一下synapse的schema的规范,看看synapse潜力有多大!