Skip to content

Commit

Permalink
Custom serializer register (#1296)
Browse files Browse the repository at this point in the history
* feat: use extension on serializer register
---------

Co-authored-by: 均源 <zhangminglun.zml@antgroup.com>
Co-authored-by: junyuan <zhangminglun.zml@ant-group.com>
  • Loading branch information
3 people authored Apr 12, 2023
1 parent a9af9cd commit 5a0c0f7
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,10 @@ public class RpcOptions {
*/
public static final String RPC_UNIQUEID_PATTERN_CHECK = "sofa.rpc.uniqueId.pattern.check";

/**
* bolt serializer register extension
* @since 5.10.0
*/
public static final String BOLT_SERIALIZER_REGISTER_EXTENSION = "sofa.rpc.bolt.serializer.register.extension";

}
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,7 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支
//是否禁止开启lookout采集信息
"connection.validate.sleep": false,
//是否关闭uniqueId 特殊字符的校验
"sofa.rpc.uniqueId.pattern.check": true
}
"sofa.rpc.uniqueId.pattern.check": true,
// bolt serializer 的注册器
"sofa.rpc.bolt.serializer.register.extension": "sofaRpcSerializationRegister"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 com.alipay.sofa.rpc.codec.bolt;

import com.alipay.sofa.rpc.ext.Extensible;

