Skip to content

Commit

Permalink
Support exclude fastjson2 or hessian2 serialization dependencies (apa…
Browse files Browse the repository at this point in the history
…che#13441)

* Support exclude fastjson2 or hessian2 serialization dependencies

* Fix style

* Support check

* Fix test cases

* lint

* Fix test cases

* Style
  • Loading branch information
AlbumenJ authored and vio-lin committed Jul 19, 2024
1 parent c5f0310 commit 637b5d8
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +30,6 @@
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
Expand Down Expand Up @@ -264,7 +264,12 @@ protected void checkDefault() {
}

if (StringUtils.isBlank(preferSerialization)) {
preferSerialization = serialization != null ? serialization : DEFAULT_PREFER_SERIALIZATION;
preferSerialization = serialization != null
? serialization
: getScopeModel()
.getBeanFactory()
.getBean(PreferSerializationProvider.class)
.getPreferSerialization();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@
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 org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -64,6 +66,7 @@ public void init() {
ApplicationModel applicationModel = ApplicationModel.defaultModel();
configManager = applicationModel.getApplicationConfigManager();
moduleConfigManager = applicationModel.getDefaultModule().getConfigManager();
FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class);
}

@Test
Expand Down Expand Up @@ -468,4 +471,11 @@ void testLoadConfig() {
Assertions.assertFalse(moduleConfigManager.getConsumers().isEmpty());

}

public static class TestPreferSerializationProvider implements PreferSerializationProvider {
@Override
public String getPreferSerialization() {
return "fastjson2,hessian2";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ProtocolConfigTest {
@BeforeEach
public void setUp() {
DubboBootstrap.reset();
// FrameworkModel.defaultModel().getBeanFactory().registerBean(TestPreferSerializationProvider.class);
}

@AfterEach
Expand Down Expand Up @@ -234,14 +235,14 @@ void testExtension() throws Exception {
void testMetaData() {
ProtocolConfig config = new ProtocolConfig();
Map<String, String> metaData = config.getMetaData();
Assertions.assertEquals(0, metaData.size(), "actual: "+metaData);
Assertions.assertEquals(0, metaData.size(), "actual: " + metaData);
}

@Test
void testOverrideEmptyConfig() {
int port = NetUtils.getAvailablePort();
//dubbo.protocol.name=rest
//dubbo.protocol.port=port
// dubbo.protocol.name=rest
// dubbo.protocol.port=port
SysProps.setProperty("dubbo.protocol.name", "rest");
SysProps.setProperty("dubbo.protocol.port", String.valueOf(port));

Expand Down Expand Up @@ -285,7 +286,7 @@ void testOverrideConfigByName() {
void testOverrideConfigById() {
int port = NetUtils.getAvailablePort();
SysProps.setProperty("dubbo.protocols.rest1.name", "rest");
SysProps.setProperty("dubbo.protocols.rest1.port", String.valueOf(port));
SysProps.setProperty("dubbo.protocols.rest1.port", String.valueOf(port));

try {
ProtocolConfig protocolConfig = new ProtocolConfig();
Expand Down Expand Up @@ -316,8 +317,7 @@ void testCreateConfigFromPropsWithId() {
try {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application("test-app")
.initialize();
bootstrap.application("test-app").initialize();

ConfigManager configManager = bootstrap.getConfigManager();
Collection<ProtocolConfig> protocols = configManager.getProtocols();
Expand All @@ -343,8 +343,7 @@ void testCreateConfigFromPropsWithName() {
try {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application("test-app")
.initialize();
bootstrap.application("test-app").initialize();

ConfigManager configManager = bootstrap.getConfigManager();
Collection<ProtocolConfig> protocols = configManager.getProtocols();
Expand All @@ -370,8 +369,7 @@ void testCreateDefaultConfigFromProps() {
try {

DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application("test-app")
.initialize();
bootstrap.application("test-app").initialize();

ConfigManager configManager = bootstrap.getConfigManager();
Collection<ProtocolConfig> protocols = configManager.getProtocols();
Expand All @@ -387,14 +385,13 @@ void testCreateDefaultConfigFromProps() {
}
}


@Test
void testPreferSerializationDefault1() throws Exception {
ProtocolConfig protocolConfig = new ProtocolConfig();
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");
Expand All @@ -410,7 +407,7 @@ void testPreferSerializationDefault2() throws Exception {
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");
Expand All @@ -419,6 +416,4 @@ void testPreferSerializationDefault2() throws Exception {
protocolConfig.refresh();
assertThat(protocolConfig.getPreferSerialization(), equalTo("x-serialization"));
}


}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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) {}
}
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.
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
serialization-api=org.apache.dubbo.common.serialize.SerializationScopeModelInitializer
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Optional;

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;
Expand All @@ -37,6 +39,19 @@
* </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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Optional;

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;
Expand All @@ -37,6 +39,20 @@
* </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() {
Expand Down

0 comments on commit 637b5d8

Please sign in to comment.