什么是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)
准备
- jdk8及以上
- 下载 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)
准备
配置
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.
来减少
网友评论