SpringCloud03:Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)

本系列對(duì)應(yīng)的是尚硅谷周陽(yáng)Spring Cloud的思維導(dǎo)圖整理的筆記,用來(lái)方便自己后面的知識(shí)點(diǎn)回顧。分別以每個(gè)知識(shí)點(diǎn)作為一篇文章詳細(xì)講述。

知識(shí)點(diǎn)傳送門:
項(xiàng)目源碼

一、Eureka是什么

Eureka是Netflix的一個(gè)子模塊,也是核心模塊之一。Eureka是一個(gè)基于REST的服務(wù),用于定位服務(wù),以實(shí)現(xiàn)云端中間層服務(wù)發(fā)現(xiàn)和故障轉(zhuǎn)移。

服務(wù)注冊(cè)與發(fā)現(xiàn)對(duì)于微服務(wù)架構(gòu)來(lái)說(shuō)是非常重要的,有了服務(wù)發(fā)現(xiàn)與注冊(cè),只需要使用服務(wù)的標(biāo)識(shí)符,就可以訪問(wèn)到服務(wù),而不需要修改服務(wù)調(diào)用的配置文件了。功能類似于dubbo的注冊(cè)中心,比如Zookeeper。

二、原理講解

1.Eureka的基本架構(gòu)

Spring Cloud 封裝了 Netflix 公司開發(fā)的 Eureka 模塊來(lái)實(shí)現(xiàn)服務(wù)注冊(cè)和發(fā)現(xiàn)(請(qǐng)對(duì)比Zookeeper)。

Eureka 采用了 C-S 的設(shè)計(jì)架構(gòu)。Eureka Server 作為服務(wù)注冊(cè)功能的服務(wù)器,它是服務(wù)注冊(cè)中心。

而系統(tǒng)中的其他微服務(wù),使用 Eureka 的客戶端連接到 Eureka Server并維持心跳連接。這樣系統(tǒng)的維護(hù)人員就可以通過(guò) Eureka Server 來(lái)監(jiān)控系統(tǒng)中各個(gè)微服務(wù)是否正常運(yùn)行。SpringCloud 的一些其他模塊(比如Zuul)就可以通過(guò) Eureka Server 來(lái)發(fā)現(xiàn)系統(tǒng)中的其他微服務(wù),并執(zhí)行相關(guān)的邏輯。
請(qǐng)注意和Dubbo的架構(gòu)對(duì)比

Eureka包含兩個(gè)組件:Eureka Server和Eureka Client
Eureka Server提供服務(wù)注冊(cè)服務(wù)
各個(gè)節(jié)點(diǎn)啟動(dòng)后,會(huì)在EurekaServer中進(jìn)行注冊(cè),這樣EurekaServer中的服務(wù)注冊(cè)表中將會(huì)存儲(chǔ)所有可用服務(wù)節(jié)點(diǎn)的信息,服務(wù)節(jié)點(diǎn)的信息可以在界面中直觀的看到

EurekaClient是一個(gè)Java客戶端,用于簡(jiǎn)化Eureka Server的交互,客戶端同時(shí)也具備一個(gè)內(nèi)置的、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。在應(yīng)用啟動(dòng)后,將會(huì)向Eureka Server發(fā)送心跳(默認(rèn)周期為30秒)。如果Eureka Server在多個(gè)心跳周期內(nèi)沒有接收到某個(gè)節(jié)點(diǎn)的心跳,EurekaServer將會(huì)從服務(wù)注冊(cè)表中把這個(gè)服務(wù)節(jié)點(diǎn)移除(默認(rèn)90秒)

2.三大角色

  • Eureka Server 提供服務(wù)注冊(cè)和發(fā)現(xiàn)
  • Service Provider服務(wù)提供方將自身服務(wù)注冊(cè)到Eureka,從而使服務(wù)消費(fèi)方能夠找到
  • Service Consumer服務(wù)消費(fèi)方從Eureka獲取注冊(cè)服務(wù)列表,從而能夠消費(fèi)服務(wù)

三、構(gòu)建步驟

1.microservicecloud-eureka-7001 eureka服務(wù)注冊(cè)中心Module

1)新建microservicecloud-eureka-7001
2)修改POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
   <groupId>com.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>microservicecloud-eureka-7001</artifactId>

  <dependencies>
   <!--eureka-server服務(wù)端 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
   </dependency>
   <!-- 修改后立即生效,熱部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>

</project>

3)新建YML文件

server: 
  port: 7001
 
