-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance to reuse duplicate spring beans to reduce memory usage. (#12990)
- Loading branch information
1 parent
036adbb
commit 8478fd7
Showing
13 changed files
with
481 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ain/java/com/alibaba/nacos/console/config/NacosConsoleBeanPostProcessorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.console.config; | ||
|
||
import com.alibaba.nacos.core.web.NacosWebBean; | ||
import com.alibaba.nacos.sys.env.Constants; | ||
import com.alibaba.nacos.sys.env.NacosDuplicateConfigurationBeanPostProcessor; | ||
import com.alibaba.nacos.sys.env.NacosDuplicateSpringBeanPostProcessor; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Bean Post Processor Configuration for nacos console. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
@Configuration | ||
@NacosWebBean | ||
@ConditionalOnProperty(value = Constants.NACOS_DUPLICATE_BEAN_ENHANCEMENT_ENABLED, havingValue = "true", matchIfMissing = true) | ||
public class NacosConsoleBeanPostProcessorConfiguration { | ||
|
||
@Bean | ||
public InstantiationAwareBeanPostProcessor nacosDuplicateSpringBeanPostProcessor( | ||
ConfigurableApplicationContext context) { | ||
return new NacosDuplicateSpringBeanPostProcessor(context); | ||
} | ||
|
||
@Bean | ||
public InstantiationAwareBeanPostProcessor nacosDuplicateConfigurationBeanPostProcessor( | ||
ConfigurableApplicationContext context) { | ||
return new NacosDuplicateConfigurationBeanPostProcessor(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
server/src/main/java/com/alibaba/nacos/server/NacosWebBeanPostProcessorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.server; | ||
|
||
import com.alibaba.nacos.core.web.NacosWebBean; | ||
import com.alibaba.nacos.sys.env.Constants; | ||
import com.alibaba.nacos.sys.env.NacosDuplicateConfigurationBeanPostProcessor; | ||
import com.alibaba.nacos.sys.env.NacosDuplicateSpringBeanPostProcessor; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Bean Post Processor Configuration for nacos web server. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
@Configuration | ||
@NacosWebBean | ||
@ConditionalOnProperty(value = Constants.NACOS_DUPLICATE_BEAN_ENHANCEMENT_ENABLED, havingValue = "true", matchIfMissing = true) | ||
public class NacosWebBeanPostProcessorConfiguration { | ||
|
||
@Bean | ||
public InstantiationAwareBeanPostProcessor nacosDuplicateSpringBeanPostProcessor( | ||
ConfigurableApplicationContext context) { | ||
return new NacosDuplicateSpringBeanPostProcessor(context); | ||
} | ||
|
||
@Bean | ||
public InstantiationAwareBeanPostProcessor nacosDuplicateConfigurationBeanPostProcessor( | ||
ConfigurableApplicationContext context) { | ||
return new NacosDuplicateConfigurationBeanPostProcessor(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sys/src/main/java/com/alibaba/nacos/sys/env/AbstractNacosDuplicateBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.sys.env; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
/** | ||
* Abstract Nacos Duplicate Bean Post Processor of {@link InstantiationAwareBeanPostProcessor} to reduce duplicate rebuild bean for spring beans. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public abstract class AbstractNacosDuplicateBeanPostProcessor implements InstantiationAwareBeanPostProcessor { | ||
|
||
private final ConfigurableApplicationContext coreContext; | ||
|
||
protected AbstractNacosDuplicateBeanPostProcessor(ConfigurableApplicationContext context) { | ||
coreContext = null == context.getParent() ? context : (ConfigurableApplicationContext) context.getParent(); | ||
} | ||
|
||
@Override | ||
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { | ||
if (!coreContext.containsBean(beanName)) { | ||
return null; | ||
} | ||
BeanDefinition beanDefinition = coreContext.getBeanFactory().getBeanDefinition(beanName); | ||
return isReUsingBean(beanClass, beanName, beanDefinition) ? coreContext.getBean(beanName) : null; | ||
} | ||
|
||
/** | ||
* Judge whether re-use beans from core context. | ||
* | ||
* @param beanClass bean class | ||
* @param beanName bean name | ||
* @param beanDefinition bean definition | ||
* @return {@code true} means re-use beans from core context, otherwise {@code false} means to re-build bean in sub context. | ||
*/ | ||
protected abstract boolean isReUsingBean(Class<?> beanClass, String beanName, BeanDefinition beanDefinition); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.sys.env; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Nacos {@link InstantiationAwareBeanPostProcessor} to reduce duplicate rebuild bean for spring configuration. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class NacosDuplicateConfigurationBeanPostProcessor extends AbstractNacosDuplicateBeanPostProcessor { | ||
|
||
public NacosDuplicateConfigurationBeanPostProcessor(ConfigurableApplicationContext context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
protected boolean isReUsingBean(Class<?> beanClass, String beanName, BeanDefinition beanDefinition) { | ||
return isConfiguration(beanClass); | ||
} | ||
|
||
private boolean isConfiguration(Class<?> beanClass) { | ||
return null != beanClass.getAnnotation(Configuration.class) || null != beanClass.getAnnotation( | ||
AutoConfiguration.class); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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.alibaba.nacos.sys.env; | ||
|
||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
|
||
/** | ||
* Nacos {@link InstantiationAwareBeanPostProcessor} to reduce duplicate rebuild bean for spring beans. | ||
* | ||
* <p> | ||
* For some important spring beans like spring context beans, if reuse from parent, will cause some problem. So skip. | ||
* </p> | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class NacosDuplicateSpringBeanPostProcessor extends AbstractNacosDuplicateBeanPostProcessor { | ||
|
||
public NacosDuplicateSpringBeanPostProcessor(ConfigurableApplicationContext context) { | ||
super(context); | ||
} | ||
|
||
@Override | ||
protected boolean isReUsingBean(Class<?> beanClass, String beanName, BeanDefinition beanDefinition) { | ||
return !isContextBean(beanClass); | ||
} | ||
|
||
private boolean isContextBean(Class<?> beanClass) { | ||
return isContextClass(beanClass.getCanonicalName()); | ||
} | ||
|
||
private boolean isContextClass(String beanClassName) { | ||
return beanClassName.startsWith("org.springframework.context") | ||
|| beanClassName.startsWith("org.springframework.boot.context"); | ||
} | ||
} |
Oops, something went wrong.