Support exclude fastjson2 or hessian2 serialization dependencies (#13441)

* Support exclude fastjson2 or hessian2 serialization dependencies

* Fix style

* Support check

* Fix test cases

* lint

* Fix test cases

* Style
This commit is contained in:
Albumen Kevin 2023-12-04 21:32:50 +08:00 committed by GitHub
parent b7e675940d
commit 0d879666b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 181 additions and 21 deletions

View File

@ -14,15 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.constants;
package org.apache.dubbo.common.serialization;
/**
* Provider Constants
*/
public interface ProviderConstants {
/**
* Default prefer serialization,multiple separated by commas
*/
String DEFAULT_PREFER_SERIALIZATION = "fastjson2,hessian2";
public interface PreferSerializationProvider {
String getPreferSerialization();
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config;
import org.apache.dubbo.common.serialization.PreferSerializationProvider;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.support.Parameter;
@ -29,7 +30,6 @@ import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL;
import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREAD_POOL_EXHAUSTED_LISTENERS_KEY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION;
import static org.apache.dubbo.common.constants.ProviderConstants.DEFAULT_PREFER_SERIALIZATION;
/**
* ProtocolConfig
@ -262,7 +262,12 @@ public class ProtocolConfig extends AbstractConfig {
}
if (StringUtils.isBlank(preferSerialization)) {
preferSerialization = serialization != null ? serialization : DEFAULT_PREFER_SERIALIZATION;
preferSerialization = serialization != null
? serialization
: getScopeModel()
.getBeanFactory()
.getBean(PreferSerializationProvider.class)
.getPreferSerialization();
}
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config.context;
import org.apache.dubbo.common.serialization.PreferSerializationProvider;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
@ -27,6 +28,7 @@ import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.Collection;
import java.util.Optional;
@ -64,6 +66,7 @@ class ConfigManagerTest {
ApplicationModel applicationModel = ApplicationModel.defaultModel();
configManager = applicationModel.getApplicationConfigManager();
moduleConfigManager = applicationModel.getDefaultModule().getConfigManager();
FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class);
}
@Test
@ -474,4 +477,11 @@ class ConfigManagerTest {
Assertions.assertFalse(moduleConfigManager.getProviders().isEmpty());
Assertions.assertFalse(moduleConfigManager.getConsumers().isEmpty());
}
public static class TestPreferSerializationProvider implements PreferSerializationProvider {
@Override
public String getPreferSerialization() {
return "fastjson2,hessian2";
}
}
}

View File

@ -31,7 +31,6 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.dubbo.common.constants.ProviderConstants.DEFAULT_PREFER_SERIALIZATION;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
@ -43,6 +42,7 @@ class ProtocolConfigTest {
@BeforeEach
public void setUp() {
DubboBootstrap.reset();
// FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class);
}
@AfterEach
@ -389,7 +389,7 @@ class ProtocolConfigTest {
assertNull(protocolConfig.getPreferSerialization());
protocolConfig.checkDefault();
assertThat(protocolConfig.getPreferSerialization(), equalTo(DEFAULT_PREFER_SERIALIZATION));
assertThat(protocolConfig.getPreferSerialization(), equalTo("fastjson2,hessian2"));
protocolConfig = new ProtocolConfig();
protocolConfig.setSerialization("x-serialization");
@ -405,7 +405,7 @@ class ProtocolConfigTest {
assertNull(protocolConfig.getPreferSerialization());
protocolConfig.refresh();
assertThat(protocolConfig.getPreferSerialization(), equalTo(DEFAULT_PREFER_SERIALIZATION));
assertThat(protocolConfig.getPreferSerialization(), equalTo("fastjson2,hessian2"));
protocolConfig = new ProtocolConfig();
protocolConfig.setSerialization("x-serialization");

View File

@ -0,0 +1,26 @@
/*
* 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.config.utils;
import org.apache.dubbo.common.serialization.PreferSerializationProvider;
public class TestPreferSerializationProvider implements PreferSerializationProvider {
@Override
public String getPreferSerialization() {
return "fastjson2,hessian2";
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.common.serialize;
import org.apache.dubbo.common.serialize.support.PreferSerializationProviderImpl;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ScopeModelInitializer;
public class SerializationScopeModelInitializer implements ScopeModelInitializer {
@Override
public void initializeFrameworkModel(FrameworkModel frameworkModel) {
frameworkModel.getBeanFactory().registerBean(PreferSerializationProviderImpl.class);
}
@Override
public void initializeApplicationModel(ApplicationModel applicationModel) {}
@Override
public void initializeModuleModel(ModuleModel moduleModel) {}
}

View File

@ -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.
*/
package org.apache.dubbo.common.serialize.support;
import org.apache.dubbo.common.serialization.PreferSerializationProvider;
import org.apache.dubbo.common.serialize.Serialization;
import org.apache.dubbo.rpc.model.FrameworkModel;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class PreferSerializationProviderImpl implements PreferSerializationProvider {
private final String preferSerialization;
public PreferSerializationProviderImpl(FrameworkModel frameworkModel) {
List<String> defaultSerializations = Arrays.asList("fastjson2", "hessian2");
this.preferSerialization = defaultSerializations.stream()
.filter(s ->
frameworkModel.getExtensionLoader(Serialization.class).hasExtension(s))
.collect(Collectors.joining(","));
}
@Override
public String getPreferSerialization() {
return preferSerialization;
}
}

View File

@ -0,0 +1 @@
serialization-api=org.apache.dubbo.common.serialize.SerializationScopeModelInitializer

View File

@ -17,6 +17,8 @@
package org.apache.dubbo.common.serialize.fastjson2;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.Serialization;
@ -37,6 +39,19 @@ import static org.apache.dubbo.common.serialize.Constants.FASTJSON2_SERIALIZATIO
* </pre>
*/
public class FastJson2Serialization implements Serialization {
private static final Logger logger = LoggerFactory.getLogger(FastJson2Serialization.class);
static {
Class<?> aClass = null;
try {
aClass = com.alibaba.fastjson2.JSONB.class;
} catch (Throwable ignored) {
}
if (aClass == null) {
logger.info("Failed to load com.alibaba.fastjson2.JSONB, fastjson2 serialization will be disabled.");
throw new IllegalStateException("The fastjson2 is not in classpath.");
}
}
@Override
public byte getContentTypeId() {

View File

@ -26,9 +26,17 @@ public class Fastjson2ScopeModelInitializer implements ScopeModelInitializer {
@Override
public void initializeFrameworkModel(FrameworkModel frameworkModel) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Fastjson2CreatorManager.class);
beanFactory.registerBean(Fastjson2SecurityManager.class);
Class<?> aClass = null;
try {
aClass = com.alibaba.fastjson2.JSONB.class;
} catch (Throwable ignored) {
}
if (aClass != null) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Fastjson2CreatorManager.class);
beanFactory.registerBean(Fastjson2SecurityManager.class);
}
}
@Override

View File

@ -25,10 +25,18 @@ import org.apache.dubbo.rpc.model.ScopeModelInitializer;
public class Hessian2ScopeModelInitializer implements ScopeModelInitializer {
@Override
public void initializeFrameworkModel(FrameworkModel frameworkModel) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Hessian2FactoryManager.class);
Class<?> aClass = null;
try {
aClass = com.alibaba.com.caucho.hessian.io.Hessian2Output.class;
} catch (Throwable ignored) {
}
frameworkModel.addClassLoaderListener(new Hessian2ClassLoaderListener());
if (aClass != null) {
ScopeBeanFactory beanFactory = frameworkModel.getBeanFactory();
beanFactory.registerBean(Hessian2FactoryManager.class);
frameworkModel.addClassLoaderListener(new Hessian2ClassLoaderListener());
}
}
@Override

View File

@ -17,6 +17,8 @@
package org.apache.dubbo.common.serialize.hessian2;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.Serialization;
@ -37,6 +39,20 @@ import static org.apache.dubbo.common.serialize.Constants.HESSIAN2_SERIALIZATION
* </pre>
*/
public class Hessian2Serialization implements Serialization {
private static final Logger logger = LoggerFactory.getLogger(Hessian2Serialization.class);
static {
Class<?> aClass = null;
try {
aClass = com.alibaba.com.caucho.hessian.io.Hessian2Output.class;
} catch (Throwable ignored) {
}
if (aClass == null) {
logger.info(
"Failed to load com.alibaba.com.caucho.hessian.io.Hessian2Output, hessian2 serialization will be disabled.");
throw new IllegalStateException("The hessian2 is not in classpath.");
}
}
@Override
public byte getContentTypeId() {