eureka:
  instance:
    hostname: localhost #eureka服務(wù)端的實(shí)例名稱
  client:
    register-with-eureka: false #false表示不向注冊(cè)中心注冊(cè)自己。
    fetch-registry: false #false表示自己端就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù)
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/        #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址。

4)EurekaServer7001_App主啟動(dòng)類

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer//EurekaServer服務(wù)器端啟動(dòng)類,接受其它微服務(wù)注冊(cè)進(jìn)來(lái)
public class EurekaServer7001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(EurekaServer7001_App.class, args);
  }
}

5)測(cè)試

No application available 沒有服務(wù)被發(fā)現(xiàn) O(∩_∩)O
因?yàn)闆]有注冊(cè)服務(wù)進(jìn)來(lái)當(dāng)然不可能有服務(wù)被發(fā)現(xiàn)

2.microservicecloud-provider-dept-8001將已有的部門微服務(wù)注冊(cè)進(jìn)eureka服務(wù)中心

1)修改microservicecloud-provider-dept-8001
2)修改POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
   <groupId>com.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>microservicecloud-provider-dept-8001</artifactId>

  <dependencies>
   <!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->
   <dependency>
     <groupId>com.atguigu.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   <!-- 將微服務(wù)provider側(cè)注冊(cè)進(jìn)eureka -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
   </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
   </dependency>
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
   </dependency>
   <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-core</artifactId>
   </dependency>
   <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
   <!-- 修改后立即生效,熱部署 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>

</project>

3)修改YML

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路徑
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity別名類所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
    
spring:
   application:
    name: microservicecloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 當(dāng)前數(shù)據(jù)源操作類型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅(qū)動(dòng)包
    url: jdbc:mysql://localhost:3306/cloudDB01              # 數(shù)據(jù)庫(kù)名稱
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 數(shù)據(jù)庫(kù)連接池的最小維持連接數(shù)
      initial-size: 5                                       # 初始化連接數(shù)
      max-total: 5                                          # 最大連接數(shù)
      max-wait-millis: 200                                  # 等待連接獲取的最大超時(shí)時(shí)間
      
eureka:
  client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
    service-url: 
      defaultZone: http://localhost:7001/eureka      

4)修改DeptProvider8001_App主啟動(dòng)類

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@SpringBootApplication
@EnableEurekaClient //本服務(wù)啟動(dòng)后會(huì)自動(dòng)注冊(cè)進(jìn)eureka服務(wù)中
public class DeptProvider8001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptProvider8001_App.class, args);
  }
}

5)測(cè)試

3.actuator與注冊(cè)微服務(wù)信息完善

1)主機(jī)名稱:服務(wù)名稱修改

修改YML文件

eureka:
  client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
    service-url: 
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: microservicecloud-dept8001

2)訪問(wèn)信息有IP信息提示

eureka:
  client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
    service-url: 
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: microservicecloud-dept8001   #自定義服務(wù)名稱信息
    prefer-ip-address: true     #訪問(wèn)路徑可以顯示IP地址

3)微服務(wù)info內(nèi)容詳細(xì)信息

問(wèn)題:超鏈接點(diǎn)擊服務(wù)報(bào)告ErrorPage
(1)修改microservicecloud-provider-dept-8001 POM文件

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

(2)總的父工程microservicecloud修改pom.xml添加構(gòu)建build信息

<build>
   <finalName>microservicecloud</finalName>
   <resources>
     <resource>
       <directory>src/main/resources</directory>
       <filtering>true</filtering>
     </resource>
   </resources>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-resources-plugin</artifactId>
       <configuration>
         <delimiters>
          <delimit>$</delimit>
         </delimiters>
       </configuration>
     </plugin>
   </plugins>
  </build>

(4)修改microservicecloud-provider-dept-8001

info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

4.eureka自我保護(hù)

1)故障現(xiàn)象

3.png
4.png

2)自我保護(hù)
什么是自我保護(hù)模式?

