spring rmi以及hessian远程调用

首先是spring rmi远程调用

========开始=========================================================

端口号不能被占用,找一个你系统没有的端口,去使用

客户端:

      public class TestRmiService1 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("springbean.xml");   
 TestRMIService bean = (TestRMIService) ac.getBean("testRMIService"); 
System.out.println(bean.sayHello());

}

      类路径下的springbean.xml

     <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--客户端代理的bean,跟调用本地代码一样,因为通信的工作,有代理进行处理 -->
<bean id="testRMIService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:9999/testService" />
<property name="serviceInterface" value="com.test.service.TestRMIService" />
</bean>
<!-- <bean id="testRmiService1" class="com.test.rmi.service.TestRmiService1"> 
<property name="testService" ref="testService" /> </bean> -->

</beans>

服务端:

      接口:public interface TestRMIService {
                public String sayHello();

                }

       实现类:

         public class TestRMIServiceImpl implements TestRMIService {

  @Override
   public String sayHello() {
return "hi,girl";
   }

        类路径下暴露服务的bean.xml文件

        <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<bean id="testRMIServiceImpl" class="com.test.controller.TestRMIServiceImpl"></bean>
<bean id="testRMIService" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="testRMIServiceImpl"></property>
<property name="serviceName" value="testService"></property>
<property name="serviceInterface" value="com.test.service.TestRMIService"></property>
<property name="registryPort" value="9999"></property>
</bean>

</beans>

pom文件:

      <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-web</artifactId>  
    <version>4.1.1.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-webmvc</artifactId>  
    <version>4.1.1.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-oxm</artifactId>  
    <version>4.1.1.RELEASE</version>  
</dependency>  

  </dependencies>

========springrmi远程调用结束======================================

========springhessian开始=========================================

首先maven 创建一个web工程

接口:

       public interface HelloHessianService {
    public String sayHi();

}

实现类;

     public class HelloHessianServiceImpl implements HelloHessianService {
//hessian 远程调用协议,使用二进制进行交互,它的二进制可以是非java语言
public String sayHi() {
// TODO Auto-generated method stub
return "hi girls";
}
public static void main(String[] args) {
//Spring Hessian代理Servelet  
        String url = "http://localhost:8080//helloHessianService";  
        HessianProxyFactory factory = new HessianProxyFactory();  
        HelloHessianService api;
try {
api = (HelloHessianService) factory.create(HelloHessianService.class, url);
       System.out.println(api.sayHi());  
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
}

}

web.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>testhessian</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- 完整的远程调用请求Url:http://localhost:8080/HessianSpringServer/sr/* -->
<servlet-name>springremoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springremoting</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

默认web-inf下暴露服务的bean:springremoting-servlet.xml

      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>  
    <!-- 定义普通bean实例 -->
<bean id="helloHessianServiceImpl" class="com.ll.serviceimpl.HelloHessianServiceImpl" />
<!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
// 需要特别注意下name的名字带/
<bean name="/helloHessianService"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要导出的目标bean -->
<property name="service" ref="helloHessianServiceImpl" />
<!-- Hessian服务的接口 -->
<property name="serviceInterface" value="com.ll.service.HelloHessianService" />
</bean> 
</beans> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值