Eureka注冊中心

Eureka

目的

通過這個(gè)服務(wù)來看eureka注冊中心的效果。

復(fù)習(xí)Spring Boot。

減少了大量配置??焖匍_發(fā)。

用Starter集成一個(gè)新框架。比如redis,web等。添加依賴,加配置文件。

嵌入式服務(wù)器,令開發(fā)和部署變的方便。

Spring Boot介紹:
https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/

代碼步驟

  1. pom.xml
  2. application.yml
  3. java代碼

看代碼。

服務(wù)注冊與發(fā)現(xiàn)

Eureka 單節(jié)點(diǎn)搭建

  1. pom.xml

    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
    有的教程中還引入spring-boot-starter-web,其實(shí)不用。因?yàn)樯厦娴囊蕾囈呀?jīng)包含了它。在pom中點(diǎn)此依賴進(jìn)去,一共點(diǎn)4次spring-cloud-netflix-eureka-server,發(fā)現(xiàn)web的依賴。
    
    
  2. application.yml

    eureka: 
      client:
        #是否將自己注冊到Eureka Server,默認(rèn)為true,由于當(dāng)前就是server,故而設(shè)置成false,表明該服務(wù)不會(huì)向eureka注冊自己的信息
        register-with-eureka: false
        #是否從eureka server獲取注冊信息,由于單節(jié)點(diǎn),不需要同步其他節(jié)點(diǎn)數(shù)據(jù),用false
        fetch-registry: false
        #設(shè)置服務(wù)注冊中心的URL,用于client和server端交流
        service-url:                      
          defaultZone: http://localhost:7900/eureka/
    
  3. application.properties

    #是否將自己注冊到Eureka Server,默認(rèn)為true,由于當(dāng)前就是server,故而設(shè)置成false,表明該服務(wù)不會(huì)向eureka注冊自己的信息
    eureka.client.register-with-eureka=false
    #是否從eureka server獲取注冊信息,由于單節(jié)點(diǎn),不需要同步其他節(jié)點(diǎn)數(shù)據(jù),用false
    eureka.client.fetch-registry=false
    #設(shè)置服務(wù)注冊中心的URL,用于client和server端交流
    eureka.client.service-url.defaultZone=http://localhost:7900/eureka/
    
  1. 代碼

    啟動(dòng)類上添加此注解標(biāo)識該服務(wù)為配置中心
    @EnableEurekaServer
    
  2. PS:Eureka會(huì)暴露一些端點(diǎn)。端點(diǎn)用于Eureka Client注冊自身,獲取注冊表,發(fā)送心跳。

  3. 簡單看一下eureka server控制臺(tái),實(shí)例信息區(qū),運(yùn)行環(huán)境信息區(qū),Eureka Server自身信息區(qū)。

Eureka 介紹

整體介紹

  1. 背景:在傳統(tǒng)應(yīng)用中,組件之間的調(diào)用,通過有規(guī)范的約束的接口來實(shí)現(xiàn),從而實(shí)現(xiàn)不同模塊間良好的協(xié)作。但是被拆分成微服務(wù)后,每個(gè)微服務(wù)實(shí)例的網(wǎng)絡(luò)地址都可能動(dòng)態(tài)變化,數(shù)量也會(huì)變化,使得原來硬編碼的地址失去了作用。需要一個(gè)中心化的組件來進(jìn)行服務(wù)的登記和管理。
  2. 概念:實(shí)現(xiàn)服務(wù)治理,即管理所有的服務(wù)信息和狀態(tài)。
