Polish : spring-cloud-incubator/spring-cloud-alibaba#473 : Dubbo Spring Cloud duplicated registration

pull/475/head
mercyblitz 6 years ago
parent af601ca269
commit 4c3ad6b874

@ -16,10 +16,12 @@
*/
package org.springframework.cloud.alibaba.dubbo.autoconfigure;
import feign.Contract;
import org.apache.dubbo.config.ProtocolConfig;
import feign.Contract;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.alibaba.dubbo.metadata.DubboProtocolConfigSupplier;
import org.springframework.cloud.alibaba.dubbo.metadata.repository.DubboServiceMetadataRepository;
import org.springframework.cloud.alibaba.dubbo.metadata.resolver.DubboServiceBeanMetadataResolver;
import org.springframework.cloud.alibaba.dubbo.metadata.resolver.MetadataResolver;
@ -31,9 +33,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import java.util.Collection;
import java.util.Iterator;
import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL;
import java.util.function.Supplier;
/**
* Spring Boot Auto-Configuration class for Dubbo Metadata
@ -44,43 +44,15 @@ import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL;
@Import({DubboServiceMetadataRepository.class, PublishingDubboMetadataConfigService.class})
public class DubboMetadataAutoConfiguration {
public static final String METADATA_PROTOCOL_BEAN_NAME = "metadata";
@Bean
@ConditionalOnMissingBean
public MetadataResolver metadataJsonResolver(ObjectProvider<Contract> contract) {
return new DubboServiceBeanMetadataResolver(contract);
}
/**
* Build an alias Bean for {@link ProtocolConfig}
*
* @param protocols {@link ProtocolConfig} Beans
* @return {@link ProtocolConfig} bean
*/
@Bean(name = METADATA_PROTOCOL_BEAN_NAME)
public ProtocolConfig protocolConfig(Collection<ProtocolConfig> protocols) {
ProtocolConfig protocolConfig = null;
for (ProtocolConfig protocol : protocols) {
String protocolName = protocol.getName();
if (DEFAULT_PROTOCOL.equals(protocolName)) {
protocolConfig = protocol;
break;
}
}
if (protocolConfig == null) { // If The ProtocolConfig bean named "dubbo" is absent, take first one of them
Iterator<ProtocolConfig> iterator = protocols.iterator();
protocolConfig = iterator.hasNext() ? iterator.next() : null;
}
if (protocolConfig == null) {
protocolConfig = new ProtocolConfig();
protocolConfig.setName(DEFAULT_PROTOCOL);
protocolConfig.setPort(20880);
}
return protocolConfig;
@Bean
public Supplier<ProtocolConfig> dubboProtocolConfigSupplier(Collection<ProtocolConfig> protocols) {
return new DubboProtocolConfigSupplier(protocols);
}
@Bean

@ -22,10 +22,10 @@ import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -49,8 +49,7 @@ import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.List;
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboMetadataAutoConfiguration.METADATA_PROTOCOL_BEAN_NAME;
import java.util.function.Supplier;
/**
* The Auto-Configuration class for Dubbo metadata {@link EventListener event handling}.
@ -73,8 +72,7 @@ public class DubboMetadataEventHandlingAutoConfiguration {
private ApplicationConfig applicationConfig;
@Autowired
@Qualifier(METADATA_PROTOCOL_BEAN_NAME)
private ProtocolConfig metadataProtocolConfig;
private Supplier<ProtocolConfig> protocolConfigSupplier;
@Autowired
private ConfigurableApplicationContext context;
@ -190,7 +188,7 @@ public class DubboMetadataEventHandlingAutoConfiguration {
serviceConfig.setVersion(currentApplicationName);
serviceConfig.setRef(dubboMetadataConfigService);
serviceConfig.setApplication(applicationConfig);
serviceConfig.setProtocol(metadataProtocolConfig);
serviceConfig.setProtocol(protocolConfigSupplier.get());
serviceConfig.export();

@ -0,0 +1,64 @@
/*
* 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.springframework.cloud.alibaba.dubbo.metadata;
import org.apache.dubbo.config.ProtocolConfig;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Supplier;
import static org.apache.dubbo.common.Constants.DEFAULT_PROTOCOL;
/**
* Dubbo's {@link ProtocolConfig} {@link Supplier}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class DubboProtocolConfigSupplier implements Supplier<ProtocolConfig> {
private final Collection<ProtocolConfig> protocols;
public DubboProtocolConfigSupplier(Collection<ProtocolConfig> protocols) {
this.protocols = protocols;
}
@Override
public ProtocolConfig get() {
ProtocolConfig protocolConfig = null;
for (ProtocolConfig protocol : protocols) {
String protocolName = protocol.getName();
if (DEFAULT_PROTOCOL.equals(protocolName)) {
protocolConfig = protocol;
break;
}
}
if (protocolConfig == null) { // If The ProtocolConfig bean named "dubbo" is absent, take first one of them
Iterator<ProtocolConfig> iterator = protocols.iterator();
protocolConfig = iterator.hasNext() ? iterator.next() : null;
}
if (protocolConfig == null) {
protocolConfig = new ProtocolConfig();
protocolConfig.setName(DEFAULT_PROTOCOL);
protocolConfig.setPort(20880);
}
return protocolConfig;
}
}
Loading…
Cancel
Save