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" ) public class
extends WebMvcConfigurerAdapter {
// 此处省略大部分代码,仅抽取一些关键的代码片段
// 加载EurekaController, spring-cloud 提供了一些额外的接口,用来获取eurekaServer的信息
@Bean
@ConditionalOnProperty
"eureka.dashboard"
"enabled"
true )
public EurekaController eurekaController() {
return new EurekaController( this .applicationInfoManager);
}
// 配置服务节点信息,这里的作用主要是为了配置Eureka的peer节点,也就是说当有收到有节点注册上来
//的时候,需要通知给那些服务节点, (互为一个集群)
@Bean
@ConditionalOnMissingBean
public PeerEurekaNodes peerEurekaNodes(PeerAwareInstanceRegistry registry,
ServerCodecs serverCodecs) {
return new
this .eurekaServerConfig,
this
this .applicationInfoManager);
}
// EurekaServer的上下文
@Bean
public EurekaServerContext eurekaServerContext(ServerCodecs serverCodecs,
PeerAwareInstanceRegistry registry, PeerEurekaNodes peerEurekaNodes) {
return new DefaultEurekaServerContext( this .eurekaServerConfig, serverCodecs,
this .applicationInfoManager);
}
// 这个类的作用是spring-cloud和原生eureka的胶水代码,通过这个类来启动EurekaSever
// 后面这个类会在EurekaServerInitializerConfiguration被调用,进行eureka启动
@Bean
public EurekaServerBootstrap eurekaServerBootstrap(PeerAwareInstanceRegistry registry,
EurekaServerContext serverContext) {
return new EurekaServerBootstrap( this .applicationInfoManager,
this
this .eurekaServerConfig, registry,
serverContext);
} // 配置拦截器,ServletContainer里面实现了jersey框架,通过他来实现eurekaServer对外的restFull接口 @Bean public FilterRegistrationBean jerseyFilterRegistration(
javax.ws.rs.core.Application eurekaJerseyApp) {
new FilterRegistrationBean();
bean.setFilter( new ServletContainer(eurekaJerseyApp));
bean.setOrder(Ordered.LOWEST_PRECEDENCE);
bean.setUrlPatterns(
"/*" ));
return bean; } // 拦截器实例 @Bean public javax.ws.rs.core.Application jerseyApplication(Environment environment,
ResourceLoader resourceLoader) {
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)
//
new HashSet<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
//
new HashMap<String, Object>();
propsAndFeatures.put(
// Skip static content used by the webapp
ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX,
"/(fonts|images|css|js)/.*" );
new DefaultResourceConfig(classes);
rc.setPropertiesAndFeatures(propsAndFeatures);
return rc; } } |
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 public class EurekaServerInitializerConfiguration
implements ServletContextAware, SmartLifecycle, Ordered {
@Autowired
private EurekaServerConfig eurekaServerConfig;
private ServletContext servletContext;
@Autowired
private ApplicationContext applicationContext; |