发表于: 2017-07-07 23:01:39
3 1069
今日完成:
将原有的SpringRMI更改为Tuscany的RMI。
明日计划:
总结下任务1-9的学习情况
准备复盘项目PPT
准备小课堂资料
收获:
今天一直在解决bug,tuscany的配置文件好难搞
参考师兄的代码,把任务9跑通了
贴下代码:
1.pom配置
<properties>
<!-- spring框架 -->
<spring-version>3.0.5.RELEASE</spring-version>
<!--mybatis核心包-->
<mybatis-version>3.4.3</mybatis-version>
<!--日志文件管理包-->
<log4j-version>1.2.17</log4j-version>
<!-- 格式化对象,方便输出日志 -->
<slf4j-version>1.7.7</slf4j-version>
<!--jetty插件-->
<jetty-version>8.1.11.v20130520</jetty-version>
</properties>
<dependencies>
<!--tuscany相关jar包-->
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-base-runtime</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-spring-runtime</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-binding-rmi-runtime</artifactId>
<version>2.0.1</version>
<scope>runtime</scope>
</dependency>
2.在Core层建model、service接口。实体类要实现Serializable接口,对该对象序列化
public class Student implements Serializable {
service接口添加@Remotable注释,它用于表示当前接口可以进行远端调用。
package com.ptteng.service;
import com.ptteng.model.Student;
import org.oasisopen.sca.annotation.Remotable;
import java.util.List;
@Remotable
public interface StudentService {
public List<Student> getAll();
3.在Service层建dao接口、service实现类,和SpringMVC一样没区别,主要是Tuscany的配置文件
spring-tuscany.xml
<?xml version= "1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca-j/spring/200810"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<sca:service name="StudentService" target="StudentService" />
<bean id="StudentService" class="com.ptteng.service.impl.StudentServiceImpl" />
<import resource="spring-mvc.xml"/>
</beans>
StudentService.composite 这个文件我看别人都是xml格式,我的改不过来 一直是灰色
<?xml version= "1.0" encoding ="UTF-8"?>
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:tuscany="http://tuscany.apache.org/xmlns/sca/1.1"
targetNamespace="http://student"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
name= "StudentService" >
<component name="StudentServiceComponent">
<implementation.spring location="spring-tuscany.xml" />
<service name= "StudentService">
<tuscany:binding.rmi uri="rmi://localhost:8031/StudentService" />
</service>
</component >
</composite>
其余的Spring-mvc.xml、jdbc.properties和SpringRMI一样
4.服务端启动
public class ServerStart1 {
public static void main(String[] args) throws InterruptedException {
Node node = NodeFactory.newInstance().createNode( "StudentService.composite");
node.start();
while (true ) {
System.out.println("Service1启动发布成功");
TimeUnit. SECONDS.sleep(Long.MAX_VALUE);
}
}
}
5.Web层创建controller和任务8,SpringRMI一样
Spring加Tuscany配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.ptteng" />
<!--输出jsp-->
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean id="viewResolve" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--构造输出url-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1"/>
</bean>
<bean id="studentService2" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" scope="prototype">
<property name="serviceUrl" value="//localhost:8032/StudentService"/>
<property name="serviceInterface" value="com.ptteng.service.StudentService"/>
</bean>
<bean id="studentService1" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" scope="prototype">
<property name="serviceUrl" value="//localhost:8031/StudentService"/>
<property name="serviceInterface" value="com.ptteng.service.StudentService"/>
</bean>
最后启动服务端、再启动客户端
问题:
启动客户端报:java.rmi.ConnectIOException: non-JRMP server at remote endpoint
百度后说:非JRMP在远程服务器端点?
端口的问题,后来想起来这个端口号在做任务8的时候用过,改过来问题解决了
maven在做clean时报错
[ERROR] [ERROR] Project 'com.ptteng.JavaTask:Task09-Service:1.0-SNAPSHOT' is duplicated in the reactor @
[ERROR] Project 'com.ptteng.JavaTask:Task09-Service:1.0-SNAPSHOT' is duplicated in the reactor -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
原因是我创建service2时生成的pom文件有误,本来应该是这样
<artifactId>Task09-Service2</artifactId>
但是我的确是??
<artifactId>Task09-Service</artifactId>
评论