pre start
开始EnableEurekaClient注解
@Target ({ElementType.TYPE})
@Retention (RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import ({EnableDiscoveryClientImportSelector.class})
public @interface EnableDiscoveryClient {
boolean autoRegister() default true ;
}
主要是加载配置文件到达开启服务
selectImports : 如xml中import 其它xml文件
selectImports: 加载META-INF/spring.factories 的类
public String[] selectImports (AnnotationMetadata metadata) {
String[] imports = super .selectImports(metadata);
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(this .getAnnotationClass().getName(), true ));
boolean autoRegister = attributes.getBoolean("autoRegister" );
if (autoRegister) {
List<String> importsList = new ArrayList(Arrays.asList(imports));
importsList.add("org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration" );
imports = (String[])importsList.toArray(new String[0 ]);
}
return imports;
}
protected boolean isEnabled () {
return ((Boolean)(new RelaxedPropertyResolver(this .getEnvironment())).getProperty("spring.cloud.discovery.enabled" , Boolean.class, Boolean.TRUE)).booleanValue();
}
List<String > factories = new ArrayList(new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(this .annotationClass, this .beanClassLoader)));
开启默认配置
spring.cloud.service-registry.auto-registration =true
@Configuration
@EnableConfigurationProperties ({AutoServiceRegistrationProperties.class})
public class AutoServiceRegistrationConfiguration {
public AutoServiceRegistrationConfiguration () {
}
}
server start EurekaServerAutoConfiguration
@Import(EurekaServerInitializerConfiguration.class) 导入配置
其中继承SmartLifecycle 加入spring生命周期
@Override
public void start () {
new Thread(new Runnable() {
@Override
public void run () {
try {
log.info("Started Eureka Server" );
publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
EurekaServerInitializerConfiguration.this .running = true ;
publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
}
catch (Exception ex) {
log.error("Could not initialize Eureka servlet context" , ex);
}
}
}).start();
}
protected void initEurekaServerContext () throws Exception {
EurekaServerContextHolder.initialize(this .serverContext);
log.info("Initialized server context" );
int registryCount = this .registry.syncUp();
this .registry.openForTraffic(this .applicationInfoManager, registryCount);
EurekaMonitors.registerAllStats();
}
protected void postInit () {
renewsLastMin.start();
if (evictionTaskRef.get () != null ) {
evictionTaskRef.get ().cancel();
}
evictionTaskRef.set (new EvictionTask());
evictionTimer.schedule(evictionTaskRef.get (),
serverConfig.getEvictionIntervalTimerInMs(),
serverConfig.getEvictionIntervalTimerInMs());
}
DefaultEurekaServerContext
@PostConstruct
@Override
public void initialize () throws Exception {
logger.info("Initializing ..." );
peerEurekaNodes.start();
registry.init(peerEurekaNodes);
logger.info("Initialized" );
}
public void start () {
taskExecutor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactory() {
@Override
public Thread newThread (Runnable r) {
Thread thread = new Thread(r, "Eureka-PeerNodesUpdater" );
thread.setDaemon(true );
return thread;
}
}
);
try {
updatePeerEurekaNodes(resolvePeerUrls());
Runnable peersUpdateTask = new Runnable() {
@Override
public void run () {
try {
updatePeerEurekaNodes(resolvePeerUrls());
} catch (Throwable e) {
logger.error("Cannot update the replica Nodes" , e);
}
}
};
taskExecutor.scheduleWithFixedDelay(
peersUpdateTask,
serverConfig.getPeerEurekaNodesUpdateIntervalMs(),
serverConfig.getPeerEurekaNodesUpdateIntervalMs(),
TimeUnit.MILLISECONDS
);
} catch (Exception e) {
throw new IllegalStateException(e);
}
for (PeerEurekaNode node : peerEurekaNodes) {
logger.info("Replica node URL: " + node.getServiceUrl());
}
}