Skip to content

Commit

Permalink
增加nop.biz.use-dict-service开关,开启后字典获取将使用RPC服务
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Aug 13, 2024
1 parent f7ca6fe commit 6673d47
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<biz x:schema="/nop/schema/biz/xbiz.xdef" xmlns:x="/nop/schema/xdsl.xdef" xmlns:feature="feature">

<actions>
<query name="getDict" feature:on="nop.biz.use-dict-service">
<arg name="dictName" type="String" mandatory="true"/>
<arg name="svcCtx" kind="ServiceContext"/>

<return type="io.nop.api.core.beans.DictBean"/>

<source>
<c:script>
const dictServiceName = $config.var('nop.biz.dict-service.name','nop-sys-service')
</c:script>
<rpc:invoke serviceName="${dictServiceName}" serviceMethod="DictProvider__getDict"
data="${ {dictName}}" cancelToken="${svcCtx}"/>
</source>
</query>
</actions>
</biz>
Original file line number Diff line number Diff line change
@@ -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<ApiResponse<?>> dynamicInvokeAsync(ApiRequest<?> request, ICancelToken cancelToken);

default CompletionStage<ApiResponse<?>> dynamicInvokeWithArgs(
String serviceName, String serviceMethod, Map<String, Object> headers,
Object data, FieldSelectionBean selection, ICancelToken cancelToken) {
ApiRequest<Object> request = new ApiRequest<>();
request.setHeaders(headers);
request.setSelection(selection);
request.setData(data);

ApiHeaders.setSvcName(request, serviceName);
ApiHeaders.setSvcAction(request, serviceMethod);
return dynamicInvokeAsync(request, cancelToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<entry key="version" value="@cfg:nop.application.version|1.0.0"/>
<entry key="group" value="@cfg:nop.application.group|default"/>
<entry key="zone" value="@cfg:nop.application.zone|"/>
<entry key="kind" value="@cfg:nop.application.kind|http" />
<entry key="kind" value="@cfg:nop.application.kind|http"/>
</map>
</property>
</bean>
Expand Down Expand Up @@ -119,4 +119,8 @@
<ref bean="nopRpcServiceInterceptor_clientContext"/>
</util:list>

<bean id="nopDynamicRpcService" parent="AbstractRpcProxyFactoryBean"
ioc:type="io.nop.rpc.api.IDynamicRpcService">
<property name="serviceName" value="DynamicRpcService"/>
</bean>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<lib x:schema="/nop/schema/xlib.xdef" xmlns:x="/nop/schema/xdsl.xdef">

<tags>
<invoke>
<attr name="serviceName" type="String" mandatory="true"/>
<attr name="serviceMethod" type="String" mandatory="true"/>
<attr name="data" optional="true"/>
<attr name="headers" type="Map" optional="true"/>
<attr name="selection" type="io.nop.api.core.beans.FieldSelectionBean"
stdDomain="field-selection" optional="true"/>
<attr name="cancelToken" type="io.nop.api.core.util.ICancelToken" optional="true"/>

<source>
<c:script><![CDATA[
const rpcService = inject('nopDynamicRpcService');
return rpcService.dynamicInvokeWithArgs(serviceName, serviceMethod,
headers, data, selection, cancelToken);
]]></c:script>
</source>
</invoke>
</tags>
</lib>
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,12 +61,8 @@ public ApiRequest<Object> toRequest(String serviceName, IFunctionModel method, O
IFunctionArgument argModel = method.getArgs().get(i);
if (argModel.getRawClass() == ApiRequest.class) {
req = (ApiRequest<Object>) 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 {
Expand All @@ -79,8 +76,13 @@ public ApiRequest<Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions nop-sys/nop-sys-app/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

nop:
application:
name: nop-sys-service

debug: true
auth:
login:
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6673d47

Please sign in to comment.