Spring Cloud 源码分析之 Eureka 篇第二章:注

2022-07-31  本文已影响0人  Java技术人

欢迎访问我的 GitHub

Spring Cloud 源码下载

启动类上的注解

package com.bolingcavalry.springclouddeepeureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringclouddeepeurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringclouddeepeurekaApplication.class, args);
    }
}

/**
 * Annotation to activate Eureka Server related configuration {@link EurekaServerAutoConfiguration}
 *
 * @author Dave Syer
 * @author Biju Kunjummen
 *
 */

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {

}
/**
 * Responsible for adding in a marker bean to activate
 * {@link EurekaServerAutoConfiguration}
 *
 * @author Biju Kunjummen
 */
@Configuration
public class EurekaServerMarkerConfiguration {

  @Bean
  public Marker eurekaServerMarkerBean() {
    return new Marker();
  }

  class Marker {
  }
}
@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
    InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
@Bean
public EurekaServerContext eurekaServerContext(ServerCodecs serverCodecs,
    PeerAwareInstanceRegistry registry, PeerEurekaNodes peerEurekaNodes) {
  return new DefaultEurekaServerContext(this.eurekaServerConfig, serverCodecs,
      registry, peerEurekaNodes, this.applicationInfoManager);
}

@Bean
public EurekaServerBootstrap eurekaServerBootstrap(PeerAwareInstanceRegistry registry,
    EurekaServerContext serverContext) {
  return new EurekaServerBootstrap(this.applicationInfoManager,
      this.eurekaClientConfig, this.eurekaServerConfig, registry,
      serverContext);
}

@Override
public void start() {
  new Thread(new Runnable() {
    @Override
    public void run() {
      try {
        //TODO: is this class even needed now?
        eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
        log.info("Started Eureka Server");

        //发送广播,将EurekaServer的配置信息广播给全部订阅了该类型消息的监听
        publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
        EurekaServerInitializerConfiguration.this.running = true;
        //发送广播,将EurekaServer的配置信息广播给全部订阅了该类型消息的监听
        publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
      }
      catch (Exception ex) {
        // Help!
        log.error("Could not initialize Eureka servlet context", ex);
      }
    }
  }).start();
}

@Configuration
protected static class EurekaServerConfigBeanConfiguration {
  @Bean
  @ConditionalOnMissingBean
  public EurekaServerConfig eurekaServerConfig(EurekaClientConfig clientConfig) {
    EurekaServerConfigBean server = new EurekaServerConfigBean();
    if (clientConfig.shouldRegisterWithEureka()) {
      // Set a sensible default if we are supposed to replicate
      server.setRegistrySyncRetries(5);
    }
    return server;
  }
}
上一篇 下一篇

猜你喜欢

热点阅读