发表于: 2017-11-25 22:07:53
1 647
今天完成的事情
尝试用Tuscany实现服务端和客户端的分离
Tuscany在2016/5/28就停止维护了,与spring集成的时候,spring的版本得换到比较早得版本,看了师兄们得日报,将spring的版本换成3.0.5的
Tuscany的相关jar包为2.0版本
和任务八的操作相似,都是在服务端提供接口和实现,也就是服务,在客户端利用接口来调用方法,实现分离,在服务端的各个实现作为组件来组成一个总的服务,来暴露给客户端
需要写一个Tuscany的配置文件:xxxxx.composite
<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="Calculator">
<component name="ServiceComponent">
<implementation.spring location="applicationContext.xml"/>
<service name="StudentWebService">
<tuscany:binding.rmi uri="rmi://127.0.0.1:9999/hehe"/>
</service>
</component>
</composite>
idea会爆红,但是没有影响,其中有一个名为ServiceComponent的组件,通过加载spring的配置文件来获得其实现类,不然就需要写其实现的完整路径,设置一个服务,名为StudentWebService,绑定了其地址为:rmi://127.0.0.1:9999/hehe,客户端可以通过该地址与服务端实现连接
spring的配置文件中,需要添加服务的实现的声明:
<sca:service name="StudentWebService" target="student" />
其中的student为通过注解命名的bean名:
@Service("student")
public class StudentWebServiceImpl implements StudentWebService {
在提供方法的接口上,需要添加注解:
@Remotable
public interface StudentWebService {
@Remotable注解用于将一个Java服务接口标注被远程的,可以作为一个服务对外公布
通过main方法来启动服务端:
public static void main(String[] args) throws Exception{
Node node = NodeFactory.newInstance().createNode("Mydemo.composite");
node.start();
System.out.println("OK");
}
在客户端中,也需要在接口上添加注解@Remotable
在spring配置文件中,开启扫描:
<context:component-scan base-package="com.jnshu.service"/>
注入连接:
<bean id="studentWebService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:9999/hehe"/>
<property name="serviceInterface" value="com.jnshu.service.StudentWebService"/>
</bean>
在controller中用注解进行装配,就可以实现远程调用服务了
@Autowired
StudentWebService studentWebService;
访问效果:
终于成功了
明天的计划
查阅SOA,SCA
完成并提交任务九
遇到的问题
无
收获
熟悉了Tuscany的基本配置
评论