Eureka深入理解
生活随笔
收集整理的這篇文章主要介紹了
Eureka深入理解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
客戶端負載均衡Ribbon,聲明式的Http Client Feign,那我們回到Eureka上面去,我們對Eureka進行一些補充,對他進行一些更深入的理解,我們來看Eureka的文檔Service Discovery:Eureka Clientshttps://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html1.4 Status Page and Health Indicator配置狀態頁eureka:instance:statusPageUrlPath: ${server.servletPath}/infohealthCheckUrlPath: ${server.servletPath}/health健康檢查的一些頁面,我個人絕對沒有必要講,因為正常我們沒有修改路徑的需求,我們應該把精力放到業務上面去,1.5 Registering a Secure ApplicationIf your app wants to be contacted over HTTPS, you can set two flags in the EurekaInstanceConfig:我們沒有證書,也沒有域名,但是我之前用域名有測試過,大致按照這個文檔配置的話就OK了1.6 Eureka’s Health Checks健康檢查eureka:client:healthcheck:enabled: trueeureka.client.healthcheck.enabled=true should only be set in application.yml. Setting the value in bootstrap.yml causes undesirable side effects, such as registering in Eureka with an UNKNOWN status.健康檢查只能配置到application.yml,如果你把它放到bootstrap.yml,bootstrap.yml是什么呢,如果你玩過SpringBoot的話,他跟application.yml有一定的區別,就是加在順序會有一定的區別,它會造成一些問題,1.7 Eureka Metadata for Instances and ClientsEureka的元數據,值得花一點時間了解Eureka源數據的原理,像主機名,IP,狀態也,以及健康檢查,就是標準的源數據,這些都會發布在你服務注冊表中,IP端口會注冊到Eureka里面,我們可以通過eureka.instance.metadataMap,自定義一些源數據,但是他并不會改變客戶端的行為,除非他知道是什么含義,自定義的東西他本身怎么會知道呢,沒有加處理的代碼,下面描述了幾個特殊情況It is worth spending a bit of time understanding how the Eureka metadata works, so you can use it in a way that makes sense in your platform. There is standard metadata for information such as hostname, IP address, port numbers, the status page, and health check. These are published in the service registry and used by clients to contact the services in a straightforward way. Additional metadata can be added to the instance registration in the eureka.instance.metadataMap, and this metadata is accessible in the remote clients. In general, additional metadata does not change the behavior of the client, unless the client is made aware of the meaning of the metadata. There are a couple of special cases, described later in this document, where Spring Cloud already assigns meaning to the metadata map.1.7.1 Using Eureka on Cloud Foundryapplication.yml. eureka:instance:hostname: ${vcap.application.uris[0]}nonSecurePort: 80端口號也是源數據1.7.2 Using Eureka on AWS我們也沒有把它放到AWS上面@Bean
@Profile("!default")
public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) {EurekaInstanceConfigBean b = new EurekaInstanceConfigBean(inetUtils);AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild("eureka");b.setDataCenterInfo(info);return b;
}1.7.3 Changing the Eureka Instance ID其實我們已經講過了,只是沒有說是源數據而已,如果你在Cloud Foundry上面玩的話,${vcap.application.instance_id這個東西會生成一個隨機的值With the metadata shown in the preceding example and multiple service instances deployed on localhost, the random value is inserted there to make the instance unique. In Cloud Foundry, the vcap.application.instance_id is populated automatically in a Spring Boot application,so the random value is not needed.eureka.instance.metadataMap這個東西我們怎么去自定義,我想要自定義一個源數據怎么玩呢,eureka:client:healthcheck:enabled: trueinstance:metadata-map:zone: ABC # eureka可以理解的元數據leon: BBC # 不會影響客戶端行為lease-renewal-interval-in-seconds: 510.40.8.152:8761/eureka/apps/microservice-simple-provider-user可以來這里看http://10.40.8.152:8761/eureka/apps/microservice-simple-provider-user<metadata>
<zone>ABC</zone>
<leon>BBC</leon>
</metadata>
eureka:client:healthcheck:enabled: trueserviceUrl:defaultZone: http://admin:1234@10.40.8.152:8761/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}metadata-map:zone: ABC leon: BBC
Using Ribbon with Eurekahttps://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.1.RELEASE/eureka.instance.metadataMap.zonemetadata-map:zone: ABC leon: BBC 配這個不會影響客戶端行為,zone: ABC這個會影響到客戶端的行為了,@Autowired
private EurekaClient eurekaClient;Using the EurekaClienthttps://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.1.RELEASE/我們也已經寫過實例了,Don’t use the EurekaClient in @PostConstruct method or in a @Scheduled method (or anywhere where the ApplicationContext might not be started yet). It is initialized in a SmartLifecycle (with phase=0) so the earliest you can rely on it being available is in another SmartLifecycle with higher phase.Alternatives to the native Netflix EurekaClient@Autowired
private DiscoveryClient discoveryClient;public String serviceUrl() {List<ServiceInstance> list = discoveryClient.getInstances("STORES");if (list != null && list.size() > 0 ) {return list.get(0).getUri();}return null;
}其實我們也已經寫過這個實例了@GetMapping("/instance-info")
public ServiceInstance showInfo() {ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();return localServiceInstance;
}org.springframework.cloud.client.discovery.DiscoveryClient你不一定要使用Eureka的原生APIWhy is it so Slow to Register a Service?注冊服務為什么這么慢呢Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick with the default because there are some computations internally in the server that make assumptions about the lease renewal period.服務他成為一個實例的時候,他還涉及到注冊表的心跳周期,默認是30秒,Eureka client和Eureka Server兩端的數據,源數據一致之前呢,當然他有本地緩存了,client的本地緩存,和服務器端的本地緩存,兩邊一致之前,它是不會被發現的,所以他會花去3個心跳周期,你可以使用這個配置,eureka.instance.leaseRenewalIntervalInSeconds,縮短這個周期,然后會加快連到客戶端的過程,在生產中最好使用默認值,這句其實是最重要的,因為在服務器內部有一些計算,會對這個續約做一些假設,那意味著什么呢,這個配置是讓我們測試用的,意思也是比較好理解的,eureka: instance:lease-renewal-interval-in-seconds: 5默認值是30,/*** Indicates how often (in seconds) the eureka client needs to send heartbeats to* eureka server to indicate that it is still alive. If the heartbeats are not* received for the period specified in leaseExpirationDurationInSeconds, eureka* server will remove the instance from its view, there by disallowing traffic to this* instance.** Note that the instance could still not take traffic if it implements* HealthCheckCallback and then decides to make itself unavailable.*/
private int leaseRenewalIntervalInSeconds = 30;org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean.leaseRenewalIntervalInSeconds單位是秒,它會把心跳的周期,默認是30秒,那我把它改成5秒,這個樣子心跳的頻率就變快了,因為客戶端和服務器端的緩存要一樣的,他才會注冊成功,也就是要同步才會注冊為一個實例,那這個過程是不是也會加快,因為他可能要3個心跳,我5秒一個心跳,那最多就是15秒,原生本來是30秒,原生的間隔是30秒,那就得90秒,但是生產中不要用https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.1.RELEASE/Service Discovery: Eureka Server@SpringBootApplication
@EnableEurekaServer
public class Application {public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}@EnableEurekaServer然后高可用High Availability, Zones and Regions這個很實用,一旦要上生產,我們一定要高可用,Eureka服務器沒有后端的存儲,他沒有數據庫,但是注冊表中必須要發送心跳,以保持注冊更新The Eureka server does not have a backend store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory). 服務注冊表在內存里面,不一定在內存,那他們怎么樣玩呢,服務實例必須要發送心跳,以保持注冊更新,client要發送心跳,保持注冊更新,客戶端還具有Eureka注冊的內存緩存,客戶端他有一個本地緩存,默認情況下,每個Eureka服務器也是一個Eureka客戶端,這我之前也有說過,需要一個服務URL來定位對等體,我可能至少需要一個URL,我要定位另外一個Eureka,那這個時候我可能定位了兩個,比如這是1,這是2,這是3,我可以把1注冊到2,那2就是1的對等體,那2我又注冊到3,那3就是2的對等體,他就說至少一個,我1上面注冊3,那3都是他們的對等體,否則一個都沒有的話,他是不是孤立了,每一個Eureka Server也是一個Eureka ClientClients also have an in-memory cache of eureka registrations (so they don’t have to go to the registry for every single request to a service).By default every Eureka server is also a Eureka client and requires (at least one) service URL to locate a peer. If you don’t provide it the service will run and work, but it will shower your logs with a lot of noise about not being able to register with the peer.意味著他們也是通過心跳進行同步,這是一個Eureka Client,或者說是我們的User服務,他也是通過發心跳續約,每30秒續約一下,他們之間其實也是的Standalone Mode單擊模式,我們一開始講的就是單擊模式Peer Awareness也就是傳說中的高可用模式,我們來看一下高可用模式怎么玩
?
總結
以上是生活随笔為你收集整理的Eureka深入理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fegion-4解决Fegion第一次请
- 下一篇: Eureka常用配置详解