spring delegate to bootstrap.

This commit is contained in:
ken.lj 2019-08-09 15:31:07 +08:00
parent 48c1cc4bfe
commit bdaacbc17d
7 changed files with 192 additions and 6 deletions

View File

@ -30,6 +30,9 @@ import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.ModuleConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ReferenceConfig;
@ -45,6 +48,7 @@ import org.apache.dubbo.config.builders.RegistryBuilder;
import org.apache.dubbo.config.builders.ServiceBuilder;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.metadata.ConfigurableMetadataServiceExporter;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.event.EventDispatcher;
import org.apache.dubbo.event.EventListener;
import org.apache.dubbo.metadata.WritableMetadataService;
@ -117,6 +121,8 @@ public class DubboBootstrap {
private final ConfigManager configManager = getInstance();
private ReferenceConfigCache cache;
private volatile boolean initialized = false;
private volatile boolean started = false;
@ -148,7 +154,11 @@ public class DubboBootstrap {
return this;
}
public DubboBootstrap metadataReport(List<MetadataReportConfig> metadataReportConfigs) {
public DubboBootstrap metadataReports(List<MetadataReportConfig> metadataReportConfigs) {
if (CollectionUtils.isEmpty(metadataReportConfigs)) {
return this;
}
configManager.addMetadataReports(metadataReportConfigs);
return this;
}
@ -235,7 +245,10 @@ public class DubboBootstrap {
* @param registryConfigs the multiple instances of {@link RegistryConfig}
* @return current {@link DubboBootstrap} instance
*/
public DubboBootstrap registries(Iterable<RegistryConfig> registryConfigs) {
public DubboBootstrap registries(List<RegistryConfig> registryConfigs) {
if (CollectionUtils.isEmpty(registryConfigs)) {
return this;
}
registryConfigs.forEach(this::registry);
return this;
}
@ -257,6 +270,9 @@ public class DubboBootstrap {
}
public DubboBootstrap protocols(List<ProtocolConfig> protocolConfigs) {
if (CollectionUtils.isEmpty(protocolConfigs)) {
return this;
}
configManager.addProtocols(protocolConfigs);
return this;
}
@ -277,6 +293,14 @@ public class DubboBootstrap {
return this;
}
public DubboBootstrap services(List<ServiceConfig> serviceConfigs) {
if (CollectionUtils.isEmpty(serviceConfigs)) {
return this;
}
serviceConfigs.forEach(configManager::addService);
return this;
}
// {@link Reference} correlative methods
public <S> DubboBootstrap reference(Consumer<ReferenceBuilder<S>> consumerBuilder) {
return reference(DEFAULT_REFERENCE_ID, consumerBuilder);
@ -293,6 +317,15 @@ public class DubboBootstrap {
return this;
}
public DubboBootstrap references(List<ReferenceConfig> referenceConfigs) {
if (CollectionUtils.isEmpty(referenceConfigs)) {
return this;
}
referenceConfigs.forEach(configManager::addReference);
return this;
}
// {@link ProviderConfig} correlative methods
public DubboBootstrap provider(Consumer<ProviderBuilder> builderConsumer) {
return provider(DEFAULT_PROVIDER_ID, builderConsumer);
@ -309,6 +342,10 @@ public class DubboBootstrap {
}
public DubboBootstrap providers(List<ProviderConfig> providerConfigs) {
if (CollectionUtils.isEmpty(providerConfigs)) {
return this;
}
providerConfigs.forEach(configManager::addProvider);
return this;
}
@ -329,20 +366,47 @@ public class DubboBootstrap {
}
public DubboBootstrap consumers(List<ConsumerConfig> consumerConfigs) {
if (CollectionUtils.isEmpty(consumerConfigs)) {
return this;
}
consumerConfigs.forEach(configManager::addConsumer);
return this;
}
// {@link ConfigCenterConfig} correlative methods
public DubboBootstrap configCenter(ConfigCenterConfig configCenterConfig) {
return configCenter(asList(configCenterConfig));
return configCenters(asList(configCenterConfig));
}
public DubboBootstrap configCenter(List<ConfigCenterConfig> configCenterConfigs) {
public DubboBootstrap configCenters(List<ConfigCenterConfig> configCenterConfigs) {
if (CollectionUtils.isEmpty(configCenterConfigs)) {
return this;
}
configManager.addConfigCenters(configCenterConfigs);
return this;
}
public DubboBootstrap monitor(MonitorConfig monitor) {
configManager.setMonitor(monitor);
return this;
}
public DubboBootstrap metrics(MetricsConfig metrics) {
configManager.setMetrics(metrics);
return this;
}
public DubboBootstrap module(ModuleConfig module) {
configManager.setModule(module);
return this;
}
public DubboBootstrap cache(ReferenceConfigCache cache) {
this.cache = cache;
return this;
}
/**
* Initialize
*/
@ -471,6 +535,8 @@ public class DubboBootstrap {
registerServiceInstance(applicationConfig);
}
referServices();
started = true;
if (logger.isInfoEnabled()) {
@ -660,6 +726,13 @@ public class DubboBootstrap {
serviceConfig.export();
}
private void referServices() {
if (cache == null) {
cache = ReferenceConfigCache.getCache();
}
configManager.getReferenceConfigs().forEach(cache::get);
}
public boolean isOnlyRegisterProvider() {
return onlyRegisterProvider;
}
@ -693,7 +766,7 @@ public class DubboBootstrap {
}
/**
* If use rest protocol if there's one, otherwise, choose the first one available.
* Use rest protocol if there's one, otherwise, choose the first one available.
*
* @return
*/

View File

@ -634,6 +634,7 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
private void createApplicationIfAbsent() {
if (this.application != null) {
this.application.refresh();
return;
}
ConfigManager configManager = ConfigManager.getInstance();

View File

@ -24,6 +24,7 @@ import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.ModuleConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
@ -96,6 +97,7 @@ public class ConfigManager {
private volatile ModuleConfig module;
private volatile ApplicationConfig application;
private volatile MonitorConfig monitor;
private volatile MetricsConfig metrics;
private final Map<String, ProtocolConfig> protocols = new ConcurrentHashMap<>();
private final Map<String, RegistryConfig> registries = new ConcurrentHashMap<>();
@ -154,6 +156,17 @@ public class ConfigManager {
return ofNullable(module);
}
public void setMetrics(MetricsConfig metrics) {
if (metrics != null) {
checkDuplicate(this.metrics, metrics);
this.metrics = metrics;
}
}
public Optional<MetricsConfig> getMetrics() {
return ofNullable(metrics);
}
// ConfigCenterConfig correlative methods
public void addConfigCenter(ConfigCenterConfig configCenter) {

View File

@ -35,6 +35,11 @@
<artifactId>dubbo-config-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bootstrap</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>

View File

@ -0,0 +1,89 @@
/*
* 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.spring;
import org.apache.dubbo.bootstrap.DubboBootstrap;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.ModuleConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.spring.util.BeanFactoryUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import java.util.List;
public class ApplicationBean extends ApplicationConfig implements ApplicationListener<ApplicationEvent>, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
List<RegistryConfig> registries = getBeans(RegistryConfig.class);
List<ProtocolConfig> protocols = getBeans(ProtocolConfig.class);
List<ConfigCenterConfig> configs = getBeans(ConfigCenterConfig.class);
List<MetadataReportConfig> metadatas = getBeans(MetadataReportConfig.class);
List<MonitorConfig> monitors = getBeans(MonitorConfig.class);
List<ProviderConfig> providers = getBeans(ProviderConfig.class);
List<ConsumerConfig> consumers = getBeans(ConsumerConfig.class);
List<ModuleConfig> modules = getBeans(ModuleConfig.class);
List<MetricsConfig> metrics = getBeans(MetricsConfig.class);
List<ServiceConfig> services = getBeans(ServiceConfig.class);
List<ReferenceConfig> references = getBeans(ReferenceConfig.class);
DubboBootstrap bootstrap = new DubboBootstrap();
bootstrap.application(this)
.monitor(CollectionUtils.isNotEmpty(monitors) ? monitors.get(0) : null)
.module(CollectionUtils.isNotEmpty(modules) ? modules.get(0) : null)
.metrics(CollectionUtils.isNotEmpty(metrics) ? metrics.get(0) : null)
.registries(registries)
.protocols(protocols)
.configCenters(configs)
.metadataReports(metadatas)
.providers(providers)
.consumers(consumers)
.services(services)
.references(references)
.start();
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
private <T> List<T> getBeans(Class<T> clazz) {
return BeanFactoryUtils.getBeans(applicationContext, new String[]{""}, clazz);
}
}

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
import org.apache.dubbo.config.support.Parameter;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.DisposableBean;
@ -53,7 +54,7 @@ public class ReferenceBean<T> extends ReferenceConfig<T> implements FactoryBean,
@Override
public Object getObject() {
return get();
return ReferenceConfigCache.getCache().get(this);
}
@Override

View File

@ -50,6 +50,10 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>