一、eureka服务端自动配置
所有文章
https://www.cnblogs.com/lay2017/p/11908715.html
正文
@EnableEurekaServer开关
eureka是一个c/s架构的服务治理框架,springcloud将其集成用作服务治理。springcloud使用eureka比较简单,只需要引入依赖以后添加一个@EnableEurekaServer注解即可,如
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication { public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
} }
@EnableEurekaServer又有什么魔力呢?为什么它能够开关EurekaServer?为此,我们打开@EnableEurekaServer注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer { }
可以看到,@EnableEurekaServer的@Import注解导入了一个EurekaServerMarkerConfiguration类。所以,开启@EnableEurekaServer注解也就是导入该类。
那么,EurekaServerMarkerConfiguration这个配置类又做了啥?
@Configuration
public class EurekaServerMarkerConfiguration { @Bean
public Marker eurekaServerMarkerBean() {
return new Marker();
} class Marker { }
}
可以看到,这里只是把一个空的Marker类变成了spring中的Bean。而Marker本身什么功能都没有实现。顾名思义,我们可以这样猜测一下:@EnableEurekaServer注解就是将Marker配置为Bean,而Marker作为Bean的存在,将会触发自动配置,从而达到了一个开关的效果。
EurekaServerAutoConfiguration自动配置类
我们再打开EurekaServerAutoConfiguration这个自动配置类
@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration implements WebMvcConfigurer {
// ......
}
首先值得注意的是@ConditionalOnBean这个注解,它将判断EurekaServerMarkerConfiguration.Marker这个Bean是否存在。如果存在才会解析这个自动配置类,从而呼应了@EnableEurekaServer这个注解的功能。
@EnableConfigurationProperties注解和@PropertySource注解都加载了一些键值对的属性。
@Import导入了一个初始化类EurekaServerInitializerConfiguration(后面再看它)
EurekaServerAutoConfiguration作为自动配置类,我们看看它主要配置了哪些东西(有所忽略)
看板
服务治理少不了需要一个DashBoard来可视化监控,EurekaController基于springmvc提供DashBoard相关的功能。
@Bean
@ConditionalOnProperty(prefix = "eureka.dashboard", name = "enabled",
matchIfMissing = true)
public EurekaController eurekaController() {
return new EurekaController(this.applicationInfoManager);
}
发现注册
发现注册作为主要的核心功能,也是必不可少的
@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());
}
启动引导
@Bean
public EurekaServerBootstrap eurekaServerBootstrap(PeerAwareInstanceRegistry registry,
EurekaServerContext serverContext) {
return new EurekaServerBootstrap(this.applicationInfoManager,
this.eurekaClientConfig, this.eurekaServerConfig, registry,
serverContext);
}
Jersey提供rpc调用
jersey是一个restful风格的基于http的rpc调用框架,eureka使用它来为客户端提供远程服务。
@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;
}
这里将会扫描Resource,并添加到ResourceConfig当中。
EurekaServerInitializerConfiguration初始化
再回过头来,看看@EnableEurekaServer注解导入的EurekaServerInitializerConfiguration类。
@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();
} }
初始化过程调用了EurekaServerBootstrap的contextInitialized方法,我们跟进看看
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);
}
}
这里初始化了EurekaEnvironment和EurekaServerContext,EurekaEnvironment无非就是设置了各种配置之类的东西。我们打开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();
}
这里主要做了两件事:
1)从相邻的集群节点当中同步注册信息
2)注册一个统计器
总结
@EnableEurekaServer注解开启了EurekaServerAutoConfiguration这个配置类的解析,EurekaServerAutoConfiguration这个配置了主要准备了看板、注册发现、启动引导、Jersey等,EurekaServerInitializerConfigration将会触发启动引导,引导过程会从其它Eureka集群节点当中同步注册信息。
一、eureka服务端自动配置的更多相关文章
- SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端、Eureka 服务信息、Eureka 发现管理、Eureka 安全配置、Eureka-HA(高可用) 机制、Eureka 服务打包部署)
1.概念:Eureka 服务发现框架 2.具体内容 对于服务发现框架可以简单的理解为服务的注册以及使用操作步骤,例如:在 ZooKeeper 组件,这个组件里面已经明确的描述了一个服务的注册以及发现操 ...
- spring-cloud配置高可用eureka服务端
spring-cloud配置eureka服务端 eureka用来发现其他程序 依赖 <?xml version="1.0" encoding="UTF-8" ...
- SpringCloud02 Eureka知识点、Eureka服务端和客户端的创建、Eureka服务端集群、Eureka客户端向集群的Eureka服务端注册
1 Eureka知识点 按照功能划分: Eureka由Eureka服务端和Eureka客户端组成 按照角色划分: Eureka由Eureka Server.Service Provider.Servi ...
- 03-openldap服务端安装配置
openldap服务端安装配置 阅读目录 基础环境准备 安装openldap服务端 初始化openldap配置 启动OpenLDAP 重新生成配置文件信息 规划OpenLDAP目录树组织架构 使用GU ...
- springcloud(三):Eureka服务端
一. 因为使用一个注册中心服务器端,n个客户端:n个生产者客户端.n消费者客户端....,所有的客户端最好的方式就是通过对象传递参数,因此需要创建一个公共组件项目,为n个客户端传值提供方便 二.创建公 ...
- spring cloud eureka 服务端开启密码认证后,客户端无法接入问题
Eureka服务端开启密码的认证比较简单 在pom文件中加入: <dependency> <groupId>org.springframework.boot</group ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理
笔记 7.Eureka服务注册中心配置控制台问题处理 简介:讲解服务注册中心管理后台,(后续还会细讲) 问题:eureka管理后台出现一串红色字体:是警告,说明有服务上线率低 EMERGENC ...
- Eureka 客户端连接Eureka服务端时 报Cannot execute request on any known server 解决办法
报Cannot execute request on any known server 这个错,总的来说就是连接Eureka服务端地址不对. 因为配置eureka.client.serviceUrl. ...
- jquery.dataTables的探索之路-服务端分页配置
最近闲来无事想研究下数据表格,因为之前接触过layui和bootstrap的数据表格,本着能学多少学多少的学习态度,学习下dataTables的服务端分页配置.特与同学们一块分享下从中遇到的问题和解决 ...
随机推荐
- Vrms、Vpk、W、dBm、dBW、dBuV、dBm/Hz
负载阻抗Z 在做这些单位转换前第一个需要提到的就是负载阻抗(Z, Ohm),我们在测试测量中说某个量为上面的某一个单位时候,都包含了一个前提条件,那就是负载阻抗,离开了负载阻抗你说的这些总带有一丝耍流 ...
- Qt kdChart 甘特图案例
KDChart 甘特图在Qt中的加载使用案例,代码来自官方 mainwindow.h /******************************************************* ...
- osg fbx模型删除模型中的某几个节点,实现编辑模型的功能
fbx model element count:80 三维视图: {三维} 4294967295 osg::MatrixTransform1 基本墙 wall_240 [361750] 4294967 ...
- git clone时加上--depth 1
当项目过大时,git clone时会出现error: RPC failed; HTTP curl The requested URL returned error: Gateway Time-out的 ...
- jQuery学习二
1.id选择器: // 4.如果页面中多个元素id相同,jquery只会获取第一个id的jquery对象 var jquery = $('#name'); alert(jquery.val()); v ...
- END使用
[root@bogon ~]# cat d.sh #!/bin/bash#. /etc/init.d/functionscat <<END+------------------------ ...
- 使用PHP实现命令模式(转)
<?php /** * 命令模式 2010-08-21 sz * @author phppan.p#gmail.com http://www.phppan.com * 哥学社成员(http:// ...
- python面向对象之花里胡哨大杂烩
python类的魔法方法之__str__.__repr__.__format__.__module__.__class__.__slots__.__call__.__del__(析构函数) 字符串的内 ...
- linux下无法启动webdriver问题
linux下无法启动webdriver问题: 查看是否有足够多的webdriver进程: ps -ef | grep chromedriver kill -9 `ps -ef |grepchromed ...
- markdown转移字符表
本片转的ASCII码,主要针对$,另外我为了不让"&#xxx;"被转移成字符,我在分号";"前加了个空格,复制的时候注意一下 字符 转义 0 空格 @ ...