/**
*
* @author junyuan
* @version AbstractSerializationRegister.java, v 0.1 2022年12月23日 15:01 junyuan Exp $
*/
@Extensible
public abstract class AbstractSerializationRegister {

public abstract void doRegisterCustomSerializer();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.remoting.CustomSerializerManager;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.ext.Extension;

import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -27,13 +28,15 @@
*
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class SofaRpcSerializationRegister {
@Extension(value = "sofaRpcSerializationRegister")
public class SofaRpcSerializationRegister extends AbstractSerializationRegister {

private static final SofaRpcSerialization RPC_SERIALIZATION = new SofaRpcSerialization();
private final SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();

private static volatile AtomicBoolean registered = new AtomicBoolean(false);
private volatile AtomicBoolean registered = new AtomicBoolean(false);

public static void registerCustomSerializer() {
@Override
public void doRegisterCustomSerializer() {
if (registered.compareAndSet(false, true)) {
innerRegisterCustomSerializer();
}
Expand All @@ -42,15 +45,15 @@ public static void registerCustomSerializer() {
/**
* we can override or rewrite the method
*/
protected static void innerRegisterCustomSerializer() {
protected void innerRegisterCustomSerializer() {
// 注册序列化器到bolt
if (CustomSerializerManager.getCustomSerializer(SofaRequest.class.getName()) == null) {
CustomSerializerManager.registerCustomSerializer(SofaRequest.class.getName(),
RPC_SERIALIZATION);
sofaRpcSerialization);
}
if (CustomSerializerManager.getCustomSerializer(SofaResponse.class.getName()) == null) {
CustomSerializerManager.registerCustomSerializer(SofaResponse.class.getName(),
RPC_SERIALIZATION);
sofaRpcSerialization);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import com.alipay.remoting.InvokeContext;
import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
import com.alipay.remoting.rpc.protocol.UserProcessor;
import com.alipay.sofa.rpc.codec.bolt.SofaRpcSerializationRegister;
import com.alipay.sofa.rpc.codec.bolt.AbstractSerializationRegister;
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.common.RpcConfigs;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.RpcOptions;
import com.alipay.sofa.rpc.common.SystemInfo;
import com.alipay.sofa.rpc.common.cache.ReflectCache;
import com.alipay.sofa.rpc.common.utils.CommonUtils;
Expand All @@ -40,6 +42,7 @@
import com.alipay.sofa.rpc.event.ServerEndHandleEvent;
import com.alipay.sofa.rpc.event.ServerReceiveEvent;
import com.alipay.sofa.rpc.event.ServerSendEvent;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.invoke.Invoker;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
Expand Down Expand Up @@ -69,7 +72,9 @@ public class BoltServerProcessor extends AsyncUserProcessor<SofaRequest> {
* 提前注册序列化器
*/
static {
SofaRpcSerializationRegister.registerCustomSerializer();
String extensionAlias = RpcConfigs.getStringValue(RpcOptions.BOLT_SERIALIZER_REGISTER_EXTENSION);
ExtensionLoaderFactory.getExtensionLoader(AbstractSerializationRegister.class)
.getExtension(extensionAlias).doRegisterCustomSerializer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.alipay.remoting.rpc.exception.InvokeServerException;
import com.alipay.remoting.rpc.exception.InvokeTimeoutException;
import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.codec.bolt.SofaRpcSerializationRegister;
import com.alipay.sofa.rpc.codec.bolt.AbstractSerializationRegister;
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.common.RpcConfigs;
import com.alipay.sofa.rpc.common.RpcConstants;
Expand All @@ -50,6 +50,7 @@
import com.alipay.sofa.rpc.event.ClientSyncReceiveEvent;
import com.alipay.sofa.rpc.event.EventBus;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
Expand Down Expand Up @@ -96,7 +97,10 @@ public class BoltClientTransport extends ClientTransport {

static {
RPC_CLIENT.init();
SofaRpcSerializationRegister.registerCustomSerializer();

String extensionAlias = RpcConfigs.getStringValue(RpcOptions.BOLT_SERIALIZER_REGISTER_EXTENSION);
ExtensionLoaderFactory.getExtensionLoader(AbstractSerializationRegister.class)
.getExtension(extensionAlias).doRegisterCustomSerializer();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sofaRpcSerializationRegister=com.alipay.sofa.rpc.codec.bolt.SofaRpcSerializationRegister
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 com.alipay.sofa.rpc.codec.bolt;

import com.alipay.remoting.CustomSerializerManager;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.test.TestSofaRpcSerializationRegister;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

/**
*
* @author junyuan
* @version BoltSerializationInitTest.java, v 0.1 2022年12月23日 16:26 junyuan Exp $
*/
public class BoltSerializationInitTest {

/**
* add a serializer to override existing serializer
*/
@Test
public void testSerializerRegisterOverride() {
AbstractSerializationRegister abstractSerializationRegister = ExtensionLoaderFactory.getExtensionLoader(
AbstractSerializationRegister.class).getExtension("sofaRpcSerializationRegister");
abstractSerializationRegister.doRegisterCustomSerializer();

Assert.assertNull("testRegister未能覆盖原版register",
CustomSerializerManager.getCustomSerializer(SofaResponse.class.getName()));

Assert.assertNotNull("testRegister未能覆盖原版register", CustomSerializerManager.getCustomSerializer(
TestSofaRpcSerializationRegister.class.getName()));
}

@After
public void clearClassSerializerMap() {
CustomSerializerManager.clear();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 com.alipay.sofa.rpc.test;

import com.alipay.remoting.CustomSerializerManager;
import com.alipay.sofa.rpc.codec.bolt.AbstractSerializationRegister;
import com.alipay.sofa.rpc.codec.bolt.SofaRpcSerialization;
import com.alipay.sofa.rpc.codec.bolt.SofaRpcSerializationRegister;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.ext.Extension;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* Register custom serializer to bolt.
*
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
@Extension(value = "sofaRpcSerializationRegister", override = true, order = 20)
public class TestSofaRpcSerializationRegister extends SofaRpcSerializationRegister {

private final SofaRpcSerialization rpcSerialization = new SofaRpcSerialization();

/**
* we can override or rewrite the method
*/
@Override
protected void innerRegisterCustomSerializer() {
// 注册序列化器到bolt
if (CustomSerializerManager.getCustomSerializer(SofaRequest.class.getName()) == null) {
CustomSerializerManager.registerCustomSerializer(SofaRequest.class.getName(),
rpcSerialization);
}

if (CustomSerializerManager.getCustomSerializer(TestSofaRpcSerializationRegister.class.getName()) == null) {
CustomSerializerManager.registerCustomSerializer(TestSofaRpcSerializationRegister.class.getName(),
rpcSerialization);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sofaRpcSerializationRegister=com.alipay.sofa.rpc.test.TestSofaRpcSerializationRegister

0 comments on commit 5a0c0f7

Please sign in to comment.