-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add gateway mode * code style * Increase the original parameter type, javaBeanDescriptor passes the actual parameter type * add gateway mod * revert spc * use spi byteAccessor * add licence * add licnece * fix meta-inf * fix bug * fix * fix ver * remove unuse * fix ver * remove unuse * add some opt * add some opt * fix dep * fix licence --------- Co-authored-by: x-shadow-man <1494445739@qq.com>
- Loading branch information
Showing
30 changed files
with
829 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<parent> | ||
<artifactId>dubbo-gateway-extensions</artifactId> | ||
<groupId>org.apache.dubbo.extensions</groupId> | ||
<version>${revision}</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>dubbo-gateway-common</artifactId> | ||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
...teway-common/src/main/java/org/apache/dubbo/gateway/common/OmnipotentCommonConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dubbo.gateway.common; | ||
|
||
public interface OmnipotentCommonConstants { | ||
|
||
//save origin group when service is omn | ||
String ORIGIN_GROUP_KEY = "originGroup"; | ||
|
||
String ORIGIN_GENERIC_PARAMETER_TYPES = "originGenericParameterTypes"; | ||
|
||
String ORIGIN_PARAMETER_TYPES_DESC = "originParameterTypesDesc"; | ||
|
||
String $INVOKE_OMN = "$invokeOmn"; | ||
|
||
String ORIGIN_PATH_KEY = "originPath"; | ||
|
||
String ORIGIN_METHOD_KEY = "originMethod"; | ||
|
||
String ORIGIN_VERSION_KEY = "originVersion"; | ||
|
||
String SPECIFY_ADDRESS = "specifyAddress"; | ||
String GATEWAY_MODE = "gatewayMode"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<parent> | ||
<artifactId>dubbo-gateway-extensions</artifactId> | ||
<groupId>org.apache.dubbo.extensions</groupId> | ||
<version>${revision}</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>dubbo-gateway-consumer</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.dubbo.extensions</groupId> | ||
<artifactId>dubbo-gateway-common</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
32 changes: 32 additions & 0 deletions
32
...umer/src/main/java/org/apache/dubbo/gateway/consumer/config/InjvmConfigPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.dubbo.gateway.consumer.config; | ||
|
||
import org.apache.dubbo.common.extension.Activate; | ||
import org.apache.dubbo.config.ConfigPostProcessor; | ||
import org.apache.dubbo.config.ReferenceConfig; | ||
import org.apache.dubbo.rpc.Constants; | ||
|
||
@Activate | ||
public class InjvmConfigPostProcessor implements ConfigPostProcessor { | ||
|
||
@Override | ||
public void postProcessReferConfig(ReferenceConfig referenceConfig) { | ||
referenceConfig.setScope(Constants.SCOPE_REMOTE); | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
...gateway-consumer/src/main/java/org/apache/dubbo/gateway/consumer/filter/OmnSerFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.dubbo.gateway.consumer.filter; | ||
|
||
import org.apache.dubbo.common.beanutil.JavaBeanDescriptor; | ||
import org.apache.dubbo.common.beanutil.JavaBeanSerializeUtil; | ||
import org.apache.dubbo.common.constants.CommonConstants; | ||
import org.apache.dubbo.common.extension.Activate; | ||
import org.apache.dubbo.common.logger.Logger; | ||
import org.apache.dubbo.common.logger.LoggerFactory; | ||
import org.apache.dubbo.common.utils.ReflectUtils; | ||
import org.apache.dubbo.rpc.Filter; | ||
import org.apache.dubbo.rpc.Invocation; | ||
import org.apache.dubbo.rpc.Invoker; | ||
import org.apache.dubbo.rpc.Result; | ||
import org.apache.dubbo.rpc.RpcContext; | ||
import org.apache.dubbo.rpc.RpcException; | ||
import org.apache.dubbo.rpc.RpcInvocation; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.GATEWAY_MODE; | ||
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.ORIGIN_GENERIC_PARAMETER_TYPES; | ||
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.ORIGIN_PARAMETER_TYPES_DESC; | ||
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.SPECIFY_ADDRESS; | ||
|
||
|
||
@Activate(group = CommonConstants.CONSUMER) | ||
public class OmnSerFilter implements Filter, Filter.Listener { | ||
|
||
private final static Logger logger = LoggerFactory.getLogger(OmnSerFilter.class); | ||
|
||
public static final String name = "specifyAddress"; | ||
|
||
@Override | ||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { | ||
|
||
Object address = invocation.get(SPECIFY_ADDRESS); | ||
if (address != null) { | ||
RpcContext.getClientAttachment().setAttachment(GATEWAY_MODE, "omn"); | ||
convertParameterTypeToJavaBeanDescriptor(invocation); | ||
} | ||
return invoker.invoke(invocation); | ||
} | ||
|
||
|
||
@Override | ||
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) { | ||
|
||
Object resData = appResponse.getValue(); | ||
if (resData == null) { | ||
return; | ||
} | ||
|
||
if (ReflectUtils.isPrimitives(resData.getClass())) { | ||
return; | ||
} | ||
generalizeJbdParameter(appResponse.getValue()); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) { | ||
|
||
} | ||
|
||
|
||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
private void generalizeJbdParameter(Object pojo) { | ||
if (pojo instanceof Collection) { | ||
|
||
Collection collection = (Collection) pojo; | ||
List list = new ArrayList(); | ||
for (Object obj : collection) { | ||
if (obj instanceof JavaBeanDescriptor) { | ||
list.add(JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) obj)); | ||
} else { | ||
list.add(obj); | ||
} | ||
} | ||
collection.clear(); | ||
collection.addAll(list); | ||
} | ||
|
||
if (pojo instanceof Map) { | ||
|
||
Map map = (Map) pojo; | ||
Map newMap = new HashMap(); | ||
for (Object key : map.keySet()) { | ||
|
||
Object value = map.get(key); | ||
if (key instanceof JavaBeanDescriptor) { | ||
key = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) key); | ||
} | ||
if (value instanceof JavaBeanDescriptor) { | ||
value = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) value); | ||
} | ||
newMap.put(key, value); | ||
|
||
} | ||
map.clear(); | ||
map.putAll(newMap); | ||
} | ||
|
||
// public field | ||
for (Field field : pojo.getClass().getDeclaredFields()) { | ||
try { | ||
field.setAccessible(true); | ||
Object fieldValue = field.get(pojo); | ||
if (fieldValue instanceof JavaBeanDescriptor) { | ||
field.set(pojo, JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) fieldValue)); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
public static void convertParameterTypeToJavaBeanDescriptor(Invocation invocation) { | ||
if (!(invocation instanceof RpcInvocation)) { | ||
logger.warn("Non-RpcInvocation type, gateway mode does not take effect, type:" + invocation.getClass().getName()); | ||
return; | ||
} | ||
Class<?>[] parameterTypes = invocation.getParameterTypes(); | ||
boolean reqFirst = Arrays.stream(parameterTypes).noneMatch(param -> param == JavaBeanDescriptor.class); | ||
if (reqFirst) { | ||
invocation.setObjectAttachment(ORIGIN_GENERIC_PARAMETER_TYPES, getDesc(parameterTypes)); | ||
invocation.setObjectAttachment(ORIGIN_PARAMETER_TYPES_DESC, ((RpcInvocation) invocation).getParameterTypesDesc()); | ||
Arrays.fill(parameterTypes, JavaBeanDescriptor.class); | ||
|
||
Object[] arguments = invocation.getArguments(); | ||
for (int i = 0; i < arguments.length; i++) { | ||
JavaBeanDescriptor jbdArg = JavaBeanSerializeUtil.serialize(arguments[i]); | ||
arguments[i] = jbdArg; | ||
} | ||
|
||
((RpcInvocation) invocation).setParameterTypesDesc(ReflectUtils.getDesc(parameterTypes)); | ||
((RpcInvocation) invocation).setCompatibleParamSignatures(Stream.of(parameterTypes).map(Class::getName).toArray(String[]::new)); | ||
} | ||
|
||
} | ||
|
||
private static String[] getDesc(Class<?>[] parameterTypes) { | ||
return Arrays.stream(parameterTypes).map(Class::getName).toArray(String[]::new); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...er/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.config.ConfigPostProcessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
injvm-initial=org.apache.dubbo.gateway.consumer.config.InjvmConfigPostProcessor |
1 change: 1 addition & 0 deletions
1
...o-gateway-consumer/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
omnSer=org.apache.dubbo.gateway.consumer.filter.OmnSerFilter |
Oops, something went wrong.