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

extend AbstractAutoServiceRegistration instead of implements AutoServiceRegistration , Fixes #18

This commit is contained in:
flystar32 2018-09-17 23:28:39 +08:00
parent bd814e8697
commit 625e85b040
2 changed files with 62 additions and 82 deletions

View File

@ -27,7 +27,6 @@ import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
import org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry; import org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -64,7 +63,10 @@ public class NacosDiscoveryAutoConfiguration {
@Bean @Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class) @ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosAutoServiceRegistration nacosAutoServiceRegistration( public NacosAutoServiceRegistration nacosAutoServiceRegistration(
ApplicationContext context, NacosServiceRegistry registry, NacosRegistration registration) { NacosServiceRegistry registry,
return new NacosAutoServiceRegistration(context, registry, registration); AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
return new NacosAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration);
} }
} }

View File

@ -16,112 +16,90 @@
package org.springframework.cloud.alibaba.nacos.registry; package org.springframework.cloud.alibaba.nacos.registry;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationContext; import org.springframework.util.Assert;
import org.springframework.context.SmartLifecycle; import org.springframework.util.StringUtils;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
/** /**
* @author xiaojing * @author xiaojing
*/ */
public class NacosAutoServiceRegistration public class NacosAutoServiceRegistration extends AbstractAutoServiceRegistration<NacosRegistration> {
implements AutoServiceRegistration, SmartLifecycle, Ordered { private static final Logger LOGGER = LoggerFactory.getLogger(NacosAutoServiceRegistration.class);
private static final Logger logger = LoggerFactory.getLogger(NacosAutoServiceRegistration.class);
@Autowired @Autowired
private NacosRegistration registration; private NacosRegistration registration;
private int order = 0;
private AtomicBoolean running = new AtomicBoolean(false);
private AtomicInteger port = new AtomicInteger(0);
private ApplicationContext context;
private ServiceRegistry serviceRegistry;
public NacosAutoServiceRegistration(ApplicationContext context, public NacosAutoServiceRegistration(ServiceRegistry<NacosRegistration> serviceRegistry,
ServiceRegistry<NacosRegistration> serviceRegistry, AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) { NacosRegistration registration) {
this.context = context; super(serviceRegistry, autoServiceRegistrationProperties);
this.serviceRegistry = serviceRegistry;
this.registration = registration; this.registration = registration;
} }
@Deprecated
public void setPort(int port) {
getPort().set(port);
}
@Override @Override
public void start() { protected NacosRegistration getRegistration() {
if (this.port.get() != 0) { if (this.registration.getPort() < 0 && this.getPort().get() > 0) {
this.registration.setPort(port.get()); this.registration.setPort(this.getPort().get());
} }
Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");
return this.registration;
}
if (!this.running.get() && this.registration.getPort() > 0) { @Override
this.serviceRegistry.register(this.registration); protected NacosRegistration getManagementRegistration() {
this.context return null;
.publishEvent(new InstanceRegisteredEvent(this, this.registration)); }
this.running.set(true);
@Override
protected void register() {
if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
LOGGER.debug("Registration disabled.");
return;
} }
} if (this.registration.getPort() < 0) {
this.registration.setPort(getPort().get());
@Override
public void stop() {
this.serviceRegistry.deregister(this.registration);
this.running.set(false);
}
@Override
public boolean isRunning() {
return this.running.get();
}
@Override
public int getPhase() {
return 0;
}
@Override
public boolean isAutoStartup() {
return true;
}
@Override
public void stop(Runnable callback) {
this.stop();
callback.run();
}
@Override
public int getOrder() {
return this.order;
}
@EventListener(WebServerInitializedEvent.class)
public void onApplicationEvent(WebServerInitializedEvent event) {
int localPort = event.getWebServer().getPort();
ApplicationContext context = event.getApplicationContext();
if(!(context instanceof ConfigurableWebServerApplicationContext) || !"management".equals(((ConfigurableWebServerApplicationContext)context).getServerNamespace())) {
logger.info("Updating port to {}", localPort);
this.port.compareAndSet(0, event.getWebServer().getPort());
this.start();
} }
super.register();
} }
@EventListener({ ContextClosedEvent.class }) @Override
public void onApplicationEvent(ContextClosedEvent event) { protected void registerManagement() {
if (event.getApplicationContext() == this.context) { if (!this.registration.getNacosDiscoveryProperties().isRegisterEnabled()) {
this.stop(); return;
} }
super.registerManagement();
} }
@Override
protected Object getConfiguration() {
return this.registration.getNacosDiscoveryProperties();
}
@Override
protected boolean isEnabled() {
return this.registration.getNacosDiscoveryProperties().isRegisterEnabled();
}
@Override
@SuppressWarnings("deprecation")
protected String getAppName() {
String appName = registration.getNacosDiscoveryProperties().getService();
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
}
} }