1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

use starting status in eureka when first registry

This commit is contained in:
theonefx 2021-01-11 22:54:10 +08:00
parent fd8a6a6f6c
commit 6c0edee13e
6 changed files with 221 additions and 28 deletions

View File

@ -82,7 +82,7 @@ public final class NacosDataParserHandler {
} }
NacosByteArrayResource nacosByteArrayResource = new NacosByteArrayResource( NacosByteArrayResource nacosByteArrayResource = new NacosByteArrayResource(
configValue.getBytes(), configName); configValue.getBytes(), configName);
nacosByteArrayResource.setFilename(getFileName(configName , extension)); nacosByteArrayResource.setFilename(getFileName(configName, extension));
List<PropertySource<?>> propertySourceList = propertySourceLoader List<PropertySource<?>> propertySourceList = propertySourceLoader
.load(configName, nacosByteArrayResource); .load(configName, nacosByteArrayResource);
if (CollectionUtils.isEmpty(propertySourceList)) { if (CollectionUtils.isEmpty(propertySourceList)) {
@ -135,8 +135,8 @@ public final class NacosDataParserHandler {
return DEFAULT_EXTENSION; return DEFAULT_EXTENSION;
} }
private String getFileName(String name,String extension){ private String getFileName(String name, String extension) {
if(StringUtils.isEmpty(extension)){ if (StringUtils.isEmpty(extension)) {
return name; return name;
} }
if (StringUtils.isEmpty(name)) { if (StringUtils.isEmpty(name)) {
@ -145,7 +145,7 @@ public final class NacosDataParserHandler {
int idx = name.lastIndexOf(DOT); int idx = name.lastIndexOf(DOT);
if (idx > 0 && idx < name.length() - 1) { if (idx > 0 && idx < name.length() - 1) {
String ext = name.substring(idx + 1); String ext = name.substring(idx + 1);
if(extension.equalsIgnoreCase(ext)){ if (extension.equalsIgnoreCase(ext)) {
return name; return name;
} }
} }

View File

@ -40,11 +40,11 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
* @author <a href="mailto:78552423@qq.com">eshun</a> * @author <a href="mailto:78552423@qq.com">eshun</a>
*/ */
public class NacosServiceRegistry implements ServiceRegistry<Registration> { public class NacosServiceRegistry implements ServiceRegistry<Registration> {
private static final String STATUS_UP = "UP"; private static final String STATUS_UP = "UP";
private static final String STATUS_DOWN = "DOWN"; private static final String STATUS_DOWN = "DOWN";
private static final Logger log = LoggerFactory.getLogger(NacosServiceRegistry.class); private static final Logger log = LoggerFactory.getLogger(NacosServiceRegistry.class);
private final NacosDiscoveryProperties nacosDiscoveryProperties; private final NacosDiscoveryProperties nacosDiscoveryProperties;
@ -123,7 +123,8 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
@Override @Override
public void setStatus(Registration registration, String status) { public void setStatus(Registration registration, String status) {
if (!STATUS_UP.equalsIgnoreCase(status) && !STATUS_DOWN.equalsIgnoreCase(status)) { if (!STATUS_UP.equalsIgnoreCase(status)
&& !STATUS_DOWN.equalsIgnoreCase(status)) {
log.warn("can't support status {},please choose UP or DOWN", status); log.warn("can't support status {},please choose UP or DOWN", status);
return; return;
} }
@ -141,8 +142,8 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
try { try {
Properties nacosProperties = nacosDiscoveryProperties.getNacosProperties(); Properties nacosProperties = nacosDiscoveryProperties.getNacosProperties();
nacosServiceManager.getNamingMaintainService(nacosProperties) nacosServiceManager.getNamingMaintainService(nacosProperties).updateInstance(
.updateInstance(serviceId, nacosDiscoveryProperties.getGroup(), instance); serviceId, nacosDiscoveryProperties.getGroup(), instance);
} }
catch (Exception e) { catch (Exception e) {
throw new RuntimeException("update nacos instance status fail", e); throw new RuntimeException("update nacos instance status fail", e);

View File

@ -24,6 +24,8 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.cloud.dubbo.autoconfigure.condition.MissingSpringCloudRegistryConfigPropertyCondition; import com.alibaba.cloud.dubbo.autoconfigure.condition.MissingSpringCloudRegistryConfigPropertyCondition;
import com.alibaba.cloud.dubbo.metadata.DubboBootstrapCommandLineRunner;
import com.alibaba.cloud.dubbo.metadata.event.DubboBootstrapStartedEvent;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository; import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.registry.DubboServiceRegistrationEventPublishingAspect; import com.alibaba.cloud.dubbo.registry.DubboServiceRegistrationEventPublishingAspect;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreDeregisteredEvent; import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreDeregisteredEvent;
@ -31,6 +33,7 @@ import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.ecwid.consul.v1.agent.model.NewService; import com.ecwid.consul.v1.agent.model.NewService;
import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.InstanceInfo;
import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.ServiceBean;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -47,6 +50,7 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration; import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry; import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry;
@ -55,9 +59,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME; import static com.alibaba.cloud.dubbo.autoconfigure.DubboServiceRegistrationAutoConfiguration.CONSUL_AUTO_SERVICE_AUTO_CONFIGURATION_CLASS_NAME;
@ -72,7 +74,8 @@ import static org.springframework.util.ObjectUtils.isEmpty;
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@Import({ DubboServiceRegistrationEventPublishingAspect.class }) @Import({ DubboServiceRegistrationEventPublishingAspect.class,
DubboBootstrapCommandLineRunner.class })
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true) matchIfMissing = true)
@AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME, @AutoConfigureAfter(name = { EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME,
@ -116,8 +119,11 @@ public class DubboServiceRegistrationAutoConfiguration {
private Map<ServiceRegistry<Registration>, Set<Registration>> registrations = new ConcurrentHashMap<>(); private Map<ServiceRegistry<Registration>, Set<Registration>> registrations = new ConcurrentHashMap<>();
@Order @Order
@EventListener(ApplicationContextEvent.class) @EventListener(DubboBootstrapStartedEvent.class)
public void attachDubboMetadataAndRegistAgain(ApplicationContextEvent event) { public void attachDubboMetadataAndRegistAgain(DubboBootstrapStartedEvent event) {
if (!event.getSource().isReady() || !event.getSource().isStarted()) {
return;
}
registrations.forEach( registrations.forEach(
(registry, registrations) -> registrations.forEach(registration -> { (registry, registrations) -> registrations.forEach(registration -> {
attachDubboMetadataServiceMetadata(registration); attachDubboMetadataServiceMetadata(registration);
@ -128,11 +134,18 @@ public class DubboServiceRegistrationAutoConfiguration {
@EventListener(ServiceInstancePreRegisteredEvent.class) @EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) { public void onServiceInstancePreRegistered(ServiceInstancePreRegisteredEvent event) {
Registration registration = event.getSource(); Registration registration = event.getSource();
ServiceRegistry<Registration> registry = event.getRegistry(); if (!DubboBootstrap.getInstance().isReady()
synchronized (registry) { || !DubboBootstrap.getInstance().isStarted()) {
registrations.putIfAbsent(registry, new HashSet<>()); ServiceRegistry<Registration> registry = event.getRegistry();
registrations.get(registry).add(registration); synchronized (registry) {
registrations.putIfAbsent(registry, new HashSet<>());
registrations.get(registry).add(registration);
}
} }
else {
attachDubboMetadataServiceMetadata(registration);
}
} }
@EventListener(ServiceInstancePreDeregisteredEvent.class) @EventListener(ServiceInstancePreDeregisteredEvent.class)
@ -167,14 +180,62 @@ public class DubboServiceRegistrationAutoConfiguration {
@Autowired @Autowired
private ObjectProvider<Collection<ServiceBean>> serviceBeans; private ObjectProvider<Collection<ServiceBean>> serviceBeans;
@Order(Ordered.LOWEST_PRECEDENCE - 1) @EventListener(DubboBootstrapStartedEvent.class)
@EventListener(ApplicationContextEvent.class) public void onServiceInstancePreRegistered(DubboBootstrapStartedEvent event) {
public void onServiceInstancePreRegistered(ApplicationContextEvent event) { DubboBootstrap bootstrap = event.getSource();
registrations.forEach((registry, registrations)-> registrations.forEach(registration -> { if (!bootstrap.isReady() || !bootstrap.isStarted()) {
return;
}
registrations.forEach(
(registry, registrations) -> registrations.removeIf(registration -> {
if (!(registration instanceof EurekaRegistration)) {
return false;
}
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
InstanceInfo instanceInfo = eurekaRegistration
.getApplicationInfoManager().getInfo();
EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration
.getInstanceConfig();
config.setInitialStatus(InstanceInfo.InstanceStatus.UP);
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
eurekaRegistration.getApplicationInfoManager()
.registerAppMetadata(instanceInfo.getMetadata());
eurekaRegistration.getApplicationInfoManager()
.setInstanceStatus(InstanceInfo.InstanceStatus.UP);
return true;
}));
}
@EventListener(ServiceInstancePreRegisteredEvent.class)
public void onServiceInstancePreRegistered(
ServiceInstancePreRegisteredEvent event) {
Registration registration = event.getSource();
if (!(registration instanceof EurekaRegistration)) {
return;
}
if (DubboBootstrap.getInstance().isReady()
&& DubboBootstrap.getInstance().isStarted()) {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration; EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager().getInfo(); InstanceInfo instanceInfo = eurekaRegistration.getApplicationInfoManager()
.getInfo();
EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration
.getInstanceConfig();
config.setInitialStatus(InstanceInfo.InstanceStatus.UP);
attachDubboMetadataServiceMetadata(instanceInfo.getMetadata()); attachDubboMetadataServiceMetadata(instanceInfo.getMetadata());
})); eurekaRegistration.getApplicationInfoManager()
.registerAppMetadata(instanceInfo.getMetadata());
}
else {
EurekaRegistration eurekaRegistration = (EurekaRegistration) registration;
EurekaInstanceConfigBean config = (EurekaInstanceConfigBean) eurekaRegistration
.getInstanceConfig();
config.setInitialStatus(InstanceInfo.InstanceStatus.STARTING);
}
} }
/** /**
@ -199,9 +260,12 @@ public class DubboServiceRegistrationAutoConfiguration {
@AutoConfigureOrder @AutoConfigureOrder
class ConsulConfiguration { class ConsulConfiguration {
@Order(Ordered.LOWEST_PRECEDENCE - 1) @EventListener(DubboBootstrapStartedEvent.class)
@EventListener(ApplicationContextEvent.class) public void attachURLsIntoMetadataBeforeReRegist(
public void attachURLsIntoMetadataBeforeReRegist(ApplicationContextEvent event) { DubboBootstrapStartedEvent event) {
if (!event.getSource().isReady() || !event.getSource().isStarted()) {
return;
}
registrations.entrySet().removeIf(entry -> { registrations.entrySet().removeIf(entry -> {
Set<Registration> registrations = entry.getValue(); Set<Registration> registrations = entry.getValue();
registrations.removeIf(registration -> { registrations.removeIf(registration -> {

View File

@ -0,0 +1,51 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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
*
* https://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.cloud.dubbo.metadata;
import com.alibaba.cloud.dubbo.metadata.event.DubboBootstrapStartedEvent;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
/**
* @see com.alibaba.cloud.dubbo.metadata.event.DubboBootstrapPreStartEvent
* @see com.alibaba.cloud.dubbo.metadata.event.DubboBootstrapStartedEvent
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
@Component
public class DubboBootstrapCommandLineRunner
implements CommandLineRunner, ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void run(String... args) {
applicationEventPublisher.publishEvent(
new DubboBootstrapStartedEvent(DubboBootstrap.getInstance()));
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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
*
* https://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.cloud.dubbo.metadata.event;
import org.springframework.context.ApplicationEvent;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
public class DubboBootstrapPreStartEvent extends ApplicationEvent {
/**
* Create a new {@code ApplicationEvent}.
* @param source the object on which the event initially occurred or with which the
* event is associated (never {@code null})
*/
public DubboBootstrapPreStartEvent(Object source) {
super(source);
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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
*
* https://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.cloud.dubbo.metadata.event;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.springframework.context.ApplicationEvent;
/**
* @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
*/
public class DubboBootstrapStartedEvent extends ApplicationEvent {
/**
* Create a new {@code ApplicationEvent}.
* @param source the object on which the event initially occurred or with which the
* event is associated (never {@code null})
*/
public DubboBootstrapStartedEvent(Object source) {
super(source);
}
@Override
public DubboBootstrap getSource() {
return (DubboBootstrap) super.getSource();
}
}