发表于: 2018-01-25 20:00:01

1 646


今日完成

1,分布式与集群概念的理解

(1)什么是分布式与集群————一个很形象的例子

小饭店原来只有一个厨师,切菜洗菜备料炒菜全干。后来客人多了,厨房一个厨师忙不过来,又请了个厨师,两个厨师都能炒一样的菜,这两个厨师的关系是集群。为了让厨师专心炒菜,把菜做到极致,又请了个配菜师负责切菜,备菜,备料,厨师和配菜师的关系是分布式,一个配菜师也忙不过来了,又请了个配菜师,两个配菜师关系是集群。

(2)了解什么是RMI(remote mthod invcation),即远程方法调用。它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法。可以用此方法调用的任何对象必须实现该远程接口。

Java RMI不是什么新技术(在Java1.1的时代都有了),但却是是非常重要的底层技术。

大名鼎鼎的EJB都是建立在rmi基础之上的,现在还有一些开源的远程调用组件,其底层技术也是rmi。

在大力鼓吹Web Service、SOA的时代,是不是每个应用都应该选用笨拙的Web Service组件来实现,通过对比测试后,RMI是最简单的,在一些小的应用中是最合适的。

下面通过一个简单的例子来说明RMI的原理和应用,下面这个例子是一个简单HelloWorld,但已涵盖RMI的核心应用与开发模式。

2.将以前的项目中的service分离出来。

(1)配置dervice的RMI 服务端。

<!--配置对应服务的实现类-->
  <bean id="studentServiceImpl" class="lujing.serviceimpl.StudentServiceImpl"/>
  <!--配置springRMI启动服务-->
  <bean id="exporter1" class="org.springframework.remoting.rmi.RmiServiceExporter">
     <property name="serviceName" value="StudentService"/>
     <property name="service" ref="studentServiceImpl"/>
     <property name="serviceInterface" value="lujing.service.StudentService"/>
     <property name="registryPort" value="8562"/>
  </bean>


<bean id="indexServiceImpl" class="lujing.serviceimpl.IndexServiceImpl"/>
   <bean id="exporter2" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="serviceName" value="IndexService"/>
       <property name="service" ref="indexServiceImpl"/>
       <property name="serviceInterface" value="lujing.service.IndexService"/>
       <property name="registryPort" value="8562"/>
   </bean>

(2)在客户端配置接受的容器,同样使用spring管理。


<!--SpringRMI-->

<bean id="proxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  <property name="serviceInterface" value="lujing.service.StudentService"/>
  <property name="serviceUrl" value="rmi://192.168.31.44:8562/StudentService"/>
</bean>

  <bean id="proxy2" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
      <property name="serviceInterface" value="lujing.service.IndexService"/>
      <property name="serviceUrl" value="rmi://192.168.31.44:8562/IndexService"/>
  </bean>

这里需要注意:客户端的service接口与服务端的service一定要一致才可以。

配置好了之后启动服务端的容器。spring能够自动启动RMI的服务。

在客户端配置对应RMI代理工厂,对接service的实现类。

  public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext-service.xml");
       StudentService xx = context.getBean("proxy",StudentService.class);
       IndexService indexService = context.getBean("proxy2",IndexService.class);
       
       System.out.println(xx.findStudentList(null));
       System.out.println(indexService.selectLearnALL());
   }
}

启动一下。,能够打印出实现类对应的信息。说明链接成功了。

遇到问题:

1.服务端实现了自动注入,客户端怎么自动注入service的实现类?

2.WEB项目中怎么实现bean使用的时候再加载呢?

因为昨天把腾讯云的客户端配置成立由spring管理,在加载项目以及spring容器的时候就自动注入了该客户端。但是该客户端并没有被使用,也无法关闭。

所以一直报连接超时的问题。

明日计划

1.将服务端与客户端完全分离,部署上服务器。

收获

1.初步了解了分布式的设立模型。


返回列表 返回列表
评论

    分享到