Eureka源码阅读解析: Server服务端启动流程实例
背景介绍
Eureka是Netflix开源的基于RESTful风格的服务注册和发现组件,主要是为了解决动态集群下的弹性的问题,尤其是在云计算中自动化资源管理的需求。
Server服务端启动流程说明
以下是Eureka Server服务端的启动流程:
-
EurekaServerAutoConfiguration会注册以下bean:
-
EurekaServerInitializerConfiguration
-
EurekaServerBootstrap
-
PeerAwareInstanceRegistryImpl
-
EurekaServerContext
-
PeerEurekaNode
-
EurekaServerConfig
-
EurekaServerInitializerConfiguration会注册以下bean:
-
EurekaHealthStatusAggregator
-
EurekaServerBootstrap会完成以下任务:
-
设置环境配置
-
初始化EurekaServerConfig
-
初始化自定义EurekaServerConfig(如果存在)
-
初始化EurekaServerContext
-
初始化PeerEurekaNode
-
初始化PeerAwareInstanceRegistryImpl
-
PeerAwareInstanceRegistryImpl会完成以下任务:
-
启动心跳定时器
-
启动注册任务
-
PeerEurekaNode会完成以下任务:
-
启动定时任务
-
EurekaServerContext会注册以下bean:
-
EurekaClientConfigBean
-
EurekaInstanceConfigBean
-
EurekaClientRegistry
-
EurekaClientRegistry和PeerAwareInstanceRegistryImpl会建立注册表的联系
-
注册中心服务器启动成功
示例说明
下面是两个示例:
示例一
@ComponentScan(basePackages = {"com.netflix.eureka"})
@EnableAutoConfiguration
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
new SpringApplicationBuilder(EurekaServer.class).web(WebApplicationType.SERVLET).run(args);
}
}
此示例展示了如何使用Spring Boot快速搭建一个Eureka Server服务端。
示例二
@Configuration
public class EurekaServerConfig {
@Value("${eureka.server.port:8761}")
private int port;
@Value("${eureka.service.url:http://${localhost}:8761/eureka/}")
private String serviceUrl;
@Value("${eureka.dashboard.url:http://${localhost}:8761}")
private String dashboardUrl;
@Bean
public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean();
instance.setNonSecurePort(port);
instance.setInstanceId("${spring.application.name}:${server.port}");
instance.setPreferIpAddress(true);
instance.setIpAddress("localhost");
instance.setVirtualHostName("localhost");
instance.setSecurePortEnabled(false);
return instance;
}
@Bean
public EurekaServerConfig eurekaServerConfig(EurekaInstanceConfigBean eurekaInstanceConfigBean) {
EurekaServerConfig server = new EurekaServerConfig();
server.setShutDownClients(false);
server.setHeartbeatExecutorThreadPoolSize(5);
server.setServerSocketTimeout(30000);
server.setRegistrySyncRetries(3);
server.setShouldFetchRegistry(false);
server.setPeerEurekaNodesUpdateIntervalMs(5000);
server.setEvictionIntervalTimerInMs(60000);
server.setPeerEurekaStatusRefreshTimeIntervalMs(1000);
server.setMinAvailableInstancesForPeerReplication(2);
server.setWaitTimeInMsWhenSyncEmpty(1000);
server.setNumberOfReplicationRetries(3);
server.setPeerEurekaNodesUpdateIntervalMs(5000);
server.setInitialInstanceInfoReplicationIntervalSeconds(10);
server.setEurekaServerURLContext("eureka");
server.setHealthCheckEnabled(true);
server.setRemoteRegionAppWhitelist("");
server.setServerContext(server.getEurekaServerURLContext());
server.setRemoteRegionRegistryFetchIntervalInSeconds(10);
server.setPeerNodeConnectTimeoutMs(2000);
server.setPeerNodeReadTimeoutMs(5000);
server.setDeltaRetentionTimerIntervalInMs(300000);
server.setDeltaRetentionTimerExpirationInMs(600000);
server.setEnableSelfPreservation(true);
server.setPrefetchRegistryCache(false);
server.setPeerEurekaNode(eurekaInstanceConfigBean.getHostName() + ":" + eurekaInstanceConfigBean.getNonSecurePort());
List<String> urls = new ArrayList<>();
urls.add(serviceUrl);
server.setServiceUrl(urls);
return server;
}
}
此示例展示了如何自定义Eureka Server配置。其中,EurekaInstanceConfigBean定义了注册中心服务器实例的相关配置,比如端口号、预先IP地址等;EurekaServerConfig定义了注册中心服务器的相关配置,比如心跳间隔、应用白名单、配置中心URL等。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Eureka源码阅读解析Server服务端启动流程实例 - Python技术站