Skip to content

Commit fd0cb02

Browse files
wxbtyx-shadow-man
andauthored
add Gateway mode extension (#205)
* 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 <[email protected]>
1 parent 12502f2 commit fd0cb02

File tree

30 files changed

+829
-23
lines changed

30 files changed

+829
-23
lines changed

dubbo-cluster-extensions/dubbo-cluster-specify-address-dubbo2/src/main/java/org/apache/dubbo/rpc/cluster/specifyaddress/UserSpecifiedAddressRouter.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public Invoker<?> getInvokerByIp(Address address) {
175175

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

181181

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

254254
private URL copyConsumerUrl(URL url, String ip, int port, Map<String, String> parameters) {
255255
return URLBuilder.from(url)
256-
.setHost(ip)
257-
.setPort(port)
258-
.setProtocol(url.getProtocol() == null ? DUBBO : url.getProtocol())
259-
.setPath(url.getPath())
260-
.clearParameters()
261-
.addParameters(parameters)
262-
.removeParameter(MONITOR_KEY)
263-
.build();
256+
.setHost(ip)
257+
.setPort(port)
258+
.setProtocol(url.getProtocol() == null ? DUBBO : url.getProtocol())
259+
.setPath(url.getPath())
260+
.clearParameters()
261+
.addParameters(parameters)
262+
.removeParameter(MONITOR_KEY)
263+
.build();
264264
}
265265

266266
public URL rebuildAddress(Address address, URL consumerUrl) {

dubbo-cluster-extensions/dubbo-cluster-specify-address-dubbo3/src/main/java/org/apache/dubbo/rpc/cluster/specifyaddress/UserSpecifiedAddressRouter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public UserSpecifiedAddressRouter(URL referenceUrl) {
7070
this.scheduledExecutorService = referenceUrl.getScopeModel().getDefaultExtension(ExecutorRepository.class).nextScheduledExecutor();
7171
this.protocol = referenceUrl.getOrDefaultFrameworkModel().getExtensionLoader(Protocol.class).getAdaptiveExtension();
7272
this.userSpecifiedServiceAddressBuilder = referenceUrl.getScopeModel().getExtensionLoader(UserSpecifiedServiceAddressBuilder.class)
73-
.getExtension(referenceUrl.getParameter(USER_SPECIFIED_SERVICE_ADDRESS_BUILDER_KEY, DefaultUserSpecifiedServiceAddressBuilder.NAME));
73+
.getExtension(referenceUrl.getParameter(USER_SPECIFIED_SERVICE_ADDRESS_BUILDER_KEY, DefaultUserSpecifiedServiceAddressBuilder.NAME));
7474
}
7575

7676
@Override
@@ -240,7 +240,7 @@ private Invoker<T> refer(URL url) {
240240

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

246246
private Map<String, Invoker<T>> processIp(List<Invoker<T>> invokerList) {

dubbo-extensions-dependencies-bom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
</issueManagement>
9090

9191
<properties>
92-
<revision>1.0.4-SNAPSHOT</revision>
92+
<revision>1.0.5-SNAPSHOT</revision>
9393
<dubbo.version>3.1.2</dubbo.version>
9494
<spring.version>5.2.9.RELEASE</spring.version>
9595
<spring-boot.version>2.4.1</spring-boot.version>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19+
<parent>
20+
<artifactId>dubbo-gateway-extensions</artifactId>
21+
<groupId>org.apache.dubbo.extensions</groupId>
22+
<version>${revision}</version>
23+
<relativePath>../pom.xml</relativePath>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<artifactId>dubbo-gateway-common</artifactId>
28+
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.gateway.common;
19+
20+
public interface OmnipotentCommonConstants {
21+
22+
//save origin group when service is omn
23+
String ORIGIN_GROUP_KEY = "originGroup";
24+
25+
String ORIGIN_GENERIC_PARAMETER_TYPES = "originGenericParameterTypes";
26+
27+
String ORIGIN_PARAMETER_TYPES_DESC = "originParameterTypesDesc";
28+
29+
String $INVOKE_OMN = "$invokeOmn";
30+
31+
String ORIGIN_PATH_KEY = "originPath";
32+
33+
String ORIGIN_METHOD_KEY = "originMethod";
34+
35+
String ORIGIN_VERSION_KEY = "originVersion";
36+
37+
String SPECIFY_ADDRESS = "specifyAddress";
38+
String GATEWAY_MODE = "gatewayMode";
39+
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19+
<parent>
20+
<artifactId>dubbo-gateway-extensions</artifactId>
21+
<groupId>org.apache.dubbo.extensions</groupId>
22+
<version>${revision}</version>
23+
<relativePath>../pom.xml</relativePath>
24+
</parent>
25+
<modelVersion>4.0.0</modelVersion>
26+
27+
<artifactId>dubbo-gateway-consumer</artifactId>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.apache.dubbo</groupId>
32+
<artifactId>dubbo</artifactId>
33+
<scope>provided</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.dubbo.extensions</groupId>
37+
<artifactId>dubbo-gateway-common</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
40+
</dependencies>
41+
42+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.gateway.consumer.config;
19+
20+
import org.apache.dubbo.common.extension.Activate;
21+
import org.apache.dubbo.config.ConfigPostProcessor;
22+
import org.apache.dubbo.config.ReferenceConfig;
23+
import org.apache.dubbo.rpc.Constants;
24+
25+
@Activate
26+
public class InjvmConfigPostProcessor implements ConfigPostProcessor {
27+
28+
@Override
29+
public void postProcessReferConfig(ReferenceConfig referenceConfig) {
30+
referenceConfig.setScope(Constants.SCOPE_REMOTE);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.gateway.consumer.filter;
18+
19+
import org.apache.dubbo.common.beanutil.JavaBeanDescriptor;
20+
import org.apache.dubbo.common.beanutil.JavaBeanSerializeUtil;
21+
import org.apache.dubbo.common.constants.CommonConstants;
22+
import org.apache.dubbo.common.extension.Activate;
23+
import org.apache.dubbo.common.logger.Logger;
24+
import org.apache.dubbo.common.logger.LoggerFactory;
25+
import org.apache.dubbo.common.utils.ReflectUtils;
26+
import org.apache.dubbo.rpc.Filter;
27+
import org.apache.dubbo.rpc.Invocation;
28+
import org.apache.dubbo.rpc.Invoker;
29+
import org.apache.dubbo.rpc.Result;
30+
import org.apache.dubbo.rpc.RpcContext;
31+
import org.apache.dubbo.rpc.RpcException;
32+
import org.apache.dubbo.rpc.RpcInvocation;
33+
34+
import java.lang.reflect.Field;
35+
import java.util.ArrayList;
36+
import java.util.Arrays;
37+
import java.util.Collection;
38+
import java.util.HashMap;
39+
import java.util.List;
40+
import java.util.Map;
41+
import java.util.stream.Stream;
42+
43+
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.GATEWAY_MODE;
44+
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.ORIGIN_GENERIC_PARAMETER_TYPES;
45+
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.ORIGIN_PARAMETER_TYPES_DESC;
46+
import static org.apache.dubbo.gateway.common.OmnipotentCommonConstants.SPECIFY_ADDRESS;
47+
48+
49+
@Activate(group = CommonConstants.CONSUMER)
50+
public class OmnSerFilter implements Filter, Filter.Listener {
51+
52+
private final static Logger logger = LoggerFactory.getLogger(OmnSerFilter.class);
53+
54+
public static final String name = "specifyAddress";
55+
56+
@Override
57+
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
58+
59+
Object address = invocation.get(SPECIFY_ADDRESS);
60+
if (address != null) {
61+
RpcContext.getClientAttachment().setAttachment(GATEWAY_MODE, "omn");
62+
convertParameterTypeToJavaBeanDescriptor(invocation);
63+
}
64+
return invoker.invoke(invocation);
65+
}
66+
67+
68+
@Override
69+
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
70+
71+
Object resData = appResponse.getValue();
72+
if (resData == null) {
73+
return;
74+
}
75+
76+
if (ReflectUtils.isPrimitives(resData.getClass())) {
77+
return;
78+
}
79+
generalizeJbdParameter(appResponse.getValue());
80+
}
81+
82+
@Override
83+
public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) {
84+
85+
}
86+
87+
88+
@SuppressWarnings({"unchecked", "rawtypes"})
89+
private void generalizeJbdParameter(Object pojo) {
90+
if (pojo instanceof Collection) {
91+
92+
Collection collection = (Collection) pojo;
93+
List list = new ArrayList();
94+
for (Object obj : collection) {
95+
if (obj instanceof JavaBeanDescriptor) {
96+
list.add(JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) obj));
97+
} else {
98+
list.add(obj);
99+
}
100+
}
101+
collection.clear();
102+
collection.addAll(list);
103+
}
104+
105+
if (pojo instanceof Map) {
106+
107+
Map map = (Map) pojo;
108+
Map newMap = new HashMap();
109+
for (Object key : map.keySet()) {
110+
111+
Object value = map.get(key);
112+
if (key instanceof JavaBeanDescriptor) {
113+
key = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) key);
114+
}
115+
if (value instanceof JavaBeanDescriptor) {
116+
value = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) value);
117+
}
118+
newMap.put(key, value);
119+
120+
}
121+
map.clear();
122+
map.putAll(newMap);
123+
}
124+
125+
// public field
126+
for (Field field : pojo.getClass().getDeclaredFields()) {
127+
try {
128+
field.setAccessible(true);
129+
Object fieldValue = field.get(pojo);
130+
if (fieldValue instanceof JavaBeanDescriptor) {
131+
field.set(pojo, JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) fieldValue));
132+
}
133+
} catch (Exception e) {
134+
throw new RuntimeException(e.getMessage(), e);
135+
}
136+
}
137+
}
138+
139+
public static void convertParameterTypeToJavaBeanDescriptor(Invocation invocation) {
140+
if (!(invocation instanceof RpcInvocation)) {
141+
logger.warn("Non-RpcInvocation type, gateway mode does not take effect, type:" + invocation.getClass().getName());
142+
return;
143+
}
144+
Class<?>[] parameterTypes = invocation.getParameterTypes();
145+
boolean reqFirst = Arrays.stream(parameterTypes).noneMatch(param -> param == JavaBeanDescriptor.class);
146+
if (reqFirst) {
147+
invocation.setObjectAttachment(ORIGIN_GENERIC_PARAMETER_TYPES, getDesc(parameterTypes));
148+
invocation.setObjectAttachment(ORIGIN_PARAMETER_TYPES_DESC, ((RpcInvocation) invocation).getParameterTypesDesc());
149+
Arrays.fill(parameterTypes, JavaBeanDescriptor.class);
150+
151+
Object[] arguments = invocation.getArguments();
152+
for (int i = 0; i < arguments.length; i++) {
153+
JavaBeanDescriptor jbdArg = JavaBeanSerializeUtil.serialize(arguments[i]);
154+
arguments[i] = jbdArg;
155+
}
156+
157+
((RpcInvocation) invocation).setParameterTypesDesc(ReflectUtils.getDesc(parameterTypes));
158+
((RpcInvocation) invocation).setCompatibleParamSignatures(Stream.of(parameterTypes).map(Class::getName).toArray(String[]::new));
159+
}
160+
161+
}
162+
163+
private static String[] getDesc(Class<?>[] parameterTypes) {
164+
return Arrays.stream(parameterTypes).map(Class::getName).toArray(String[]::new);
165+
}
166+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
injvm-initial=org.apache.dubbo.gateway.consumer.config.InjvmConfigPostProcessor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
omnSer=org.apache.dubbo.gateway.consumer.filter.OmnSerFilter

0 commit comments

Comments
 (0)