什么是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)
Eureka通常是在AWS中部署的(當(dāng)然也不一定)义钉,上圖描述的是netflix在AWS中的部署架構(gòu)昧绣。圖中的us-east-1x指的是不同的zone,ASW將服務(wù)劃分成不同地區(qū)(region)捶闸,每個(gè)region中又有若干zone夜畴,結(jié)構(gòu)圖大致如下所示
圖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)備
- jdk8及以上
- 下載 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)備
配置
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.
來減少