springboot+HttpInvoke 实现RPC调用

开始用springboot2+hession4实现RPC服务时,发现第一个服务可以调用成功,但第二个就一直报 '<' is an unknown code。第一个服务还是可以调用的。参考网上的方法,客户端与服务端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、将接口注入在所需要的地方即可实现远程调用接口所定义的方法

猜你喜欢

转载自www.cnblogs.com/codechange/p/11285532.html