springboot RPC调用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot RPC调用

秋水秋色   2022-05-23 我要评论

开始用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、将接口注入在所需要的地方即可实现远程调用接口所定义的方法

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们