默認(rèn)情況下,如果EurekaServer在一定時(shí)間內(nèi)沒有接收到某個(gè)微服務(wù)實(shí)例的心跳,EurekaServer將會(huì)注銷該實(shí)例(默認(rèn)90秒)。但是當(dāng)網(wǎng)絡(luò)分區(qū)故障發(fā)生時(shí),微服務(wù)與EurekaServer之間無(wú)法正常通信,以上行為可能變得非常危險(xiǎn)了——因?yàn)槲⒎?wù)本身其實(shí)是健康的,此時(shí)本不應(yīng)該注銷這個(gè)微服務(wù)。Eureka通過(guò)“自我保護(hù)模式”來(lái)解決這個(gè)問(wèn)題——當(dāng)EurekaServer節(jié)點(diǎn)在短時(shí)間內(nèi)丟失過(guò)多客戶端時(shí)(可能發(fā)生了網(wǎng)絡(luò)分區(qū)故障),那么這個(gè)節(jié)點(diǎn)就會(huì)進(jìn)入自我保護(hù)模式。一旦進(jìn)入該模式,EurekaServer就會(huì)保護(hù)服務(wù)注冊(cè)表中的信息,不再刪除服務(wù)注冊(cè)表中的數(shù)據(jù)(也就是不會(huì)注銷任何微服務(wù))。當(dāng)網(wǎng)絡(luò)故障恢復(fù)后,該Eureka Server節(jié)點(diǎn)會(huì)自動(dòng)退出自我保護(hù)模式。

在自我保護(hù)模式中,Eureka Server會(huì)保護(hù)服務(wù)注冊(cè)表中的信息,不再注銷任何服務(wù)實(shí)例。當(dāng)它收到的心跳數(shù)重新恢復(fù)到閾值以上時(shí),該Eureka Server節(jié)點(diǎn)就會(huì)自動(dòng)退出自我保護(hù)模式。它的設(shè)計(jì)哲學(xué)就是寧可保留錯(cuò)誤的服務(wù)注冊(cè)信息,也不盲目注銷任何可能健康的服務(wù)實(shí)例。一句話講解:好死不如賴活著

綜上,自我保護(hù)模式是一種應(yīng)對(duì)網(wǎng)絡(luò)異常的安全保護(hù)措施。它的架構(gòu)哲學(xué)是寧可同時(shí)保留所有微服務(wù)(健康的微服務(wù)和不健康的微服務(wù)都會(huì)保留),也不盲目注銷任何健康的微服務(wù)。使用自我保護(hù)模式,可以讓Eureka集群更加的健壯、穩(wěn)定。

在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false 禁用自我保護(hù)模式。
一句話:某時(shí)刻某一個(gè)微服務(wù)不可用了,eureka不會(huì)立刻清理,依舊會(huì)對(duì)該微服務(wù)的信息進(jìn)行保存

四、集群配置

1.新建microservicecloud-eureka-7002/microservicecloud-eureka-7003
2.按照7001為模板粘貼POM
3.修改7002和7003的主啟動(dòng)類
4.修改映射配置

  • 找到C:\Windows\System32\drivers\etc路徑下的hosts文件
  • 修改映射配置添加進(jìn)hosts文件
    • 127.0.0.1 eureka7001.com
    • 127.0.0.1 eureka7002.com
    • 127.0.0.1 eureka703.com

5.3臺(tái)eureka服務(wù)器的yml配置

  • 7001
server: 
  port: 7001
 
eureka: 
  instance:
    hostname: eureka7001.com #eureka服務(wù)端的實(shí)例名稱
  client: 
    register-with-eureka: false     #false表示不向注冊(cè)中心注冊(cè)自己。
    fetch-registry: false     #false表示自己端就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù)
    service-url: 
      #單機(jī) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址(單機(jī))。
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  • 7002
server: 
  port: 7002

eureka: 
  instance:
    hostname: eureka7002.com #eureka服務(wù)端的實(shí)例名稱
  client: 
    register-with-eureka: false     #false表示不向注冊(cè)中心注冊(cè)自己。
    fetch-registry: false     #false表示自己端就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù)
    service-url: 
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址。
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
  • 7003
server: 
  port: 7003

eureka: 
  instance:
    hostname: eureka7003.com #eureka服務(wù)端的實(shí)例名稱
  client: 
    register-with-eureka: false     #false表示不向注冊(cè)中心注冊(cè)自己。
    fetch-registry: false     #false表示自己端就是注冊(cè)中心,我的職責(zé)就是維護(hù)服務(wù)實(shí)例,并不需要去檢索服務(wù)
    service-url: 
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊(cè)服務(wù)都需要依賴這個(gè)地址。
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

6.microservicecloud-provider-dept-8001微服務(wù)發(fā)布到上面3臺(tái)eureka集群配置中

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml  #mybatis所在路徑
  type-aliases-package: com.atguigu.springcloud.entities #entity別名類
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml #mapper映射文件
    
spring:
   application:
    name: microservicecloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: 123456
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200
      
eureka:
  client: #客戶端注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: microservicecloud-dept8001   #自定義服務(wù)名稱信息
    prefer-ip-address: true     #訪問(wèn)路徑可以顯示IP地址
      
info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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