注冊中心相當(dāng)于買票乘車,只看有沒有票(有沒有服務(wù)),有就去買票(獲取注冊列表),然后乘車(調(diào)用)。不必關(guān)心有多少火車在運(yùn)行。
  1. 注冊中心好處:不用關(guān)心有多少提供方。

  2. 注冊中心有哪些:Eureka,Nacos,Consul,Zookeeper等。

  3. 服務(wù)注冊與發(fā)現(xiàn)包括兩部分,一個(gè)是服務(wù)器端,另一個(gè)是客戶端。

    Server是一個(gè)公共服務(wù),為Client提供服務(wù)注冊和發(fā)現(xiàn)的功能,維護(hù)注冊到自身的Client的相關(guān)信息,同時(shí)提供接口給Client獲取注冊表中其他服務(wù)的信息,使得動(dòng)態(tài)變化的Client能夠進(jìn)行服務(wù)間的相互調(diào)用。

    Client將自己的服務(wù)信息通過一定的方式登記到Server上,并在正常范圍內(nèi)維護(hù)自己信息一致性,方便其他服務(wù)發(fā)現(xiàn)自己,同時(shí)可以通過Server獲取到自己依賴的其他服務(wù)信息,完成服務(wù)調(diào)用,還內(nèi)置了負(fù)載均衡器,用來進(jìn)行基本的負(fù)載均衡。

  4. 我們的Spring Cloud是用Eureka作為服務(wù)注冊中心。

  5. Eureka:是一個(gè)RESTful風(fēng)格的服務(wù),是一個(gè)用于服務(wù)發(fā)現(xiàn)和注冊的基礎(chǔ)組件,是搭建Spring Cloud微服務(wù)的前提之一,它屏蔽了Server和client的交互細(xì)節(jié),使得開發(fā)者將精力放到業(yè)務(wù)上。

  6. serverA從serverB同步信息,則serverB是serverA的peer。

  7. 上面例子中如果service-url為空,且register-with-eureka,fetch-registry為true,則會(huì)報(bào)錯(cuò),Cannot execute request on any known server,因?yàn)閟erver同時(shí)也是一個(gè)client,他會(huì)嘗試注冊自己,所以要有一個(gè)注冊中心url去注冊。

  8. Netflix開源的組件。包括server和client兩部分。

    https://github.com/Netflix/Eureka
    

注冊中心和微服務(wù)間的關(guān)系

《服務(wù)注冊與發(fā)現(xiàn)關(guān)系圖》

client功能

  1. 注冊:每個(gè)微服務(wù)啟動(dòng)時(shí),將自己的網(wǎng)絡(luò)地址等信息注冊到注冊中心,注冊中心會(huì)存儲(chǔ)(內(nèi)存中)這些信息。
  2. 獲取服務(wù)注冊表:服務(wù)消費(fèi)者從注冊中心,查詢服務(wù)提供者的網(wǎng)絡(luò)地址,并使用該地址調(diào)用服務(wù)提供者,為了避免每次都查注冊表信息,所以client會(huì)定時(shí)去server拉取注冊表信息到緩存到client本地。
  3. 心跳:各個(gè)微服務(wù)與注冊中心通過某種機(jī)制(心跳)通信,若注冊中心長時(shí)間和服務(wù)間沒有通信,就會(huì)注銷該實(shí)例。
  4. 調(diào)用:實(shí)際的服務(wù)調(diào)用,通過注冊表,解析服務(wù)名和具體地址的對應(yīng)關(guān)系,找到具體服務(wù)的地址,進(jìn)行實(shí)際調(diào)用。

server注冊中心功能

  1. 服務(wù)注冊表:記錄各個(gè)微服務(wù)信息,例如服務(wù)名稱,ip,端口等。

    注冊表提供 查詢API(查詢可用的微服務(wù)實(shí)例)和管理API(用于服務(wù)的注冊和注銷)。

  2. 服務(wù)注冊與發(fā)現(xiàn):注冊:將微服務(wù)信息注冊到注冊中心。發(fā)現(xiàn):查詢可用微服務(wù)列表及其網(wǎng)絡(luò)地址。

  3. 服務(wù)檢查:定時(shí)檢測已注冊的服務(wù),如發(fā)現(xiàn)某實(shí)例長時(shí)間無法訪問,就從注冊表中移除。

組件:Eureka , Consul , ZooKeeper,nacos等。

服務(wù)注冊

例子:api-listen-order

  1. pom.xml
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  1. application.yml
#注冊中心
eureka: 
  client:
    #設(shè)置服務(wù)注冊中心的URL
    service-url:                      
      defaultZone: http://root:root@localhost:7900/eureka/

ps:不想注冊,設(shè)置成false即可,實(shí)例演示結(jié)果:注冊中心沒有實(shí)例信息。找控制臺(tái)204信息也沒有找到。

spring: 
  cloud:
    service-registry:
      auto-registration:
        enabled: false

注冊成功:

DiscoveryClient_API-LISTEN-ORDER/api-listen-order:30.136.133.9:port - registration status: 204

后面源碼講手動(dòng)注冊。

PS:

Eureka Server與Eureka Client之間的聯(lián)系主要通過心跳的方式實(shí)現(xiàn)。心跳(Heartbeat)即Eureka Client定時(shí)向Eureka Server匯報(bào)本服務(wù)實(shí)例當(dāng)前的狀態(tài),維護(hù)本服務(wù)實(shí)例在注冊表中租約的有效性。

