服務注冊Eureka基礎


1 微服務的注冊中心

1.1 概述

  • 注冊中心可以說是微服務架構中的“通訊錄”,它記錄了服務和服務地址的映射關系。在分布式架構中,服務會注冊到這里,當服務需要調用其他服務的時候,就從這里找到服務的地址,進行調用。

微服務注冊中心概述

1.2 注冊中心的主要作用

  • 服務注冊中心(簡稱注冊中心)是微服務架構非常重要的一個組件,在微服務架構里面起到了協調者的作用。注冊中心一般包含如下幾個功能:
  • 1️⃣服務發現:
    • 服務注冊/反注冊:保存服務提供者和服務調用者的信息。
    • 服務訂閱/取消訂閱:服務調用者訂閱服務提供者的信息,最好有實時推送的功能。
    • 服務路由(可選):具有篩選整合服務提供者的能力。
  • 2️⃣服務配置:
    • 配置訂閱:服務提供者和服務調用者訂閱服務相關的配置。
    • 配置下發:主動將配置推送給服務提供者和服務調用者。
  • 3️⃣服務健康檢測:
    • 檢測服務提供者的健康狀況。

1.3 常見的注冊中心

1.3.1 zookeeper

  • zookeeper是一個分布式服務框架,是Apache Hadoop的一個子項目,它主要是用來解決分布式應用經常遇到的一些數據管理問題,如統一命名服務、狀態同步服務、集群管理、分布式應用配置項的管理等等。
  • 簡單的說,zookeeper=文件系統+監聽通知機制。

1.3.2 Eureka

  • Eureka是在Java語言上,基於RESTful API開發的服務注冊和發現組件,SpringCloud Netflix中的重要組件。

1.3.3 Consul

  • Consul是HashiCorp基於Go語言開發的支持多數據中心分布式高可用的服務發現和注冊軟件,采用Raft算法保證服務的一致性,且支持健康檢查。

1.3.4 Nacos

  • Nacos是一個更基於構建雲原生應用的動態服務發現、配置管理和服務管理平台。簡單的說,Nacos就是注冊中心+配置中心的組合,提供簡單易用的特性集,幫助我們解決微服務開發必會涉及到的服務注冊和發現、服務配置、服務管理等問題。Nacos還是SpringCloud Alibaba組件之一,負責服務注冊和發現。

1.3.5 總結

組件名 語言 CAP 一致性算法 服務健康檢查 對外暴露接口
Eureka Java AP 可以配置支持 HTTP
Consul Go CP Raft 支持 HTTP/DNS
Zookeeper Java CP Paxos 支持 客戶端
Nacos Java AP Raft 支持 HTTP

2 Eureka的概述

2.1 Eureka的基礎知識

  • Eureka是Netflix開發的服務發現框架,SpringCloud將它集成到自己的子項目spring-cloud-netflix中,實現SpringCloud的服務發現功能。

Eureka基礎知識

  • 上圖簡要描述了Eureka的基本架構,由3個角色組成:
  • 1️⃣Eureka Server:提供服務注冊和發現。
  • 2️⃣Service Provider:服務提供方(將自身服務注冊到Eureka,從而使得服務消費方能夠找到)。
  • 3️⃣Service Consumer:服務消費方(從Eureka獲取注冊服務列表,從而能夠消費服務)。

2.2 Eureka的交互流程和原理

Eureka交互原理

  • 由上圖可知,Eureka包含兩個組件:Eureka Server和Eureka Client,它們的作用如下:
    • 1️⃣Eureka Client是一個Java客戶端,用於簡化和Eureka Server的交互。
    • 2️⃣Eureka Server提供服務發現能力,各個微服務啟動時,會通過Eureka Client向Eureka Server進行注冊自己的信息(例如網絡信息),Eureka Server會存儲該服務的信息。
    • 3️⃣微服務啟動后,會周期性的向Eureka Server發送心跳(默認周期為30秒)以續約自己的信息。如果Eureka Server在一定時間內(默認為90秒)沒有接收到某個微服務節點的心跳,Eureka Server將會注銷該微服務節點。
    • 4️⃣每個Eureka Server同時也是Eureka Client,多個Eureka Server之間通過復制的方式完成服務注冊表的同步。
    • 5️⃣Eureka Client會緩存Eureka Server中的信息。即使所有的Eureka Server節點都宕機,服務消費者依然可以使用緩存中的信息找到服務提供者。
  • 綜上,Eureka通過心跳檢測、健康檢查和客戶端緩存等機制,提高了系統的靈活性、可伸縮性和可用性。

3 Eureka的使用步驟

  • 1️⃣搭建Eureka Server。
    • 創建工程(eureka_server)。
    • 導入Eureka對應的坐標。
    • 配置application.yml。
    • 配置啟動類。
  • 2️⃣將服務提供者注冊到Eureka Server上。
  • 3️⃣服務消費者通過注冊中心獲取服務列表,並調用。

