发表于: 2017-04-03 23:44:26
4 1479
一.今天完成的
1.学习SOA,SCA,Tuscanyrmi
2,写了部分代码,理论上可以实现使用Tuscany代替springRMI实现web端调用服务端,但有报错没能运行
二.明天的计划:当然是解决报错啦
三.今天的收获
什么是soa
面向服务的体系结构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来。接口是采用中立的方式进行定义的,它应该独立于实现服务的硬件平台、操作系统和编程语言。这使得构建在各种这样的系统中的服务可以以一种统一和通用的方式进行交互。
什么是sca
sca(服务组件框架)是SOA的一种实现形式,是一套面向服务的SOA编程架构,…..
刚开始看的时候晕头转向的
其实都是纸老虎,编程领域总是喜欢把简单的东西说得冠冕堂皇,不就是组件套组件,套好了再给别人用,总感觉是惯用的伎俩
什么是SCA组件
最简单的组件可以是一个类,
当然;一个借口+一个类也可以组成一个组件,这是我们现在要掌握的
说回任务,如何在任务中使用SCA
任务8中我们有包含增删改查方法的接口和实现类,这俩货加一起也算是一个组件,再细分下去,增删改查方法每个拿出来拆成一个接口+一个实现类也是一个组件,大概是吃饱了撑的,按照教程我们应该把任务8中增删改查接口和实现类变成这样子:
查询学生接口
public interface Iselect {
public Student select(long id);
}
查询学生接口对应的实现类
public class Select implements Iselect{
@Autowired
private StudentMapper studentMapper;
public Student select(long id) {
return studentMapper.select(id);
}
}
更新学生接口
public interface Iupdate {
public Integer update(Student student);
}
更新学生接口对应的实现类
public class Update implements Iupdate {
@Autowired
private StudentMapper studentMapper;
public Integer update(Student student) {
return studentMapper.update(student);
}
}
IstudentService接口里面包含了select和update两个方法,和上面定义的select和update方法一毛一样
public interface IstudentService {
public Student select(long id);
public Integer update(Student student);
}
IstudentService接口对应的实现类引用了Iselect和Iupdate组件,并调用了对应的方法
public class IstudentServiceImpl implements IstudentService{
private Iselect select;
private Iupdate update;
public Iselect getSelect() {
return select;
}
@Reference
public void setSelect(Iselect select) {
this.select = select;
}
public Iupdate getUpdate() {
return update;
}
@Reference
public void setUpdate(Iupdate update) {
this.update = update;
}
@Override
public Student select(long id) {return this.select.select(id);}
@Override
public Integer update(Student student) {
return this.update.update(student);
}
}
上面的this.select.select(id)和this.update.update(student)
看的我颇为蛋疼,一定要这么秀吗?一定要写的这么生涩难懂吗?
可能是我真的还不懂什么叫做编程的装逼修养吧…
装完逼,是时候实现远程方法调用了,毕竟这才是正事
还记得任务8中用springRMI实现远程方法调用要在XML中配置一下然后发布一下服务吗
在这里也差不多,建立一个后缀名为.composite的xml文件,代码如下
<?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"
targetNamespace="http://IstudentServiceComponent"
xmlns:c="http://IstudentServiceComponent"
name="IstudentServiceComponent" >
<component name="IstudentServiceComponent">
<implementation.java class="com.jnshu.service.impl.IstudentServiceImpl" />
<service name="IstudentServiceImpl">
<interface.java interface="com.jnshu.dao.IstudentService"/>
<tuscany:binding.rmi port="8099" host="localhost" serviceName="IstudentRMIService" />
</service>
<reference name="select" target="SelectComponent"/>
<reference name="update" target="UpdateComponent"/>
</component>
<component name="SelectComponent">
<implementation.java class="com.jnshu.Select" />
</component>
<component name="UpdateComponent">
<implementation.java class="com.jnshu.Update" />
</component>
</composite>
里面的各个字段搞的我有点烦,没有参考资料的情况下只能靠推理(吐槽一下:Tuscany rmi相关资料简直比岛国动作片还难找)
接下来和task8的springRMI一样搞完xml需要写个类发布一下服务,
public class TuscanyServer {
public static void main(String[] args) throws IOException {
System.out.println("将SCA组件作为RMI接口供外界访问.......");
SCADomain domain = SCADomain.newInstance("StudentService.composite");
System.out.println("回车键推车.......");
System.in.read();
domain.close();
System.out.println("退出.......");
System.exit(0);
}
}
大功告成!
果然报错!
编程五分钟,报错两小时,这是java的铁律,
不用在意这些细节,俩小时一过还没搞定的话,我们继续写客户端
@Controller
@RequestMapping("/student")
public class StudentClient {
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String getStudentById(HttpServletRequest request, HttpServletResponse response
, ModelMap modelMap, @PathVariable Long id) throws RemoteException, NotBoundException, MalformedURLException {
IstudentService istudentService = (IstudentService) Naming.lookup("//localhost:8099/IstudentRMIService");
Student student = null;
istudentService.select(id);
student = istudentService.select(id);
modelMap.addAttribute("student", student);
return "student";
}
}
至此,完成了使用Tuscany 实现web端调用service端的代码部分工作了
那么剩下的99%工作为调试报错;
四.遇到的问题
1.发布服务报错ClassNotFoundException,但我确定路径没问题
所以可能问题出在.composite文件了
照着教程改的,能有什么问题呢?
难道是jar包的问题?但我已经贯彻了宁滥勿缺原则,有的没的jar包都导入进来了
<!--tuscany相关jar包 -->
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-binding-rmi-runtime</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-webapp</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-binding-jsonrpc-runtime</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-embedded</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-sca-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-node-impl</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-node</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-java-runtime
</artifactId>
<version>2.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId> org.apache.tuscany.sca</groupId >
<artifactId> tuscany-base-runtime </artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId> org.apache.tuscany.sca</groupId >
<artifactId> tuscany-implementation-spring-runtime </artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-binding-jms-runtime</artifactId>
<version>2.0</version>
<scope>runtime</scope>
</dependency>
最大的问题是,缺少学习资料!只能对着仅有的一个教程作推理,很多东西基本靠猜!
评论