Eureka Server启动(一)

匿名 (未验证) 提交于 2019-12-03 00:27:02

start包配置

spring-cloud-netflix-eureka-server包结构

在这个包下面META-INF下面有个spring.factories这个配置文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration

使用了springboot EnableAutoConfiguration这个注解,在springboot应用启动的时候,会自动加载

EurekaServerAutoConfiguration这个bean,后面主要看这个bean的源码

启动源码分析

EurekaServerAutoConfiguration

用于EurekaServer往beanfactory添加相关eureka-server功能bean

@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
InstanceRegistryProperties.class})
@PropertySource("classpath:/eureka/server.properties")
publicclassextendsWebMvcConfigurerAdapter {
// 此处省略大部分代码,仅抽取一些关键的代码片段
// 加载EurekaController, spring-cloud 提供了一些额外的接口,用来获取eurekaServer的信息
@Bean
@ConditionalOnProperty"eureka.dashboard""enabled"true)
publicEurekaController eurekaController() {
returnnewEurekaController(this.applicationInfoManager);
}
// 配置服务节点信息,这里的作用主要是为了配置Eureka的peer节点,也就是说当有收到有节点注册上来
//的时候,需要通知给那些服务节点, (互为一个集群)
@Bean
@ConditionalOnMissingBean
publicPeerEurekaNodes peerEurekaNodes(PeerAwareInstanceRegistry registry,
ServerCodecs serverCodecs) {
returnnewthis.eurekaServerConfig,
thisthis.applicationInfoManager);
}
// EurekaServer的上下文
@Bean
publicEurekaServerContext eurekaServerContext(ServerCodecs serverCodecs,
PeerAwareInstanceRegistry registry, PeerEurekaNodes peerEurekaNodes) {
returnnewDefaultEurekaServerContext(this.eurekaServerConfig, serverCodecs,
this.applicationInfoManager);
}
// 这个类的作用是spring-cloud和原生eureka的胶水代码,通过这个类来启动EurekaSever
// 后面这个类会在EurekaServerInitializerConfiguration被调用,进行eureka启动
@Bean
publicEurekaServerBootstrap eurekaServerBootstrap(PeerAwareInstanceRegistry registry,
EurekaServerContext serverContext) {
returnnewEurekaServerBootstrap(this.applicationInfoManager,
thisthis.eurekaServerConfig, registry,
serverContext);
}
// 配置拦截器,ServletContainer里面实现了jersey框架,通过他来实现eurekaServer对外的restFull接口
@Bean
publicFilterRegistrationBean jerseyFilterRegistration(
javax.ws.rs.core.Application eurekaJerseyApp) {
newFilterRegistrationBean();
bean.setFilter(newServletContainer(eurekaJerseyApp));
bean.setOrder(Ordered.LOWEST_PRECEDENCE);
bean.setUrlPatterns(
"/*"));
returnbean;
}
// 拦截器实例
@Bean
publicjavax.ws.rs.core.Application jerseyApplication(Environment environment,
ResourceLoader resourceLoader) {
newClassPathScanningCandidateComponentProvider(
false, environment);
// Filter to include only classes that have a particular annotation.
//
provider.addIncludeFilter(newAnnotationTypeFilter(Path.class));
provider.addIncludeFilter(newAnnotationTypeFilter(Provider.class));
// Find classes in Eureka packages (or subpackages)
//
newHashSet<Class<?>>();
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
//
newHashMap<String, Object>();
propsAndFeatures.put(
// Skip static content used by the webapp
ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX,
"/(fonts|images|css|js)/.*");
newDefaultResourceConfig(classes);
rc.setPropertiesAndFeatures(propsAndFeatures);
returnrc;
}
}



1.@Configuration 表明这是一个配置类
2.@Import(EurekaServerInitializerConfiguration.class) 导入启动EurekaServer的bean
3.@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)  这个是表示只有在spring容器

里面含有Market这个实例的时候,才会加载当前这个Bean(EurekaServerAutoConfiguration ),这个就是控制

是否开启EurekaServer的关键,在@EableEurekaServer这个注解里面,就是创建了一个Market兑现,用来

告诉他,我开启了Eureka服务

4.@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class })

加载配置


EurekaServerInitializerConfiguration

从上面的代码分析上可以看到,在EurekaServerAutoConfiguration加载完成之后就会执行

EurekaServerInitializerConfiguration这个类的start方法, 这个类实现了spring的SmartLifecyl,具体可以

可以看这个SmartLifecycle,里面有详细的介绍。


EurekaServerInitializerConfiguration
/**
* @author Dave Syer
*/
@Configuration
@CommonsLog
publicclassEurekaServerInitializerConfiguration
implementsServletContextAware, SmartLifecycle, Ordered {
@Autowired
privateEurekaServerConfig eurekaServerConfig;
privateServletContext servletContext;
@Autowired
privateApplicationContext applicationContext;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!