Hessian 是輕量級二進制 RPC 協議,適合服務間高性能通信。本文記錄服務端導出與客戶端調用的完整 Spring XML 配置。
Serializable。<?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 路徑片段,客戶端需對應。
<?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 必須與服務端聲明的接口一致。ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:remote-client.xml");
WarnInfoService service = (WarnInfoService) ctx.getBean("myServiceClient");
service.目標方法();
Serializable,且 serialVersionUID 兩端一致。