基于SCA(tuscany)的SOA实践之一(发布web服务是如此的简单)
本文讲述基于SCA(tuscany)的SOA实践,第一篇讲发布服务,tuscany支持N多协议,这里主要是针对webservice
先说一下开发环境jdk6+eclipse3.4+wst+stp+tuscany1.3.1
1 建立一个动态web工程tuscanytest,源文件目录是src/main/java,资源文件目录是src/main/resources,web 目录是src/main/webapp,把tuscany的jar复制到src/main/webapp/WEB-INF/lib/目录下
2 接口,sca中接口可以有java和wsdl2种方法,本例使用java
定义接口 IUserservice,代码如下
package com.tuscanytest.service;
import java.util.List;
import org.osoa.sca.annotations.Remotable;
@Remotable
public interface IUserservice {
//如果服务是分布式的,就需要使用@Remotable声明
List<User> getAll();
}
3 定义User和Address类,都是简单的pojo对象,代码如下
User类
package com.tuscanytest.service;
public class User {
private String id;
private String name;
private String password;
private Address address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address类
package com.tuscanytest.service;
public class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
4 定义IUserservice的UserServiceImpl实现类,这里只是简单的从内存中获得模拟数据,代码如下
package com.tuscanytest.service;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements IUserservice {
private static List<User> users = null;
static {
users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setId("id" + i);
user.setName("name" + i);
user.setPassword("password" + i);
Address address = new Address();
address.setStreet("street" + i);
user.setAddress(address);
users.add(user);
}
}
public List<User> getAll() {
return users;
}
}
5 在src/main/resources/下定义default.composite文件,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.0" name="tuscanytest"
targetNamespace="http://serivce.tuscanytest.com">
<component name="UserServiceImpl">
<implementation.java promote="UserServiceImpl">
<interface.java interface="com.tuscanytest.service.IUserservice" />
<binding.ws></binding.ws>
<!--定义了发布服务绑定的协议,使用webservice,还有其他多种协议绑定,可参考tuscany文档-->
</service>
</composite>
文件最终部署到webapp/WEB-INF/classes/目录下即可
6 定义sca-contribution.xml,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<contribution xmlns="http://www.osoa.org/xmlns/sca/1.0"
targetNamespace="http://serivce.tuscanytest.com"
xmlns:tuscanytest="http://serivce.tuscanytest.com">
<deployable composite="tuscanytest:tuscanytest"/>
</contribution>
文件最终部署到webapp/META-INF/目录下即可
7 修改webapp/WEB-INF/web.xml文件,添加如下的filter
<filter>
<filter-name>tuscany</filter-name>
<filter-class>org.apache.tuscany.sca.host.webapp.TuscanyServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>tuscany</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
8 然后启动部署到web服务器上,访问地址http://localhost:9080/tuscanytest/UserService?wsdl,就可以看到wsdl服务描述文件
看你只需要增加一行配置,一个普通的java类就变成了web服务了
比起axis2的复杂来说,发布服务是如此的简单