4 搭建Eureka Server(注冊中心)

4.1 搭建Eureka Server(注冊中心)

4.1.1 在pom.xml中導入相關jar包的坐標

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>spring_cloud_demo</artifactId>
        <groupId>org.sunxiaping</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka_service</artifactId>

    <dependencies>
        <!--   導入Eureka Server對應的坐標     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>


</project>

4.1.2 配置yml

  • application.yml
server:
  port: 9000 #端口

#配置Eureka Server
eureka:
  instance:
    # 主機地址名稱
    hostname: localhost
  client:
    register-with-eureka: false # 是否將自己注冊到注冊中心
    fetch-registry: false # 是否從Eureka中獲取服務列表
    service-url:  # 配置暴露給Eureka Client的請求地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4.1.3 配置啟動類

  • EurekaApplication.java
package com.sunxiaping.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author 許威威
 * @version 1.0
 */
@SpringBootApplication
@EnableEurekaServer //開啟Eureka Server
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

4.2 服務中心管理平台

  • 打開瀏覽器訪問http://localhost:9000/,即可進入Eureka Server內置的管理控制台,顯示效果如下:

服務注冊中心管理后台

5 服務注冊到Eureka注冊中心

5.1 在商品微服務中引入Eureka Client的坐標

  • 修改部分:
<!--   導入Eureka Client對應的坐標     -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 完整部分:
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>spring_cloud_demo</artifactId>
        <groupId>org.sunxiaping</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product_service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--   導入Eureka Client對應的坐標     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.2.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

5.2 修改yml配置文件

  • application.yml
server:
  port: 9001 # 微服務的端口號

spring:
  application:
    name: service-product # 微服務的名稱
  datasource:
    url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    generate-ddl: true
    show-sql: true
    open-in-view: true
    database: mysql

# 配置 eureka
eureka:
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/

5.3 配置啟動類

  • ProductApplication.java
package com.sunxiaping.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient //開啟Eureka Client
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
}

5.4 測試

服務提供者注冊到Eureka Server

5.5 actuator與注冊微服務信息完善

5.5.1 主機名:服務名稱修改

  • 當前問題:

主機名稱服務名稱修改之前

  • 修改服務提供者的application.yml文件。
  • 修改部分:
# 配置 eureka
eureka:
  instance:
  	# 主機名稱:服務名稱修改,其實就是向eureka server中注冊的實例id
    instance-id: service-product:9001
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/
  • 完整部分:
server:
  port: 9001 # 微服務的端口號

spring:
  application:
    name: service-product # 微服務的名稱
  datasource:
    url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    generate-ddl: true
    show-sql: true
    open-in-view: true
    database: mysql

# 配置 eureka
eureka:
  instance:
    # 主機名稱:服務名稱修改,其實就是向eureka server中注冊的實例id
    instance-id: service-product:9001
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/

  • 修改之后:

主機名稱服務名稱修改之后

5.5.2 顯示IP信息

  • 當前問題:

顯示IP信息修改之前

  • 修改服務提供者的application.yml文件。
  • 修改部分:
# 配置 eureka
eureka:
  instance:
    # 主機名稱:服務名稱修改,其實就是向eureka server中注冊的實例id
    instance-id: service-product:9001
    # 顯示IP信息
    prefer-ip-address: true
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/
  • 完整部分:
server:
  port: 9001 # 微服務的端口號

spring:
  application:
    name: service-product # 微服務的名稱
  datasource:
    url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    generate-ddl: true
    show-sql: true
    open-in-view: true
    database: mysql

# 配置 eureka
eureka:
  instance:
    # 主機名稱:服務名稱修改,其實就是向eureka server中注冊的實例id
    instance-id: service-product:9001
    # 顯示IP信息
    prefer-ip-address: true
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/

  • 修改之后:

顯示IP信息修改之后

5.5.3 微服務info內容詳細信息

  • 當前問題:點擊超鏈接報告ErrorPage

微服務info內容詳細信息之前

  • 修改服務提供者的pom.xml。
  • 修改部分:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 完整部分:
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>spring_cloud_demo</artifactId>
        <groupId>org.sunxiaping</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>product_service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--   導入Eureka Client對應的坐標     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.2.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 在總工程添加build信息。

  • 修改部分:

<build>
    <finalName>spring_cloud_demo</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>
  • 完整部分:
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <packaging>pom</packaging>
    <modules>
        <module>product_service</module>
        <module>spring_cloud_common</module>
        <module>order_service</module>
        <module>eureka_service</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>


    <groupId>org.sunxiaping</groupId>
    <artifactId>spring_cloud_demo</artifactId>
    <version>1.0</version>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


    <build>
        <finalName>spring_cloud_demo</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>

