好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

springboot+HttpInvoke 实现RPC调用的方法

开始用springboot2+hession4实现RPC服务时,发现第一个服务可以调用成功,但第二个就一直报'<'isanunknowncode。第一个服务还是可以调用的。参考网上的方法,客户端与服务端hession版本保持一致,查看本地版本是一致的, 换成其他版本也没有效果。设置重载方法为true,都没有效果。如果有其他小伙伴有过解决方法,望指正。 后改用用了spring自带的HTTPInvoke。现记录如下:

1、将服务端的服务暴露出来

?

1

2

3

4

5

6

7

8

9

10

@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、客户端,将接口交由代理去执行远程方法

?

1

2

3

4

5

6

7

8

9

10

@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调用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/codechange/p/11285532.html

查看更多关于springboot+HttpInvoke 实现RPC调用的方法的详细内容...

  阅读:35次