新聞中心
這篇文章主要介紹“怎么把非springboot項(xiàng)目集成eureka”,在日常操作中,相信很多人在怎么把非springboot項(xiàng)目集成eureka問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么把非springboot項(xiàng)目集成eureka”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
10余年的江陽網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整江陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“江陽網(wǎng)站設(shè)計(jì)”,“江陽網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
1 背景
隨著SOA架構(gòu)的演進(jìn),越來越多的服務(wù)商需要提供一種通用的可以動(dòng)態(tài)伸縮的基礎(chǔ)架構(gòu),
而對嘻「」來說,亦是如此。并且隨著TPS的不斷增加,more and more endpoint 需要整合這種分布式基礎(chǔ)架構(gòu)。這里有點(diǎn)類似當(dāng)年google的Bigtable 的論文剛發(fā)表時(shí)的案例,對不同業(yè)務(wù),或者相同業(yè)務(wù)不同功能,在或者相同功能實(shí)現(xiàn)節(jié)點(diǎn)拓展,分布式處理。(要實(shí)現(xiàn)分布式處理能力的系統(tǒng)必然伴隨這分布式場景的一些問題,這里不做討論。
2 目的說明
類似Dubbo分布式服務(wù)治理,不同的微服務(wù)需要依賴注冊中心做服務(wù)治理與管控,springCloud 也是這樣,作為微服務(wù)的生態(tài),不同的組件負(fù)責(zé)不同的功能(例如archaius 做配置and eureka 做服務(wù)PUBSUB等...)。
3 方案對比
3.1 directly to register
簡單的說.通過分析源碼發(fā)現(xiàn)DiscoveryClient對象是客戶端啟動(dòng)加載的核心類,
它創(chuàng)建的時(shí)候就會(huì)去注冊,如下
在restemplate 調(diào)用的過程中需要先通過應(yīng)用層通信拿到服務(wù)器對應(yīng)spring.application.name 為vipaddress的InstanceInfo,instanceOf里面維護(hù)了對應(yīng)的你將要調(diào)用的服務(wù)器或者服務(wù)如下:
可能會(huì)在這里拿不出來實(shí)例信息,最后報(bào)沒有對應(yīng)服務(wù)實(shí)例,以上來自Applications Class,
源碼后文會(huì)詳細(xì)分析,這里不過多分析只解決問題
****** 4.2 The Error for Register Process******
這里按照官網(wǎng)案例搭建注冊環(huán)境,可能會(huì)出現(xiàn)一切準(zhǔn)備就緒然注冊不上去的情況。
走一遍注冊流程如下:
Archaius會(huì)默認(rèn)加載classpath下的config.properties文件作為當(dāng)前內(nèi)存資源配置,也可以改,通過archaius.configurationSource.defaultFileName,當(dāng)然既然是動(dòng)態(tài)的肯定也是可以做多數(shù)據(jù)源動(dòng)態(tài)處理,具體不詳細(xì)說明了
具體配置意義先不考慮,先為了解決問題考慮環(huán)境
****** 4.3 eureka/apps for configuration******
手動(dòng)注冊這一截是沒有的 顯示empty
所以我們需要把這一截加上去,
代碼如下:
int port = instanceInfo.getPort(); Mapmaps = Maps.newHashMap(); maps.put("management.port", String.valueOf(port)); //activity-manager:dev-10.0.2.17:8010 applicationInfoManager.getInstance().registerAppMetadata(maps);
還有一個(gè)細(xì)節(jié)問題:
Archaius1Utils.initConfig("eureka-client");
這個(gè)是在查看報(bào)錯(cuò)日志的時(shí)候報(bào)的,為了更好的兼容框架需要解決,大概意思是說缺少這個(gè)配置
在加載此類實(shí)現(xiàn)類的時(shí)候就會(huì)一起初始化該類。很明顯組件已經(jīng)幫我們做了,我們只需要提供對應(yīng)配置文件即可
依賴為
這里需要注意的兩點(diǎn):
繼續(xù)DEBUG會(huì)看到:
這里開始遞歸調(diào)用攔截器,也就是我們在啟動(dòng)時(shí)候放進(jìn)去的攔截器會(huì)在這里調(diào)用
當(dāng)然這里有個(gè)細(xì)節(jié)
這里我們需要關(guān)注兩個(gè)點(diǎn)
1 通過服務(wù)serviceId拿到核心instanceInfo ,下面會(huì)解釋
2 通過對應(yīng)服務(wù)信息選擇對應(yīng)服務(wù)調(diào)用
分別對應(yīng)第一二行代碼
繼續(xù)debug
這里需要通過一個(gè)serviceId加載一個(gè)LloadBalancer,代碼如下
Spring 默認(rèn)會(huì)調(diào)用以上工廠加載,所以我們點(diǎn)進(jìn)去會(huì)看到
這里也需要注意兩點(diǎn):
1 我們這個(gè)lloadbalancer是用的時(shí)候才去加載,換句話說懶加載
2 這里有個(gè)緩存的 操作 第一個(gè)if為false就會(huì)返回緩存,換句話說只會(huì)創(chuàng)建一次
然后我們進(jìn)去
這些東西。
以上的clientConfig 就是我們在代碼設(shè)置進(jìn)去的EurekaClientConfigBean
現(xiàn)在還沒有上面參數(shù),不要緊因?yàn)檫€沒有開始初始化。
繼續(xù)加載完之后開始加載
上
圖大概意思就是先加載父類baseLoadbalancer然后加載自己
restOfInit方法就是核心加載方法
上圖標(biāo)紅的是比較重要的方法
這里比較重點(diǎn)的如上標(biāo)紅處
進(jìn)去看到:
他就會(huì)走到這里,這是什么呢?
對應(yīng)一下會(huì)從visualHostNameAppMap 里面通過vipAddress那到List
總結(jié)一下:
負(fù)載均衡實(shí)現(xiàn)原理概述為根據(jù)配置加載負(fù)載均衡攔截器,用戶客戶端調(diào)用遍歷處理,通過servceId通過HTTP拿到對應(yīng)instanceInfo(和dubbo流程差不多,第一次都需要去拿之后緩存在本地。默認(rèn)90秒把重新調(diào)用一次拉取信息)多個(gè)需要依算法選取一個(gè)然后進(jìn)行遠(yuǎn)程調(diào)用。
代碼如下:
package com.kili.lipapay.nmc.common; import com.google.common.collect.Maps; import com.netflix.appinfo.ApplicationInfoManager; import com.netflix.appinfo.InstanceInfo; import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider; import com.netflix.config.ConfigurationManager; import com.netflix.config.DynamicPropertyFactory; import com.netflix.discovery.DefaultEurekaClientConfig; import com.netflix.discovery.DiscoveryClient; import com.netflix.discovery.DiscoveryManager; import com.netflix.discovery.EurekaClient; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.util.Date; import java.util.Map; import java.util.Properties; @Component class SimpleEurakeService { private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory .getInstance(); public void registerWithEureka() throws IOException { ApplicationInfoManager applicationInfoManager = null; EurakeInstanceConfig config = null; InstanceInfo instanceInfo = null; // Register with Eureka if (applicationInfoManager == null) { config = new EurakeInstanceConfig(); instanceInfo = new EurekaConfigBasedInstanceInfoProvider(config).get(); applicationInfoManager = new ApplicationInfoManager(config, instanceInfo); } // Archaius1Utils.initConfig("eureka-client"); Properties properties = new Properties(); InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); properties.load(inputStream); properties.setProperty("eureka.ipAddr", InetAddress.getLocalHost().getHostAddress()); String instanceId = applicationInfoManager.getInfo().getAppName() + ":dev-" + properties.getProperty("eureka.ipAddr") + ":" + properties.getProperty("eureka.port"); properties.setProperty("eureka.instanceId", instanceId); ConfigurationManager.loadProperties(properties); int port = instanceInfo.getPort(); Mapmaps = Maps.newHashMap(); maps.put("management.port", String.valueOf(port)); //activity-manager:dev-10.0.2.17:8010 applicationInfoManager.getInstance().registerAppMetadata(maps); applicationInfoManager.getInstance().setInstanceStatus( InstanceInfo.InstanceStatus.UP); EurekaClient eurekaClient = new DiscoveryClient(applicationInfoManager, new DefaultEurekaClientConfig()); String vipAddress = configInstance.getStringProperty( "eureka.vipAddress", "unknown").get(); InstanceInfo nextServerInfo = null; while (nextServerInfo == null) { try { nextServerInfo = eurekaClient .getNextServerFromEureka(vipAddress, false); } catch (Throwable e) { System.out .println("Waiting for service to register with eureka.."); try { Thread.sleep(10000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } public void unRegisterWithEureka() { // Un register from eureka. DiscoveryManager.getInstance().shutdownComponent(); } private void processRequest(final Socket s) { try { BufferedReader rd = new BufferedReader(new InputStreamReader( s.getInputStream())); String line = rd.readLine(); if (line != null) { System.out.println("Received the request from the client."); } PrintStream out = new PrintStream(s.getOutputStream()); System.out.println("Sending the response to the client..."); out.println("Reponse at " + new Date()); } catch (Throwable e) { System.err.println("Error processing requests"); } finally { if (s != null) { try { s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } @PostConstruct private void init() throws IOException { SimpleEurakeService sampleEurekaService = new SimpleEurakeService(); sampleEurekaService.registerWithEureka(); }
配置如下:
# note that for a purely client usage (e.g. only used to get information about other services, # there is no need for registration. This property applies to the singleton DiscoveryClient so # if you run a server that is both a service provider and also a service consumer, # then don't set this property to false. #eureka.shouldEnforceRegistrationAtInit=false ## configuration related to reaching the eureka serversSS eureka.client.service-url.defaultZone=http://localhost:7025/eureka eureka.serviceUrl.default=http://localhost:7025/eureka spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=config-server eureka.region=default spring.application.name=sampleservice #Name of the application to be identified by other services eureka.name=sampleservice #Virtual host name by which the clients identifies this service eureka.vipAddress=sampleservice #The port where the service will be running and serving requests eureka.port=8080 #For eureka clients running in eureka server, it needs to connect to servers in other zones eureka.preferSameZone=false #Change this if you want to use a DNS based lookup for determining other eureka servers. For example #of specifying the DNS entries, check the eureka-client-test.properties, eureka-client-prod.properties eureka.shouldUseDns=false eureka.us-east-1.availabilityZones=default erueka.registration.enabled=true
到此,關(guān)于“怎么把非springboot項(xiàng)目集成eureka”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
當(dāng)前標(biāo)題:怎么把非springboot項(xiàng)目集成eureka
分享URL:http://ef60e0e.cn/article/pespde.html