Skip to content

Commit

Permalink
add Gateway mode extension (#205)
Browse files Browse the repository at this point in the history
* 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
wxbty and x-shadow-man authored Apr 27, 2023
1 parent 12502f2 commit fd0cb02
Show file tree
Hide file tree
Showing 30 changed files with 829 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public Invoker<?> getInvokerByIp(Address address) {

private void throwException(Address address) {
throw new RpcException("user specified server address : [" + address + "] is not a valid provider for service: ["
+ getUrl().getServiceKey() + "]");
+ getUrl().getServiceKey() + "]");
}


Expand Down Expand Up @@ -253,14 +253,14 @@ public <T> URL buildAddress(List<Invoker<T>> invokers, Address address, URL cons

private URL copyConsumerUrl(URL url, String ip, int port, Map<String, String> parameters) {
return URLBuilder.from(url)
.setHost(ip)
.setPort(port)
.setProtocol(url.getProtocol() == null ? DUBBO : url.getProtocol())
.setPath(url.getPath())
.clearParameters()
.addParameters(parameters)
.removeParameter(MONITOR_KEY)
.build();
.setHost(ip)
.setPort(port)
.setProtocol(url.getProtocol() == null ? DUBBO : url.getProtocol())
.setPath(url.getPath())
.clearParameters()
.addParameters(parameters)
.removeParameter(MONITOR_KEY)
.build();
}

public URL rebuildAddress(Address address, URL consumerUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public UserSpecifiedAddressRouter(URL referenceUrl) {
this.scheduledExecutorService = referenceUrl.getScopeModel().getDefaultExtension(ExecutorRepository.class).nextScheduledExecutor();
this.protocol = referenceUrl.getOrDefaultFrameworkModel().getExtensionLoader(Protocol.class).getAdaptiveExtension();
this.userSpecifiedServiceAddressBuilder = referenceUrl.getScopeModel().getExtensionLoader(UserSpecifiedServiceAddressBuilder.class)
.getExtension(referenceUrl.getParameter(USER_SPECIFIED_SERVICE_ADDRESS_BUILDER_KEY, DefaultUserSpecifiedServiceAddressBuilder.NAME));
.getExtension(referenceUrl.getParameter(USER_SPECIFIED_SERVICE_ADDRESS_BUILDER_KEY, DefaultUserSpecifiedServiceAddressBuilder.NAME));
}

@Override
Expand Down Expand Up @@ -240,7 +240,7 @@ private Invoker<T> refer(URL url) {

private void throwException(Address address) {
throw new RpcException("user specified server address : [" + address + "] is not a valid provider for service: ["
+ getUrl().getServiceKey() + "]");
+ getUrl().getServiceKey() + "]");
}

private Map<String, Invoker<T>> processIp(List<Invoker<T>> invokerList) {
Expand Down
2 changes: 1 addition & 1 deletion dubbo-extensions-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
</issueManagement>

<properties>
<revision>1.0.4-SNAPSHOT</revision>
<revision>1.0.5-SNAPSHOT</revision>
<dubbo.version>3.1.2</dubbo.version>
<spring.version>5.2.9.RELEASE</spring.version>
<spring-boot.version>2.4.1</spring-boot.version>
Expand Down
29 changes: 29 additions & 0 deletions dubbo-gateway-extensions/dubbo-gateway-common/pom.xml
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>
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";

}
42 changes: 42 additions & 0 deletions dubbo-gateway-extensions/dubbo-gateway-consumer/pom.xml
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>
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
injvm-initial=org.apache.dubbo.gateway.consumer.config.InjvmConfigPostProcessor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
omnSer=org.apache.dubbo.gateway.consumer.filter.OmnSerFilter
Loading

0 comments on commit fd0cb02

Please sign in to comment.