springboot+HttpInvoke 实现RPC调用的方法
开始用springboot2+hession4实现RPC服务时,发现第一个服务可以调用成功,但第二个就一直报'<'isanunknowncode。第一个服务还是可以调用的。参考网上的方法,客户端与服务端hession版本保持一致,查看本地版本是一致的, 换成其他版本也没有效果。设置重载方法为true,都没有效果。如果有其他小伙伴有过解决方法,望指正。 后改用用了spring自带的HTTPInvoke。现记录如下:
1、将服务端的服务暴露出来
@Configuration
public class HttpInvokeServiceConfig {
@Bean("/xxx")
public HttpInvokerServiceExporter rpcService(xxxService xxxService) {
HttpInvokerServiceExporter httpInvokerServiceExporter = new HttpInvokerServiceExporter();
httpInvokerServiceExporter.setService(xxxService);
httpInvokerServiceExporter.setServiceInterface(xxxService.class);
return httpInvokerServiceExporter;
}
}2、客户端,将接口交由代理去执行远程方法
@Configuration
public class ClientRpcConfig {
@Bean
public HttpInvokerProxyFactoryBean rpcService() {
HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
httpInvokerProxyFactoryBean.setServiceUrl(server_url);
httpInvokerProxyFactoryBean.setServiceInterface(xxxService.class);
return httpInvokerProxyFactoryBean;
}
}注意点
1)、服务端与客户端接口名一致、方法参数一致
2)、如果接口参数是对象的话,参数对象须实现序列化
3)、接口参数是对象的话,服务端与客户端对象名要一致、包路径也得一致。 不然会报找不到类
3、将接口注入在所需要的地方即可实现远程调用接口所定义的方法
到此这篇关于springboot+HttpInvoke 实现RPC调用的文章就介绍到这了,更多相关springboot RPC调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Mybatis和orcale update语句中接收参数为对象的实例代码
Mybatis的 mapper.xml 中 update 语句使用 if 标签判断对像属性是否为空值。本文重点给大家介绍Mybatis和orcale update语句中接收参数为对象的实例代码,需要的朋友参考下吧2017-09-09
如何解决Maven打包时每次都出现Download maven-metadata.xml卡住问题
这篇文章主要介绍了如何解决Maven打包时每次都出现Download maven-metadata.xml卡住问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-05-05


最新评论