java很多分布式框架都是基于远程通信技术实现的,,好像在java远程通信技术中,rmi的性能比较好,。。
rmi每次远程方法调用 是不是 多线程的了。。。。。测试了一下啊
server端
<beans> <bean id="helloService" class="com.logcd.spring.rmi.HelloServiceImpl"/> <!--RmiServiceExporter显示地支持使用RMI调用器暴露任何非RMI服务--> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="helloService"/> <property name="serviceInterface" value="com.logcd.spring.rmi.HelloService"/> <!--定义要暴露的服务名可以与输出的bean不同名,客户端通过这个名字来调用服务--> <property name="serviceName" value ="HelloService"/> <!--覆盖RMI注册端口号(1099),通常应用服务器也会维护RMI注册,最好不要冲突--> <property name="registryPort" value="1199"/> </bean>
client
<beans> <!--使用RmiProxyFactoryBean连接服务端 这里设置--scope="prototype"也还是单例的>
<bean id="serviceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean" scope="prototype"> <property name="serviceUrl" value="rmi://localhost:1199/HelloService"/> <property name="serviceInterface" value="com.logcd.spring.rmi.HelloService"/> </bean> <bean id="obj" class="java.lang.Object" scope="prototype"> </bean> </beans>
server code
public String doHello(String name) { int i = 0; try { Thread.sleep(1000);//每次都会sleep一秒 System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } return "Hello , " + name; }
public static void main(String args[]){ ApplicationContext context= new ClassPathXmlApplicationContext( new String[]{"rmi-server.xml"}); }
client code
public static void main(String args[]){ ApplicationContext context= new ClassPathXmlApplicationContext( new String[]{"rmi-client.xml"}); for(int i=0;i<100;i++){ HelloService service = (HelloService)context.getBean("serviceProxy"); Object obj = context.getBean("obj"); System.out.println(service.hashCode()+"----obj=="+obj.hashCode()); System.out.println(service.doHello("logcd")); System.out.println("----------------------"+i); }
client 控制台打印
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. 269856977----obj==13238549 Hello , logcd ----------------------0 269856977----obj==29131495 Hello , logcd ----------------------1 269856977----obj==14440411 Hello , logcd ----------------------2 269856977----obj==13459339 Hello , logcd ----------------------3 269856977----obj==7579563 Hello , logcd ----------------------4 269856977----obj==23255990 Hello , logcd ----------------------5 269856977----obj==9182681 Hello , logcd ----------------------6 269856977----obj==4898828 Hello , logcd ----------------------7 269856977----obj==22634321
scope="prototype" 设置了。。但是 rmi还是单利的。,,单线程的。。。
server 控制台
RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185 RMI TCP Connection(14)-10.191.72.185
说明 一个client 连接server是一个线程,,,,,,,,