发表于: 2017-09-21 20:32:15

1 770


额,师兄我的图片还是不能直接从页面上传到服务器,明天你抽空帮我看一下把,只能从本地上传。

今天完成的事情:

1.今天看了一下看了一下spring rmi的相关概念,


2.看了一下之前博涛师兄讲的小课堂,然后找了一个spring demo 实现了一下。

就是一个简单的一个接口口,一个实现类,一个服务端程序。

public class RMIServer {
public static void main(String[] args) throws IOException {

new ClassPathXmlApplicationContext("springrmi.xml");
       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

       while (true) {
if (reader.readLine().equals("exit")) {
System.exit(0);
           }
}

}
}

这个是服务端的程序,就是加载了一下配置文件。

<bean id="rmiService"
         class="com.sr.RmiServiceImpl"/>

   <bean id="serviceExporter"
         class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service">
           <ref bean="rmiService"/>
       </property>
       <property name="serviceName">
           <value>rmiService</value>
       </property>
       <property name="serviceInterface">
           <value>com.sr.RmiService</value>
       </property>
       <property name="registryPort">
           <value>5555</value>
       </property>
   </bean>
</beans>

配置文件也比较简单,就是创建了两个bean

    <bean id="rmiServiceProxy"
         class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceUrl">
           <value>rmi://localhost:5555/rmiService</value>
       </property>
       <property name="serviceInterface">
           <value>com.task.service.UserService1</value>
       </property>
   </bean>
</beans>

这是客户端的bean

public class RMIClient {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"spring-*.xml");

       UserService1 service =
(UserService1) context.getBean("rmiServiceProxy");
       int a = service.deleteUser(1l);
       System.out.print(a);
   }
}

代码比较简单,就是帮助理解了一下spring rmi是怎么运作的。知道什么是rmi了 再看这些就懂得很快。

rmi就是远程方法调用,这个吧接口和实现类分离了,其实也就是一个系统调用另一个系统的某个方法

然后把自己的项目也用了spring rmi 实验了一下。客户端调用了一下其中的删除方法,是成功的,因为一开始没头绪,后面看了其他师兄的日报,就跳过了很多坑。首先是类需要序列化,跟之前实现缓存的时候是一样的。因为RMI在传输的时候要把数据序列化好像是这样,其次服务端加载spring 配置文件的时候要都加载上,不然会报错唔。然后晚上遇到了问题,就是我一开始没搞懂这个web 和service到底是怎么做的。所以把spring mvc的配置文件也加载上去了,这是后会提示错误。说我缺少jar包,我添加上去也是不对,后来经过师兄讲解才知道这个web 和service 。web层主要负责controller和调用接口,service层就是单纯的实现数据库的增删查改。所以明天打算把项目拆解一下。把这两个层分开。


public class RMIClient {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"spring-*.xml");

       UserService1 service =
(UserService1) context.getBean("rmiServiceProxy");
       int a = service.deleteUser(1l);
       System.out.print(a);
   }
}

这里调用了一下删除的方法

返回1说明调用是成功的。

数据库也没了。


所以上面说了这么多,分布式到底解决了什么问题?

  • 为了性能扩展——系统负载高,单台机器无法承载,希望通过使用多台机器来提高系统的负载能力
  • 为了增强可靠性——软件不是完美的,网络不是完美的,甚至机器本身也不可能是完美的,随时可能会出错,为了避免故障,需要将业务分散开保留一定的冗余度

明天的计划:把之前的项目拆解一下,分成web层和service层,然后部署一下。


返回列表 返回列表
评论

    分享到