mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish : spring-cloud-incubator/spring-cloud-alibaba#377 : DubboMetadataConfigService will not be export for the pure Dubbo consumer side.
This commit is contained in:
parent
892216ffd2
commit
fc14f658a7
@ -16,18 +16,29 @@
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.autoconfigure;
|
||||
|
||||
import org.apache.dubbo.config.ApplicationConfig;
|
||||
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.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.alibaba.dubbo.metadata.resolver.MetadataResolver;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.DubboMetadataConfigService;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.PublishingDubboMetadataConfigService;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboMetadataAutoConfiguration.METADATA_PROTOCOL_BEAN_NAME;
|
||||
|
||||
/**
|
||||
* The Auto-Configuration class for Dubbo REST metadata registration,
|
||||
@ -44,12 +55,21 @@ import org.springframework.context.event.EventListener;
|
||||
@Configuration
|
||||
public class DubboRestMetadataRegistrationAutoConfiguration {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private MetadataResolver metadataResolver;
|
||||
|
||||
@Autowired
|
||||
private PublishingDubboMetadataConfigService dubboMetadataConfigService;
|
||||
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
|
||||
@Autowired
|
||||
@Qualifier(METADATA_PROTOCOL_BEAN_NAME)
|
||||
private ProtocolConfig metadataProtocolConfig;
|
||||
|
||||
@Value("${spring.application.name:application}")
|
||||
private String currentApplicationName;
|
||||
|
||||
@ -58,4 +78,32 @@ public class DubboRestMetadataRegistrationAutoConfiguration {
|
||||
ServiceBean serviceBean = event.getServiceBean();
|
||||
dubboMetadataConfigService.publishServiceRestMetadata(metadataResolver.resolveServiceRestMetadata(serviceBean));
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void onApplicationReady() {
|
||||
exportDubboMetadataConfigService();
|
||||
}
|
||||
|
||||
private void exportDubboMetadataConfigService() {
|
||||
|
||||
if (StringUtils.isEmpty(dubboMetadataConfigService.getServiceRestMetadata())) {
|
||||
// If there is no REST metadata, DubboMetadataConfigService will not be exported.
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("There is no REST metadata, the Dubbo service[{}] will not be exported.",
|
||||
dubboMetadataConfigService.getClass().getName());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ServiceConfig<DubboMetadataConfigService> serviceConfig = new ServiceConfig<>();
|
||||
|
||||
serviceConfig.setInterface(DubboMetadataConfigService.class);
|
||||
// Use current Spring application name as the Dubbo Service version
|
||||
serviceConfig.setVersion(currentApplicationName);
|
||||
serviceConfig.setRef(dubboMetadataConfigService);
|
||||
serviceConfig.setApplication(applicationConfig);
|
||||
serviceConfig.setProtocol(metadataProtocolConfig);
|
||||
|
||||
serviceConfig.export();
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.springframework.cloud.alibaba.dubbo.autoconfigure.DubboMetadataAutoConfiguration.METADATA_PROTOCOL_BEAN_NAME;
|
||||
import static org.springframework.util.ObjectUtils.isEmpty;
|
||||
|
||||
/**
|
||||
* Publishing {@link DubboMetadataConfigService} implementation
|
||||
@ -68,7 +69,9 @@ public class PublishingDubboMetadataConfigService implements DubboMetadataConfig
|
||||
public String getServiceRestMetadata() {
|
||||
String serviceRestMetadataJsonConfig = null;
|
||||
try {
|
||||
if (!isEmpty(serviceRestMetadata)) {
|
||||
serviceRestMetadataJsonConfig = objectMapper.writeValueAsString(serviceRestMetadata);
|
||||
}
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user