网站地图
  
  高级搜索
  首页   技术论坛   博客 派计划   产品中心   资源中心   银弹在线   商城  





Apache Synapse ESB 初探 (3)--具体的例子使用axis2和xfire的服务    
#1楼
给作者发送短消息 给作者发送短消息 实名会员 
查看用户其他信息
总分 237 分
财富 460 goCom币
威望 142
排名 第 74 名
段位 新手必读
使用 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/classes

compile:
[javac] Compiling 10 source files to /opt/synapse-1.1.1/samples/axis2Client/target/classes

stockquote:
[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 MyEclipse

public interface IHello {
public String sayHello(String name);
}


实现文件:HelloImpl.java
package org.conan;
//Generated by MyEclipse

public 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</servlet-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端口8088
WS的访问地址为:http://localhost:8088/HelloESB1/services/Hello?wsdl


2. 修改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?wsdl


4. 创建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 request
Hello abc

注:上面的提示信息为Tomcat6.0的NIO的原因。

完全与我的程序解耦合,并且操作还是相当简单的。下面我打算试试,复杂点的集成。
要好好研究一下synapse的schema的规范,看看synapse潜力有多大!






   
 




发表回复
账号用户名   密码   登录
内容:url email imgsrc image code quote
范例 Example
bold italic underline linethrough   


 [更多...]