您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“EurekaServer自動裝配方法及啟動流程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“EurekaServer自動裝配方法及啟動流程”吧!
@EnableEurekaServer
注解我們知道,在使用Eureka作為注冊中心的時候,我們會在啟動類中增加一個@EnableEurekaServer
注解,這個注解我們是一個自定義的EnableXXX系列的注解,主要作用我們之前也多次提到了,就是引入配置類而已??匆幌略创a吧
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({EurekaServerMarkerConfiguration.class}) public @interface EnableEurekaServer { }
引入了一個配置類EurekaServerMarkerConfiguration
,看一下這個類的具體內(nèi)容
@Configuration public class EurekaServerMarkerConfiguration { @Bean public Marker eurekaServerMarkerBean() { return new Marker(); } class Marker { } }
現(xiàn)在看這里好像難以理解,這是啥意思,搞個空的類干啥的,不要著急,接著往下看
既然注解上沒有找到我們想要的東西,那么就看一下spring.factories
文件吧,這里自動配置的實(shí)現(xiàn)類是EurekaServerAutoConfiguration
由于這個類涉及的代碼實(shí)在是太多了,這里就不貼了,咱們直接來解析這個類:
看名字就知道了這個類是負(fù)責(zé)Eureka的初始化工作的,這個類實(shí)現(xiàn)了SmartLifecycle
接口,所以在spring初始化和銷毀的時候,就會分別調(diào)用它的start和stop方法
首先看一下start方法
public void start() { new Thread(new Runnable() { @Override public void run() { try { //啟動EurekaServer eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext); log.info("Started Eureka Server"); publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig())); EurekaServerInitializerConfiguration.this.running = true; publish(new EurekaServerStartedEvent(getEurekaServerConfig())); } catch (Exception ex) { // Help! log.error("Could not initialize Eureka servlet context", ex); } } }).start(); }
這個代碼好像比較直接了當(dāng)啊,直接就起個線程啟動了EurekaServer,然后發(fā)布了一些啟動事件,來看啟動的過程吧
public void contextInitialized(ServletContext context) { try { //初始化執(zhí)行環(huán)境 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); } }
這里一共包含初始化環(huán)境和初始化上下文兩個分支
這個不是很重要,可以過濾掉
protected void initEurekaEnvironment() throws Exception { log.info("Setting the eureka configuration.."); //AWS相關(guān)的東西,可以忽略 String dataCenter = ConfigurationManager.getConfigInstance() .getString(EUREKA_DATACENTER); if (dataCenter == null) { log.info( "Eureka data center value eureka.datacenter is not set, defaulting to default"); ConfigurationManager.getConfigInstance() .setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, DEFAULT); } else { ConfigurationManager.getConfigInstance() .setProperty(ARCHAIUS_DEPLOYMENT_DATACENTER, dataCenter); } //設(shè)置 Eureka 環(huán)境,默認(rèn)為test String environment = ConfigurationManager.getConfigInstance() .getString(EUREKA_ENVIRONMENT); if (environment == null) { ConfigurationManager.getConfigInstance() .setProperty(ARCHAIUS_DEPLOYMENT_ENVIRONMENT, TEST); log.info( "Eureka environment value eureka.environment is not set, defaulting to test"); } else { ConfigurationManager.getConfigInstance() .setProperty(ARCHAIUS_DEPLOYMENT_ENVIRONMENT, environment); } }
protected void initEurekaServerContext() throws Exception { // 設(shè)置json與xml序列化工具 JsonXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH); XmlXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH); if (isAws(this.applicationInfoManager.getInfo())) { this.awsBinder = new AwsBinderDelegate(this.eurekaServerConfig, this.eurekaClientConfig, this.registry, this.applicationInfoManager); this.awsBinder.start(); } EurekaServerContextHolder.initialize(this.serverContext); log.info("Initialized server context"); // 同步Eureka集群數(shù)據(jù) int registryCount = this.registry.syncUp(); this.registry.openForTraffic(this.applicationInfoManager, registryCount); // 注冊監(jiān)控統(tǒng)計(jì)信息 EurekaMonitors.registerAllStats(); }
這個方法中同步集群數(shù)據(jù)和注冊監(jiān)控信息都涉及的內(nèi)容比較多,所以本篇文章就不再展開了,請關(guān)注我留意后續(xù)文章
看到這里就揭開了開篇@EnableEurekaServer
注解注入的那個bean的含義了。也就是說如果咱們的啟動類沒有使用@EnableEurekaServer
注解的話,這個自動配置類就不會執(zhí)行,那也就沒有Eureka的事了
深入這個注解發(fā)現(xiàn)這個還是使用的@Import
注解的機(jī)制引入了兩個類,這個注解在之前的源碼解析文章中也多次提到了,這里就不展開了
EurekaDashboardProperties
這個類比較簡單,主要是Eureka的控制臺的相關(guān)配置
//控制臺默認(rèn)路徑 private String path = "/"; //是否開啟控制臺 private boolean enabled = true;
InstanceRegistryProperties
,這個類是控制Eureka的注冊時的配置信息
//每分鐘續(xù)約次數(shù) @Value("${eureka.server.expectedNumberOfRenewsPerMin:1}") private int expectedNumberOfRenewsPerMin = 1; //默認(rèn)打開的通信數(shù)量 @Value("${eureka.server.defaultOpenForTrafficCount:1}") private int defaultOpenForTrafficCount = 1;
相信大家比較熟悉這個注解,加載Eureka的配置文件而已
配置文件中也僅僅只包含這個信息
spring.http.encoding.force=false
EurekaServerAutoConfiguration
類上幾個注解就解析完了,接著看一下這個類中注入的幾個比較重要的類吧
EurekaServerConfig 如果當(dāng)前應(yīng)用允許注冊到其他Eureka服務(wù)中時,也就是屬性eureka.client.fetch-registry
為true時。就設(shè)置屬性registrySyncRetries
的值為5,這個屬性的意思是當(dāng)Eureka服務(wù)器啟動時嘗試去獲取集群里其他服務(wù)器上的注冊信息的次數(shù)
這個就是Eureka自己的controller了,控制臺的相關(guān)信息就是從這里獲取的
設(shè)置Eureka的序列化工具
集群注冊信息同步相關(guān)的類,請期待后續(xù)深入解析文章
到此,相信大家對“EurekaServer自動裝配方法及啟動流程”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。