Eureka簡(jiǎn)介及其配置

什么是Eureka?

eureka是Netflix開發(fā)的一款基于REST(Representational State Transfer)的服務(wù)雄家。通常在AWS云服務(wù)中用于服務(wù)注冊(cè)發(fā)現(xiàn)和負(fù)載均衡等,也是SpringCloud中使用的服務(wù)注冊(cè)發(fā)現(xiàn)組件。eureka還提供有一個(gè)基于java的Client組件,用來與服務(wù)端交互卦洽,同時(shí)具有一套內(nèi)置的負(fù)載均衡器贞言,可以進(jìn)行基本的輪詢負(fù)載均衡斜棚。

客戶端-服務(wù)端通訊方式

Eureka幫助你定位到你想與之交互的那個(gè)服務(wù),但卻不會(huì)限制交互的方式和協(xié)議,換句話說弟蚀,Eureka獲取服務(wù)的協(xié)議可以是thrift蚤霞、http(s)或者任何rpc方式。

架構(gòu)

圖1 AWS部署架構(gòu)圖

Eureka通常是在AWS中部署的(當(dāng)然也不一定)义钉,上圖描述的是netflix在AWS中的部署架構(gòu)昧绣。圖中的us-east-1x指的是不同的zone,ASW將服務(wù)劃分成不同地區(qū)(region)捶闸,每個(gè)region中又有若干zone夜畴,結(jié)構(gòu)圖大致如下所示


image.png

圖1中每個(gè)zone都是一個(gè)Eureka集群。其中至少有一臺(tái)eureka server删壮,用來處理zone failure贪绘。
Eureka中注冊(cè)的服務(wù)每30s會(huì)向服務(wù)端發(fā)送一次心跳,用來告知服務(wù)端自己是否"存活"央碟,這個(gè)過程就是圖中的renew税灌,如果renew操作在重試幾次后都沒有成功,那這個(gè)服務(wù)在90s之內(nèi)就會(huì)被踢出亿虽。需要注意的是菱涤,renew信息和服務(wù)注冊(cè)信息會(huì)在多個(gè)zone間同步,任何一個(gè)zone中的客戶端都可以尋找到任意一個(gè)zone中注冊(cè)的服務(wù)信息洛勉。

Eureka 客戶端配置(application client & application service)

準(zhǔn)備

  1. jdk8及以上
  2. 下載 jar包 或通過maven引入
 <dependency>
  <groupId>com.netflix.eureka</groupId>
  <artifactId>eureka-client</artifactId>
  <version>1.1.16</version>
 </dependency>

配置

配置Eureake最簡(jiǎn)單的方式是通過properties文件粘秆,默認(rèn)情況下,erureake會(huì)加載classpath下的eureka-client.properties 收毫,Eureka會(huì)進(jìn)一步搜索是否存在特定環(huán)境(生產(chǎn)環(huán)境prod或測(cè)試環(huán)境test)的配置文件 翻擒,
并通過-Deureka.environment(注意沒有.properties后綴)來切換客戶端配置。配置示例如下

###Eureka Client configuration for Sample Eureka Client

# 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.registration.enabled=false

## configuration related to reaching the eureka servers
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8080/eureka/v2/

eureka.decoderName=JacksonJson
###Eureka Client configuration for Sample Service that register with Eureka


## configurations related to self identification for registration.
## There are other properties that can be defined, see eureka-client/../CloudInstanceConfig for full details.
# where am I deployed?
eureka.region=default

# what is my application name? (clients can query on this appName)
eureka.name=sampleRegisteringService

# what is my application virtual ip address? (clients can query on this vipAddress)
eureka.vipAddress=sampleservice.mydomain.net

# what is the port that I serve on? (Change this if port 8001 is already in use in your environment)
eureka.port=8001

## configuration related to reaching the eureka servers
eureka.preferSameZone=true
eureka.shouldUseDns=false
eureka.serviceUrl.default=http://localhost:8080/eureka/v2/

更多詳細(xì)配置請(qǐng)參考如下牛哺;
https://github.com/Netflix/eureka/blob/master/eureka-client/src/main/java/com/netflix/appinfo/EurekaInstanceConfig.java
https://github.com/Netflix/eureka/blob/master/eureka-client/src/main/java/com/netflix/discovery/EurekaClientConfig.java

Eureka 服務(wù)端配置(Eureka Server)

準(zhǔn)備

  1. jdk8及以上
  2. tomcat 6.0.10及以上
  3. 自行構(gòu)建 war或從maven central直接下載

配置

Eureka 服務(wù)內(nèi)部會(huì)啟動(dòng)一個(gè)客戶端來尋找其他Eureka服務(wù)陋气,類似于上一節(jié),需要配置好該服務(wù)的client引润。Eureka server會(huì)通過客戶端配置中的name字段來尋找擁有相同name的節(jié)點(diǎn)巩趁。之后也和上一節(jié)類似,配置相應(yīng)的eureka-server-{test,prod}.properties即可淳附。示例如下

##Eureka Client configuration for Eureka Service

# Properties based configuration for eureka client that is part of the eureka server.
# Similar eureka-client.properties configs can be used for the entire eureka ecosystem (i.e. for both the
# eureka servers as well as registering webapps), with minimal changes to application specific properties
# (see below for these).
#
# The properties specified here is mostly what the users need to change.
# All of these can be specified as a java system property with -D option (eg)-Deureka.region=us-east-1


## -----------------------------------------------------
## The below properties are application specific.
## Each new application should set these as appropriate.
## -----------------------------------------------------

