1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00
mercyblitz 2019-01-10 22:58:12 +08:00
parent a6b9f6c5dc
commit 325d00d1b6

View File

@ -16,9 +16,11 @@
package org.springframework.cloud.alibaba.nacos; package org.springframework.cloud.alibaba.nacos;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration; import org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration;
@ -66,4 +68,21 @@ public class NacosDiscoveryAutoConfiguration {
return new NacosAutoServiceRegistration(registry, return new NacosAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration); autoServiceRegistrationProperties, registration);
} }
@Bean
@ConditionalOnBean(NacosAutoServiceRegistration.class) // NacosAutoServiceRegistration should be present
@ConditionalOnNotWebApplication // Not Web Application
public ApplicationRunner applicationRunner(NacosAutoServiceRegistration nacosAutoServiceRegistration) {
return args -> {
// WebServerInitializedEvent should not be multicast in Non-Web environment.
// Whatever, NacosAutoServiceRegistration must be checked it's running or not.
if (!nacosAutoServiceRegistration.isRunning()) { // If it's not running, let it start.
// FIXME: Please make sure "spring.cloud.nacos.discovery.port" must be configured on an available port,
// or the startup or Nacos health check will be failed.
nacosAutoServiceRegistration.start();
// NacosAutoServiceRegistration will be stopped after its destroy() method is invoked.
// @PreDestroy destroy() -> stop()
}
};
}
} }