Eureka Client將定時(shí)從Eureka Server中拉取注冊表中的信息,并將這些信息緩存到本地,用于服務(wù)發(fā)現(xiàn)。

服務(wù)調(diào)用

Eureka高可用

高可用:可以通過運(yùn)行多個(gè)Eureka server實(shí)例并相互注冊的方式實(shí)現(xiàn)。Server節(jié)點(diǎn)之間會(huì)彼此增量地同步信息,從而確保節(jié)點(diǎn)中數(shù)據(jù)一致。

搭建步驟

1.準(zhǔn)備

準(zhǔn)備2個(gè)節(jié)點(diǎn)部署eureka,也可以單機(jī)部署

修改本機(jī)host文件,綁定一個(gè)主機(jī)名,單機(jī)部署時(shí)使用ip地址會(huì)有問題

2.配置文件

節(jié)點(diǎn) 1:

#是否將自己注冊到其他Eureka Server,默認(rèn)為true 需要
eureka.client.register-with-eureka=true
#是否從eureka server獲取注冊信息, 需要
eureka.client.fetch-registry=true
#設(shè)置服務(wù)注冊中心的URL,用于client和server端交流
#此節(jié)點(diǎn)應(yīng)向其他節(jié)點(diǎn)發(fā)起請求
eureka.client.serviceUrl.defaultZone=http://ek2.com:7902/eureka/
#主機(jī)名,必填
eureka.instance.hostname=ek1.com
management.endpoint.shutdown.enabled=true
#web端口,服務(wù)是由這個(gè)端口處理rest請求的
server.port=7901

節(jié)點(diǎn) 2:

#是否將自己注冊到其他Eureka Server,默認(rèn)為true 需要
eureka.client.register-with-eureka=true
#是否從eureka server獲取注冊信息, 需要
eureka.client.fetch-registry=true
#設(shè)置服務(wù)注冊中心的URL,用于client和server端交流
#此節(jié)點(diǎn)應(yīng)向其他節(jié)點(diǎn)發(fā)起請求
eureka.client.serviceUrl.defaultZone=http://ek1.com:7902/eureka/
#主機(jī)名,必填
eureka.instance.hostname=ek2.com
management.endpoint.shutdown.enabled=true
#web端口,服務(wù)是由這個(gè)端口處理rest請求的
server.port=7902

使用Spring Boot2.x Actuator監(jiān)控應(yīng)用

開啟監(jiān)控

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

默認(rèn)端點(diǎn)

Spring Boot 2.0 的Actuator只暴露了health和info端點(diǎn),提供的監(jiān)控信息無法滿足我們的需求

在1.x中有n多可供我們監(jiān)控的節(jié)點(diǎn),官方的回答是為了安全….

開啟所有端點(diǎn)

在application.yml中加入如下配置信息

*代表所有節(jié)點(diǎn)都加載

#開啟所有端點(diǎn)
management.endpoints.web.exposure.include=*

所有端點(diǎn)都開啟后的api列表

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"archaius":{"href":"http://localhost:8080/actuator/archaius","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false},"refresh":{"href":"http://localhost:8080/actuator/refresh","templated":false},"features":{"href":"http://localhost:8080/actuator/features","templated":false},"service-registry":{"href":"http://localhost:8080/actuator/service-registry","templated":false}}}

api端點(diǎn)功能

Health

會(huì)顯示系統(tǒng)狀態(tài)

{"status":"UP"}

shutdown

用來關(guān)閉節(jié)點(diǎn)

開啟遠(yuǎn)程關(guān)閉功能

management.endpoint.shutdown.enabled=true

使用Post方式請求端點(diǎn)

{

"message": "Shutting down, bye..."

}

autoconfig

獲取應(yīng)用的自動(dòng)化配置報(bào)告
beans

獲取應(yīng)用上下文中創(chuàng)建的所有Bean

configprops

獲取應(yīng)用中配置的屬性信息報(bào)告

env

獲取應(yīng)用所有可用的環(huán)境屬性報(bào)告

Mappings

獲取應(yīng)用所有Spring Web的控制器映射關(guān)系報(bào)告

info

獲取應(yīng)用自定義的信息

metrics

返回應(yīng)用的各類重要度量指標(biāo)信息

Metrics節(jié)點(diǎn)并沒有返回全量信息,我們可以通過不同的key去加載我們想要的值

metrics/jvm.memory.max

Threaddump

1.x中為dump

返回程序運(yùn)行中的線程信息

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容