From 6673d47e6b97bcbda4d5fb1997c5966da1039ffb Mon Sep 17 00:00:00 2001 From: canonical Date: Tue, 13 Aug 2024 21:35:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0nop.biz.use-dict-service?= =?UTF-8?q?=E5=BC=80=E5=85=B3=EF=BC=8C=E5=BC=80=E5=90=AF=E5=90=8E=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E8=8E=B7=E5=8F=96=E5=B0=86=E4=BD=BF=E7=94=A8RPC?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/model/DictProvider/DictProvider.xbiz | 20 ++++++++++++++ .../io/nop/rpc/api/IDynamicRpcService.java | 27 +++++++++++++++++++ .../rpc/beans/rpc-cluster-defaults.beans.xml | 6 ++++- .../main/resources/_vfs/nop/rpc/xlib/rpc.xlib | 23 ++++++++++++++++ .../java/io/nop/rpc/core/RpcConstants.java | 4 +++ .../reflect/DefaultRpcMessageTransformer.java | 14 +++++----- .../core/reflect/RpcInvocationHandler.java | 6 +++-- .../src/main/resources/application.yaml | 14 +++++----- .../xdef/domain/SimpleStdDomainHandlers.java | 3 +++ 9 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 nop-biz/src/main/resources/_vfs/nop/core/model/DictProvider/DictProvider.xbiz create mode 100644 nop-rpc/nop-rpc-api/src/main/java/io/nop/rpc/api/IDynamicRpcService.java create mode 100644 nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/xlib/rpc.xlib diff --git a/nop-biz/src/main/resources/_vfs/nop/core/model/DictProvider/DictProvider.xbiz b/nop-biz/src/main/resources/_vfs/nop/core/model/DictProvider/DictProvider.xbiz new file mode 100644 index 000000000..3c8db2ad1 --- /dev/null +++ b/nop-biz/src/main/resources/_vfs/nop/core/model/DictProvider/DictProvider.xbiz @@ -0,0 +1,20 @@ + + + + + + + + + + + + + const dictServiceName = $config.var('nop.biz.dict-service.name','nop-sys-service') + + + + + + \ No newline at end of file diff --git a/nop-rpc/nop-rpc-api/src/main/java/io/nop/rpc/api/IDynamicRpcService.java b/nop-rpc/nop-rpc-api/src/main/java/io/nop/rpc/api/IDynamicRpcService.java new file mode 100644 index 000000000..eeb3dbcc2 --- /dev/null +++ b/nop-rpc/nop-rpc-api/src/main/java/io/nop/rpc/api/IDynamicRpcService.java @@ -0,0 +1,27 @@ +package io.nop.rpc.api; + +import io.nop.api.core.beans.ApiRequest; +import io.nop.api.core.beans.ApiResponse; +import io.nop.api.core.beans.FieldSelectionBean; +import io.nop.api.core.util.ApiHeaders; +import io.nop.api.core.util.ICancelToken; + +import java.util.Map; +import java.util.concurrent.CompletionStage; + +public interface IDynamicRpcService { + CompletionStage> dynamicInvokeAsync(ApiRequest request, ICancelToken cancelToken); + + default CompletionStage> dynamicInvokeWithArgs( + String serviceName, String serviceMethod, Map headers, + Object data, FieldSelectionBean selection, ICancelToken cancelToken) { + ApiRequest request = new ApiRequest<>(); + request.setHeaders(headers); + request.setSelection(selection); + request.setData(data); + + ApiHeaders.setSvcName(request, serviceName); + ApiHeaders.setSvcAction(request, serviceMethod); + return dynamicInvokeAsync(request, cancelToken); + } +} diff --git a/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/beans/rpc-cluster-defaults.beans.xml b/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/beans/rpc-cluster-defaults.beans.xml index 22e37319a..61f0ad4a2 100644 --- a/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/beans/rpc-cluster-defaults.beans.xml +++ b/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/beans/rpc-cluster-defaults.beans.xml @@ -21,7 +21,7 @@ - + @@ -119,4 +119,8 @@ + + + \ No newline at end of file diff --git a/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/xlib/rpc.xlib b/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/xlib/rpc.xlib new file mode 100644 index 000000000..917c1c63c --- /dev/null +++ b/nop-rpc/nop-rpc-cluster/src/main/resources/_vfs/nop/rpc/xlib/rpc.xlib @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/RpcConstants.java b/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/RpcConstants.java index 4226f4e3c..c8e1384c5 100644 --- a/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/RpcConstants.java +++ b/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/RpcConstants.java @@ -32,4 +32,8 @@ public interface RpcConstants { String PROP_HTTP_METHOD = "httpMethod"; String PROP_HTTP_URL = "httpUrl"; + + String METHOD_DYNAMIC_INVOKE = "dynamicInvoke"; + + String SERVICE_DYNAMIC_RPC_SERVICE = "DynamicRpcService"; } diff --git a/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/DefaultRpcMessageTransformer.java b/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/DefaultRpcMessageTransformer.java index 6d0fb6bd2..40d6e6ee4 100644 --- a/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/DefaultRpcMessageTransformer.java +++ b/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/DefaultRpcMessageTransformer.java @@ -22,6 +22,7 @@ import io.nop.core.reflect.bean.BeanTool; import io.nop.core.reflect.bean.IBeanModel; import io.nop.core.type.IGenericType; +import io.nop.rpc.core.RpcConstants; import java.util.HashMap; import java.util.List; @@ -60,12 +61,8 @@ public ApiRequest toRequest(String serviceName, IFunctionModel method, O IFunctionArgument argModel = method.getArgs().get(i); if (argModel.getRawClass() == ApiRequest.class) { req = (ApiRequest) args[0]; - ApiHeaders.setSvcName(req, serviceName); - ApiHeaders.setSvcAction(req, methodName); } else if (argModel.isAnnotationPresent(RequestBean.class)) { req = ApiRequest.build(args[0]); - ApiHeaders.setSvcName(req, serviceName); - ApiHeaders.setSvcAction(req, methodName); } else if (argModel.getType().isAssignableTo(ICancelToken.class)) { // 忽略当前参数,继续执行 } else { @@ -79,8 +76,13 @@ public ApiRequest toRequest(String serviceName, IFunctionModel method, O if (req == null) req = new ApiRequest<>(); - ApiHeaders.setSvcName(req, serviceName); - ApiHeaders.setSvcAction(req, methodName); + if (!serviceName.equals(RpcConstants.SERVICE_DYNAMIC_RPC_SERVICE)) + ApiHeaders.setSvcName(req, serviceName); + + // 如果是dynamicInvoke方法,则可以使用ApiRequest中设置的svc-action + if (ApiHeaders.getSvcAction(req) == null || !methodName.equals(RpcConstants.METHOD_DYNAMIC_INVOKE)) + ApiHeaders.setSvcAction(req, methodName); + initSelection(req, method); if (params != null && req.getData() == null) { diff --git a/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/RpcInvocationHandler.java b/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/RpcInvocationHandler.java index e404ba336..335995d2f 100644 --- a/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/RpcInvocationHandler.java +++ b/nop-rpc/nop-rpc-core/src/main/java/io/nop/rpc/core/reflect/RpcInvocationHandler.java @@ -9,6 +9,7 @@ import io.nop.api.core.beans.ApiRequest; import io.nop.api.core.beans.ApiResponse; +import io.nop.api.core.util.ApiHeaders; import io.nop.api.core.util.ICancelToken; import io.nop.commons.util.DestroyHelper; import io.nop.core.reflect.IFunctionModel; @@ -61,9 +62,10 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl IFunctionModel methodModel = methods.computeIfAbsent(method, mtd -> MethodModelBuilder.from(mtd.getDeclaringClass(), mtd)); - String methodName = transformer.getMethodName(methodModel); - ApiRequest req = transformer.toRequest(serviceName, methodModel, args); + + String methodName = ApiHeaders.getSvcAction(req); + ICancelToken cancelToken = getCancelToken(args); IRpcServiceInvocation inv = new DefaultRpcServiceInvocation(serviceName, methodName, req, cancelToken, false, diff --git a/nop-sys/nop-sys-app/src/main/resources/application.yaml b/nop-sys/nop-sys-app/src/main/resources/application.yaml index 84a72d1b0..e947f8d93 100644 --- a/nop-sys/nop-sys-app/src/main/resources/application.yaml +++ b/nop-sys/nop-sys-app/src/main/resources/application.yaml @@ -1,5 +1,7 @@ - nop: + application: + name: nop-sys-service + debug: true auth: login: @@ -17,12 +19,12 @@ nop: jdbc-url: jdbc:h2:./db/test username: sa password: -# driver-class-name: com.mysql.cj.jdbc.Driver -# jdbc-url: jdbc:mysql://127.0.0.1:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC -# username: nop -# password: nop-test + # driver-class-name: com.mysql.cj.jdbc.Driver + # jdbc-url: jdbc:mysql://127.0.0.1:3306/dev?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC + # username: nop + # password: nop-test -# 支持graphql __schema查询, + # 支持graphql __schema查询, graphql: schema-introspection: enabled: true diff --git a/nop-xlang/src/main/java/io/nop/xlang/xdef/domain/SimpleStdDomainHandlers.java b/nop-xlang/src/main/java/io/nop/xlang/xdef/domain/SimpleStdDomainHandlers.java index 2d2e14149..6e1008805 100644 --- a/nop-xlang/src/main/java/io/nop/xlang/xdef/domain/SimpleStdDomainHandlers.java +++ b/nop-xlang/src/main/java/io/nop/xlang/xdef/domain/SimpleStdDomainHandlers.java @@ -1363,6 +1363,9 @@ public IGenericType getGenericType(boolean mandatory, IStdDomainOptions options) @Override public Object parseProp(IStdDomainOptions options, SourceLocation loc, String propName, Object text, XLangCompileTool cp) { + if (text instanceof FieldSelectionBean) + return text; + try { return new FieldSelectionBeanParser().parseFromText(loc, text.toString()); } catch (NopException e) {