Eureka简介及其配置

什么是Eureka?

eureka是Netflix开发的一款基于REST(Representational State Transfer)的服务。通常在AWS云服务中用于服务注册发现和负载均衡等,也是SpringCloud中使用的服务注册发现组件。eureka还提供有一个基于java的Client组件,用来与服务端交互,同时具有一套内置的负载均衡器,可以进行基本的轮询负载均衡。

客户端-服务端通讯方式

Eureka帮助你定位到你想与之交互的那个服务,但却不会限制交互的方式和协议,换句话说,Eureka获取服务的协议可以是thrift、http(s)或者任何rpc方式。

架构

图1 AWS部署架构图

Eureka通常是在AWS中部署的(当然也不一定),上图描述的是netflix在AWS中的部署架构。图中的us-east-1x指的是不同的zone,ASW将服务划分成不同地区(region),每个region中又有若干zone,结构图大致如下所示


image.png

图1中每个zone都是一个Eureka集群。其中至少有一台eureka server,用来处理zone failure。
Eureka中注册的服务每30s会向服务端发送一次心跳,用来告知服务端自己是否"存活",这个过程就是图中的renew,如果renew操作在重试几次后都没有成功,那这个服务在90s之内就会被踢出。需要注意的是,renew信息和服务注册信息会在多个zone间同步,任何一个zone中的客户端都可以寻找到任意一个zone中注册的服务信息。

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

准备

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

配置

配置Eureake最简单的方式是通过properties文件,默认情况下,erureake会加载classpath下的eureka-client.properties ,Eureka会进一步搜索是否存在特定环境(生产环境prod或测试环境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/

更多详细配置请参考如下;
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 服务端配置(Eureka Server)

准备

  1. jdk8及以上
  2. tomcat 6.0.10及以上
  3. 自行构建 war或从maven central直接下载

配置

Eureka 服务内部会启动一个客户端来寻找其他Eureka服务,类似于上一节,需要配置好该服务的client。Eureka server会通过客户端配置中的name字段来寻找拥有相同name的节点。之后也和上一节类似,配置相应的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

本地环境

在本地环境运行eureka服务时,在完全启动前可能会有3min左右的延迟,这是因为默认情况下该服务会同其他peers节点进行同步,但是没有其他借点的情况就会重试数次。这个时间可以通过配置 eureka.numberRegistrySyncRetries=0.来减少

AWS

如果需要再aws中配置服务,需要额外的一部分配置,参考这里, 更多高阶配置,参考这里

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容