A, eureka server automatic configuration

All articles

https://www.cnblogs.com/lay2017/p/11908715.html

 

text

Switch @EnableEurekaServer

eureka is a service management framework for a c / s architecture, springcloud be integrated as a service management. springcloud eureka using relatively simple, only need to introduce a @EnableEurekaServer add annotations to rely later, such as

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

 

@EnableEurekaServer what is the magic? Why is it possible to switch EurekaServer? To this end, we open @EnableEurekaServer comment

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

}

We can see, @ EnableEurekaServer of @Import notes introduced a EurekaServerMarkerConfiguration class. So turn @EnableEurekaServer annotation is to import the class.

 

Well, EurekaServerMarkerConfiguration this configuration class has done what?

@Configuration
public class EurekaServerMarkerConfiguration {

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

    class Marker {

    }
}

It can be seen here, just an empty Marker class into a spring of Bean. Marker itself and what functions are not implemented. As the name suggests, we can guess: @EnableEurekaServer Marker annotation is to configure Bean, but Bean's existence as a Marker, it will trigger automatic configuration to achieve the effect of a switch.

 

EurekaServerAutoConfiguration automatic configuration class

We then open the automatic configuration class EurekaServerAutoConfiguration

@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
        InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration implements WebMvcConfigurer {
    // ......
}

The first thing to note is that @ConditionalOnBean this comment, it will determine whether there is EurekaServerMarkerConfiguration.Marker the Bean. If there will resolve this automatic configuration class, which echoes the @EnableEurekaServer this annotation feature.

@EnableConfigurationProperties @PropertySource annotations and notes are loaded with some of the properties of key-value pairs.

@Import introduced an initialization class EurekaServerInitializerConfiguration (look behind it)

 

EurekaServerAutoConfiguration as automatic configuration class, we look at its main configuration which things (have been neglected)

Signboard

Service governance ultimately requires a DashBoard to visual monitoring, EurekaController based DashBoard springmvc provide related functions.

@Bean
@ConditionalOnProperty(prefix = "eureka.dashboard", name = "enabled",
        matchIfMissing = true)
public EurekaController eurekaController() {
    return new EurekaController(this.applicationInfoManager);
}

 

Find registered

Find registered as the main core functions, it is also essential

@Bean
public PeerAwareInstanceRegistry peerAwareInstanceRegistry(
        ServerCodecs serverCodecs) {
    this.eurekaClient.getApplications(); // force initialization
    return new InstanceRegistry(this.eurekaServerConfig, this.eurekaClientConfig,
            serverCodecs, this.eurekaClient,
            this.instanceRegistryProperties.getExpectedNumberOfClientsSendingRenews(),
            this.instanceRegistryProperties.getDefaultOpenForTrafficCount());
}

 

Boot

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

 

Jersey provides rpc call

jersey is a call rpc http-based framework, eureka use it restful style to provide remote services to clients.

@Bean
public javax.ws.rs.core.Application jerseyApplication(Environment environment,
        ResourceLoader resourceLoader) {

    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            false, environment);

    // Filter to include only classes that have a particular annotation.
    //
    provider.addIncludeFilter(new AnnotationTypeFilter(Path.class));
    provider.addIncludeFilter(new AnnotationTypeFilter(Provider.class));

    // Find classes in Eureka packages (or subpackages)
    //
    Set<Class<?>> classes = new HashSet<>();
    for (String basePackage : EUREKA_PACKAGES) {
        Set<BeanDefinition> beans = provider.findCandidateComponents(basePackage);
        for (BeanDefinition bd : beans) {
            Class<?> cls = ClassUtils.resolveClassName(bd.getBeanClassName(),
                    resourceLoader.getClassLoader());
            classes.add(cls);
        }
    }

    // Construct the Jersey ResourceConfig
    Map<String, Object> propsAndFeatures = new HashMap<>();
    propsAndFeatures.put(
            // Skip static content used by the webapp
            ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX,
            EurekaConstants.DEFAULT_PREFIX + "/(fonts|images|css|js)/.*");

    DefaultResourceConfig rc = new DefaultResourceConfig(classes);
    rc.setPropertiesAndFeatures(propsAndFeatures);

    return rc;
}

Here you will scan Resource, and add them to ResourceConfig.

 

EurekaServerInitializerConfiguration initialization

Then come back to see EurekaServerInitializerConfiguration class @EnableEurekaServer notes import.

@Configuration
public class EurekaServerInitializerConfiguration implements ServletContextAware, SmartLifecycle, Ordered {
    // ...

    @Override
    public void start() {
        new Thread(() -> {
            try {
                eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);

                publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
                EurekaServerInitializerConfiguration.this.running = true;
                publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
            } catch (Exception ex) {
                //
            }
        }).start();
    }

}

Initialization method called contextInitialized EurekaServerBootstrap, we follow up and see

 

public void contextInitialized(ServletContext context) {
    try {
        initEurekaEnvironment();
        initEurekaServerContext();

        context.setAttribute(EurekaServerContext.class.getName(), this.serverContext);
    }
    catch (Throwable e) {
        log.error("Cannot bootstrap eureka server :", e);
        throw new RuntimeException("Cannot bootstrap eureka server :", e);
    }
}

Here initialized EurekaEnvironment and EurekaServerContext, EurekaEnvironment nothing more than setting up something like a variety of configurations. We take a look at open initEurekaServerContext

protected void initEurekaServerContext() throws Exception {
    // ......

    // Copy registry from neighboring eureka node
    int registryCount = this.registry.syncUp();
    this.registry.openForTraffic(this.applicationInfoManager, registryCount);

    // Register all monitoring statistics.
    EurekaMonitors.registerAllStats();
}

Here mainly do two things:

1) synchronization registration information from the adjacent cluster nodes among

2) register a statistical device

 

to sum up

@EnableEurekaServer comment open analytical EurekaServerAutoConfiguration this class configuration, the configuration of the main EurekaServerAutoConfiguration prepared a kanban, found that registration, boot, Jersey and so on, EurekaServerInitializerConfigration will trigger the boot, the boot process will synchronize information from other Eureka registration among cluster nodes.

 

 

 

Guess you like

Origin www.cnblogs.com/lay2017/p/11918369.html