发表于: 2017-07-22 22:32:25

1 1324



今天:学习了Tuscany,尝试使用了Tuscany RMI对任务项目进行分离


1. Tuscany是Apache软件基金会下的一个项目,进入官网后,会发现上面赫然写着一行大字——“Tuscany已经退休了,已经被移至了Apache阁楼中”。


2. 什么是SCA;Service Oritented Architecture意为面向服务的构架,其中还有一些不太懂的概念,比如同步调用和异步调用,静态调用和动态调用等。总结,SCA就是将服务模块化,独立于通信协议和实现方法。Tuscany可以说是基于SCA的框架,所以理解SCA对于理解Tuscany来说还是比较重要的。但是SCA,包括http://www.oasis-opencsa.org/sca的这些框架都很多年没有更新过了。


3. Tuscany中的几个概念的大小关系:Domain > Node > Composite > Component

比如我们想把任务项目的服务拆分出去,每一个Service类都作为一个Component,这些Component组成一个总的Service Component,Service Component就构成一个Composite,Composite放在一个Node中,Node是一台服务器上实例化的一个提供服务的实例,Node由Domain实例化,Domain可以看做是Spring中的ApplicationContext。


4. Tuscany的基本构架(摘抄)

  • Implementation:SCA 组件(Component)的实现方式,一个 SCA 组件可以由各种语言或技术平台实现,如:POJO,EJB,Spring Bean,bpel 流程,各种脚本语言等等。
  • Binding:是 SCA 的绑定(Binding)规范的实现,SCA 服务(Service)和引用(Reference)的绑定方式,即一个 SCA 服务可以暴露为 Web Service,Java RMI 服务,http 资源,jms 消息等等,一个 SCA 引用也可以通过 Web Service,RMI 调用,http 调用,jms 调用等方式调用远端服务。
  • Databinding:数据绑定方式,这是 Tuscany 提出的概念,一般用与在 Binding 中定义参数的传输格式,比如 Web Service 的 Binding 一般用 XML 格式,SCA 的 Binding 一般用 SDO 格式,Jsonrpc 的 Binding 一般用 Json 格式等等。
  • Interface:是 SCA 的接口(Interface)规范的实现,SCA 服务(Service)和引用(Reference)的接口暴露方式,一般有 Java,WSDL 等类型。

5. 因为能找到的材料实在有限,就下载了Tuscany官方的样例项目,研究了各个项目的项目结构。

HelloWorld结构如下:

包含了服务端的一个接口和实现类:接口有@Remotable 标签


@Remotable
public interface Helloworld {

String sayHello(String name);

}

一个Composite配置文件;一个Composite配置文件对应一个Composite,类似Spring配置文件,其中包含很多Component,类似Bean

<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
          xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
          targetNamespace="http://sample"
          name="helloworld-contribution">

   <component name="HelloworldComponent">
       <implementation.java class="sample.HelloworldImpl"/>
   </component>

</composite>

一个contribution配置文件:Contribution 是将 SCA 组件在 JAVA EE 容器或者 Web 容器部署的部署信息。

<contribution xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
             xmlns:sample="http://sample">
 
  <deployable composite="sample:helloworld-contribution" />

</contribution>

总结结构:


6. Tuscany的注解:Tuscany和Spring一样,有很多自带的注解,例如@Init @Remotable @Property @Reference ,其中@Reference的用法与@Autowire 十分类似,其他的注解由于缺少文档,不知道其具体的用处,只能猜想@Init 是实例化后执行的方法,@Remotable标注在接口上,可能是使这个接口变成远程服务?


7. Tuscany与Spring的整合:

在Spring配置文件中,先引入SCA命名空间(不是Tuscany命名空间),然后写这样的一个sca服务标签,对应一个bean。

<sca:service name="HelloworldService" target="HelloworldBean"/>

<bean id="HelloworldBean" class="sample.HelloworldImpl">
</bean>


在Composite配置文件中,需要把实现方法改为Spring,但个人不能理解的是,Component是如何对应配置文件中的具体哪一个bean的?

<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
          xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
          targetNamespace="http://sample"
          name="helloworld-contribution">

   <component name="HelloworldComponent">
      <implementation.spring location="helloworld-context.xml"/>
   </component>

</composite>

总之,整合后感觉很乱。


8. 使用Tuscany RMI对任务进行分解(不整合Spring):

首先对照官方文档写了Composite配置文件,写一个Service标签,对应一个Service组件,标签中有接口的全类名,连接方式是rmi,引用Component的名字。

<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
          xmlns:rmi="http://tuscany.apache.org/xmlns/binding/rmi/"
          xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
          targetNamespace="http://store"
          name="ServiceComponent">

   <service name="UserService" promote="UserServiceComponent">
       <interface.java interface="tech.zengtian.springrmi.service.UserService"/>
       <binding.rmi host="localhost" port="1099" serviceName="UserService"/>
       <reference target="UserServiceComponent">UserServiceComponent</reference>
   </service>
   <component name="UserServiceComponent">
       <implementation.java class="tech.zengtian.springrmi.service.UserServiceImpl"/>
   </component>

</composite>


写了一个main方法测试:

使用NodeFactory工厂的静态方法得到Node,读取Composite配置文件,新建Contribution配置。

开启node后,按理来说,node中的服务也开始等待被调用,通过RMI的Naming中的方法可以调用Service。

public class main {
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
       String contribution = ContributionLocationHelper.getContributionLocation(main.class);
       Node node = NodeFactory.newInstance().createNode("server.composite", new Contribution("test", contribution));
       node.start();
       UserService userService = (UserService) Naming.lookup("rmi://127.0.0.1:1099/UserService");
       User user = userService.getUserByID((long)1);
       System.out.println(user.getName());
   }
}


但是,报的错误是:

Invalid content was found starting with element 'binding.rmi'.

原因是命名空间(第二行那个)无效,所以无法解析rmi那一行代码,所以之后的也无法继续。


在浏览器中查看那个命名空间的链接也是404。


总结:Tuscany是一个基于SCA的功能强大的框架,但毕竟已经被官方正式退休,花太多时间学习收益不大。


问题:命名空间问题。


明天:开始整理任务1到9的问题,查漏补缺;准备复盘的评审;



返回列表 返回列表
评论

    分享到