</project>
  • 修改服務提供者的application.yml文件。
  • 修改部分:
# 微服務info內容詳細信息
info:
  app.name: xxx
  company.name: xxx
  build.artifactId: $project.artifactId$
  build.version: $project.version$
  • 完整部分:
server:
  port: 9001 # 微服務的端口號

spring:
  application:
    name: service-product # 微服務的名稱
  datasource:
    url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    generate-ddl: true
    show-sql: true
    open-in-view: true
    database: mysql

# 配置 eureka
eureka:
  instance:
    # 主機名稱:服務名稱修改,其實就是向eureka server中注冊的實例id
    instance-id: service-product:9001
    # 顯示IP信息
    prefer-ip-address: true
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/

# 微服務info內容詳細信息
info:
  app.name: xxx
  company.name: xxx
  build.artifactId: $project.artifactId$
  build.version: $project.version$
  • 修改之后:

微服務info內容詳細信息之后

6 Eureka的元數據

6.1 概述

  • Eureka的元數據有兩種:標准元數據和自定義元數據。
  • 1️⃣標注元數據:主機名、IP地址、端口號、狀態頁和健康檢查等信息,這些信息都會發布在服務注冊表中,用於服務之間的調用。
  • 2️⃣自定義元數據:可以使用eureka.instance.metadata-map配置,符合KEY/VALUE的存儲格式。這些元數據可以在遠程客戶端中訪問。

6.2 服務消費者通過注冊中心獲取服務列表,並調用

6.2.1 在訂單微服務中引入Eureka Client的坐標

  • 修改部分:
<!--   導入Eureka Client對應的坐標     -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 完整部分:
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>spring_cloud_demo</artifactId>
        <groupId>org.sunxiaping</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order_service</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--   導入Eureka Client對應的坐標     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

</project>

6.2.2 修改yml配置文件

  • application.yml
server:
  port: 9002 # 微服務的端口號

spring:
  application:
    name: service-order # 微服務的名稱
  datasource:
    url: jdbc:mysql://192.168.1.57:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    generate-ddl: true
    show-sql: true
    open-in-view: true
    database: mysql

# 配置Eureka
eureka:
  instance:
    # 實例的名稱
    instance-id: service-order:9002
    # 顯示IP信息
    prefer-ip-address: true
  client:
    service-url: # Eureka Server的地址
      defaultZone: http://localhost:9000/eureka/

# 微服務info內容詳細信息
info:
  app.name: xxx
  company.name: xxx
  build.artifactId: $project.artifactId$
  build.version: $project.version$

6.2.3 修改Controller

  • OrderController.java
package com.sunxiaping.order.controller;


import com.sunxiaping.order.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
@RequestMapping(value = "/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * SpringCloud提供的獲取元數據的工具類
     * 調用方法獲取服務的元數據
     */
    @Autowired
    private DiscoveryClient discoveryClient;


    /**
     * 通過訂單系統,調用商品微服務根據id查詢商品信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/buy/{id}")
    public Product buy(@PathVariable(value = "id") Long id) {
        List<ServiceInstance> instanceList = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instanceList.get(0);
        URI uri = serviceInstance.getUri();

        if (null != uri) {
            return restTemplate.getForObject(uri.toString() + "/product/findById/" + id, Product.class);
        }
        return null;
    }
}

7 Eureka中的自我保護

7.1 概述

  • 微服務第一次注冊成功后,每30秒會發送一次心跳將服務的實例信息注冊到注冊中心。通知Eureka Server該實例依然存在。如果超過90秒沒有發送心跳,則服務器將從注冊中心將此服務移除。
  • Eureka Server在運行期間,會統計心跳失敗的比例在15分鍾內是否低於85%,如果出現低於85%的情況(在單機調試的時候很容易滿足,實際在生產環境上通常是由於網絡不穩定導致),那么Eureka就會認為客戶端和注冊中心出現了網絡故障,此時會做如下的處理:
    • 1️⃣Eureka不再從注冊列表中刪除因為長時間沒有收到心跳而應該過期的服務。
    • 2️⃣Eureka仍然能夠接受新服務的注冊和查詢請求,但是不會同步到其他節點上(保證當前節點依然可用)。
    • 3️⃣當網絡穩定的時候,當前實例新的注冊信息會被同步到其他節點中。
  • 驗證自我保護機制開啟,並不會馬上呈現在web后台上,而是默認需要等待5分鍾(可以通過eureka.server.wait-time-in-ms-when-sync-empty配置),即5分鍾后你就會看到如下所示的提示信息:

Eureka的自我保護機制

7.2 如何關閉自我保護機制

  • 通過設置eureka.enableSelfPreservation=false來關閉自我保護機制。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM