spring httpinvoker入門小程式
阿新 • • 發佈:2018-12-11
背景瞭解:
Httpinvoker是SpringFramework提供的遠端通訊協議,只能用於JAVA程式間的通訊,且服務端和客戶端必須使用SpringFramework。
1、在springframework的系統中,正常建立好service和service介面。
介面,一個test方法。
package com.eshore.act.service.active.customization.common.service;
public interface ICommonService {
public String test();
}
實現類,test方法實現。
package com.eshore.act.service.active.customization.common.service.impl; @Service("commonService") public class CommonServiceImpl implements ICommonService { public String test() { return "remote method invoke"; } }
2、暴露服務-服務端,把服務端的介面暴露出去,提供外界訪問。
<!-- 活動主服務 --> <bean id="ws_commonService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="commonService" /> <property name="serviceInterface" value="com.eshore.act.service.active.customization.common.service.ICommonService" /> </bean>
3、客戶端需要一份和服務端一樣的介面
package com.eshore.act.web.wsdefine.customization.common;
public interface ICommonService {
public String test();
}
4、客戶端配置遠端服務
<!-- 活動主服務 --> <bean id="commonService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" lazy-init="true"> <property name="serviceUrl"><value>localhost:8080/act-service/remote.ICommonService</value></property> <property name="serviceInterface"><value>com.eshore.act.web.wsdefine.customization.common.ICommonService</value></property> </bean>
5、controller 示例
@RestController
public class DemoAction {
@Autowired
private ICommonService commonService;
@RequestMapping("hello")
public String test(){
return commonService.test();
}
}
6、效果展示