dubbo泛化调用
java调用其他api时,一般都是需要引用对应的jar包,在使用对应的出入参Class进行调用api接口,还是通过泛化方式即接口全路径、接口名称、入参为Map、出参为Map 通过这种方式可以屏蔽远程依赖。
泛化调用核心接口
根据传入的接口方法、入参类型字符串数组、入参值字符数组、
public interface GenericService { /** * Generic invocation * * @param method Method name, e.g. findPerson. If there are overridden methods, parameter info is * required, e.g. findPerson(java.lang.String) * @param parameterTypes Parameter types * @param args Arguments * @return invocation return value * @throws GenericException potential exception thrown from the invocation */ Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException; default CompletableFuture<Object> $invokeAsync(String method, String[] parameterTypes, Object[] args) throws GenericException { Object object = $invoke(method, parameterTypes, args); if (object instanceof CompletableFuture) { return (CompletableFuture<Object>) object; } return CompletableFuture.completedFuture(object); } }
一种方式是实现GenericService接口
public class GenericImplOfHelloService implements GenericService { @Override public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException { if (method.equals("sayHello")) { System.out.print("executing sayHello."); throw new RuntimeException("sayHello: throws exception"); } else if (method.equals("sayHelloAsync")) { System.out.print("executing sayHelloAsync."); return CompletableFuture.completedFuture("sayHelloAsync: hello " + args[0]); } else { try { return defaultOperation(method, parameterTypes, args); } catch (Exception e) { throw new GenericException(e); } } } private Object defaultOperation(String method, String[] parameterTypes, Object[] args) throws Exception { throw new UnsupportedOperationException("method does not exist."); } }
具体调用方式
ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
//指定api接口
referenceConfig.setInterface("org.apache.dubbo.samples.generic.call.api.HelloService");
applicationConfig.setRegistry(registryConfig);
referenceConfig.setApplication(applicationConfig);
referenceConfig.setGeneric("true");
referenceConfig.setAsync(true);
referenceConfig.setTimeout(7000);
genericService = referenceConfig.get();
//调用对应接口的方法、入参类型、入参值
Object result = genericService.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"world"});
provider实际上注册到了zk中的形式是一个url
dubbo://192.168.239.1:20880/org.apache.dubbo.samples.generic.call.api.HelloService?application=generic-provider&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=org.apache.dubbo.samples.generic.call.api.HelloService&methods=sayHello,sayHelloAsync,sayHelloAsyncComplex,sayHelloAsyncGenericComplex&prefer.serialization=fastjson2,hessian2&release=3.2.6&service-name-mapping=true&side=provider×tamp=1701356673623&token=141fb589-c2b7-447d-9e48-4da2eaf06c90
收藏文章数量从多到少与“把书读薄”是一个道理
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!