# Region where the application is deployed.
# - for AWS specify one of the AWS regions
# - for other datacenters specify a arbitrary string indicating the region.
#   This is normally specified as a -D option (eg) -Deureka.region=us-east-1
eureka.region=default

# Name of the application to be identified by other services (in this case, it is the eureka service itself)
eureka.name=eureka

# Virtual host name by which the clients identifies this service (in this case, it is the eureka service itself)
eureka.vipAddress=eureka.mydomain.net

# The port where the service will be identified and will be serving requests
eureka.port=8080

# Set to false as this config is for the eureka client in the eureka server itself.
# The eureka clients running in eureka server needs to connect to servers in other zones.
#
# For other applications this should not be set (default to true) for better zone based load balancing.
eureka.preferSameZone=false

## ------------------------------------------------------------------------------
## The below properties govern how clients should connect to eureka servers.
## In general these can be the same for all clients in the same eureka ecosystem.
## ------------------------------------------------------------------------------

# Change this if you want to use a DNS based lookup for determining other eureka servers (see example below)
eureka.shouldUseDns=false

# Since shouldUseDns is false, we use the following properties to explicitly specify the route to the eureka servers
eureka.serviceUrl.default=http://localhost:8080/eureka/v2/

# for the eureka server's own client config, set on-demand update to false as it may be too quick for the REST
# resource initialization
eureka.shouldOnDemandUpdateStatusChange=false

# the default eureka server application context is /eureka/v2 if deployed with eureka.war
# Set this property for custom application context.
#eureka.eurekaServer.context=eureka/v2

## -----------------------
## AWS deployment examples
##------------------------

# for AWS deployments, availability zones can be specified to take advantage of eureka client zone affinity by
# specifying the following configurations.
# for example, if the deployment is in us-east-1 and the available zones are us-east-1a, us-east-1b and us-east-1c,

# define the region
#eureka.region=us-east-1

# notice that the region is specified as part of the config name
#eureka.us-east-1.availabilityZones=us-east-1a,us-east-1b,us-east-1c

# "eurekaServerPort" is whatever port your eureka servers are configured with
#eureka.serviceUrl.us-east-1a=http://eurekaServerHostName:eurekaServerPort/eureka/v2
#eureka.serviceUrl.us-east-1b=http://eurekaServerHostName:eurekaServerPort/eureka/v2
#eureka.serviceUrl.us-east-1c=http://eurekaServerHostName:eurekaServerPort/eureka/v2

# If shouldUseDns is set to true, the below is an example of how to configure eureka client to route to eureka servers,
# assuming for example your DNS records are setup thus:
# txt record: txt.mycompany.eureka.mydomain.com => "server1address" "server2address" "server3address"
#eureka.shouldUseDns=true
#eureka.eurekaServer.domainName=mydomain.com

## Set this only for this sample service without which starting the instance will by default wait for the default of 5 mins
eureka.waitTimeInMsWhenSyncEmpty=0

## for the example, set this to zero as we will not have peers to sync up with.
## Do not set in a real environment with multi-node eureka clusters.
eureka.numberRegistrySyncRetries=0

## for cascade loading of property files for different deployment environments and/or regions etc,
## see archaius cascade loading: https://github.com/Netflix/archaius/wiki/Deployment-context

本地環(huán)境

在本地環(huán)境運(yùn)行eureka服務(wù)時(shí)议慰,在完全啟動(dòng)前可能會(huì)有3min左右的延遲,這是因?yàn)槟J(rèn)情況下該服務(wù)會(huì)同其他peers節(jié)點(diǎn)進(jìn)行同步奴曙,但是沒有其他借點(diǎn)的情況就會(huì)重試數(shù)次别凹。這個(gè)時(shí)間可以通過配置 eureka.numberRegistrySyncRetries=0.來減少

AWS

如果需要再aws中配置服務(wù),需要額外的一部分配置洽糟,參考這里, 更多高階配置炉菲,參考這里

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末堕战,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子拍霜,更是在濱河造成了極大的恐慌辉川,老刑警劉巖赋兵,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡穷遂,警方通過查閱死者的電腦和手機(jī)带膜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門癌瘾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來更扁,“玉大人,你說我怎么就攤上這事勺鸦∷锌茫” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵祝旷,是天一觀的道長(zhǎng)履澳。 經(jīng)常有香客問我,道長(zhǎng)怀跛,這世上最難降的妖魔是什么距贷? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮吻谋,結(jié)果婚禮上忠蝗,老公的妹妹穿的比我還像新娘。我一直安慰自己漓拾,他們只是感情好阁最,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著骇两,像睡著了一般速种。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上低千,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天配阵,我揣著相機(jī)與錄音,去河邊找鬼示血。 笑死棋傍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的难审。 我是一名探鬼主播瘫拣,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼告喊!你這毒婦竟也來了麸拄?” 一聲冷哼從身側(cè)響起派昧,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎感帅,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體地淀,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡失球,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了帮毁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片实苞。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖烈疚,靈堂內(nèi)的尸體忽然破棺而出黔牵,到底是詐尸還是另有隱情,我是刑警寧澤爷肝,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布猾浦,位于F島的核電站,受9級(jí)特大地震影響灯抛,放射性物質(zhì)發(fā)生泄漏金赦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一对嚼、第九天 我趴在偏房一處隱蔽的房頂上張望夹抗。 院中可真熱鬧,春花似錦纵竖、人聲如沸漠烧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽已脓。三九已至,卻和暖如春通殃,著一層夾襖步出監(jiān)牢的瞬間摆舟,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工邓了, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留恨诱,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓骗炉,卻偏偏與公主長(zhǎng)得像照宝,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子句葵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容