今天小编工作上遇到一个关于dubbo泛化调用比较有代表性意义的坑,写出来给小伙伴们分享一下,希望大家能从我的经验中获得一些成长,未来遇到类似的场景能少走一些弯路。 我们来看下dubbo框架中对泛化调用的接口定义:
public interface GenericService {
/** * 泛化调用 * * @param method 方法名,如:findPerson,如果有重载方法,需带上参数列表,如:findPerson(java.lang.String) * @param parameterTypes 参数类型 * @param args 参数列表 * @return 返回值 * @throws Throwable 方法抛出的异常 */ Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException;}
可以看到泛化调用,需要入参方法名,参数类型列表和参数值列表。
再来看段dubbo泛化调用的示例代码:// 注册中心RegistryConfig registry = new RegistryConfig();registry.setAddress("127.0.0.1:2181");registry.setProtocol("zookeeper");// 应用信息ApplicationConfig application = new ApplicationConfig();application.setName("GenericInvokeTest");application.setRegistry(registry);// 泛化接口ReferenceConfig reference = new ReferenceConfig<>();// 真实接口reference.setInterface("com.test.pdf.service.PdfConvertService");// dubbo注册的版本号reference.setVersion("1.0.0");// 超时时间reference.setTimeout(10000);// 声明为泛化接口reference.setGeneric(true);r