首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Hessian 跨項目遠程調用實戰:Spring 整合配置全流程

     編輯:Java 綜合教學

Hessian 跨項目遠程調用實戰:Spring 整合配置全流程

Hessian 是輕量級二進制 RPC 協議,適合服務間高性能通信。本文記錄服務端導出與客戶端調用的完整 Spring XML 配置。

核心注意事項

  1. 方法參數為對象時,該類必須實現 Serializable。
  2. 服務端導出路徑與客戶端調用路徑必須完全一致。

1. 服務端配置(remote-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- 業務實現 Bean -->
    <bean id="warnInfoService" class="com.cmsz.rc.services.impl.WarnInfoServiceImpl" />

    <!-- Hessian 導出 -->
    <bean name="/logPersonHessian"
          class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="warnInfoService" />
        <property name="serviceInterface" value="com.cmsz.rc.services.WarnInfoService" />
    </bean>
</beans>

/logPersonHessian 為導出的 URL 路徑片段,客戶端需對應。

2. 客戶端配置(remote-client.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="myServiceClient"
          class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl"
            value="https://192.168.215.87:8080/RiskControl_TimingDetection/logPersonHessian" />
        <property name="serviceInterface"
            value="com.cmsz.rc.services.WarnInfoService" />
    </bean>
</beans>
  • serviceUrl = 服務端 IP:Port + ContextPath + 導出名。
  • serviceInterface 必須與服務端聲明的接口一致。

3. 客戶端調用代碼

ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:remote-client.xml");
WarnInfoService service = (WarnInfoService) ctx.getBean("myServiceClient");
service.目標方法();

常見坑點排查

  • 序列化失敗:檢查參數/返回值對象是否實現 Serializable,且 serialVersionUID 兩端一致。
  • 404 錯誤:核對 ContextPath、導出名、端口、防火牆。
  • 接口不匹配:雙方接口包名、方法簽名必須完全相同。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved