1. 程式人生 > >4、Spring Cloud Eureka

4、Spring Cloud Eureka

1.Eureka簡介

(1).CAP簡介

  CAP原則又稱CAP定理,指的是在一個分散式系統中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分割槽容錯性),三者不可兼得。

https://baike.baidu.com/item/CAP%E5%8E%9F%E5%88%99/5712863?fr=aladdin

(2).Eureka簡介

    Eureka是Netflix的一個子模組,也是核心模組之一。Eureka是一個基於REST的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。服務註冊與發現對於微服務架構來說是非常重要的,有了服務發現與註冊,只需要使用服務的識別符號,就可以訪問到服務,而不需要修改服務呼叫的配置檔案了。功能類似於dubbo的註冊中心,比如Zookeeper。

https://github.com/Netflix/eureka

 

2.Eureka原理

(1).Eureka原理

[1].Eureka原理簡介

  Spring Cloud 封裝了 Netflix 公司開發的 Eureka 模組來實現服務註冊和發現(請對比Zookeeper)。

  Eureka 採用了 C-S 的設計架構。Eureka Server 作為服務註冊功能的伺服器,它是服務註冊中心。

  而系統中的其他微服務,使用 Eureka Client(Eureka客戶端)連線到 Eureka Server並維持心跳連線。這樣系統的維護人員就可以通過 Eureka Server 來監控系統中各個微服務是否正常執行。SpringCloud 的一些其他模組(比如Zuul)就可以通過 Eureka Server 來發現系統中的其他微服務,並執行相關的邏輯。

  Eureka和Dubbo的架構對比

 

 

[2].Eureka元件

  Eureka包含兩個元件:Eureka Server和Eureka Client,Eureka Server提供服務註冊服務;各個節點啟動後,會在EurekaServer中進行註冊,這樣urekaServer中的服務登錄檔中將會儲存所有可用服務節點的資訊,服務節點的資訊可以在介面中直觀的看到。

  EurekaClient是一個Java客戶端,用於簡化Eureka Server的互動,客戶端同時也 具備一個內建的、使用輪詢(round-robin)負載演算法的負載均衡器。在應用啟動後,將會向Eureka Server傳送心跳(預設週期為30秒)。如果Eureka Server在多個心跳週期內沒有接收到某個節點的心跳,EurekaServer將會從服務登錄檔中把這個服務節點移除(預設90秒)。

(2).Eureka角色

  Eureka Server 提供服務註冊和發現。

  Service Provider服務提供方將自身服務註冊到Eureka,從而使服務消費方能夠找到。

  Service Consumer服務消費方從Eureka獲取註冊服務列表,從而能夠消費服務。

(3).Eureka工程

  總父工程

  通用模組api

  服務提供者Provider

  服務消費者Consumer

3.Eureka架構步驟

(1).建立服務註冊中心eureka

  microservicecloud-eureka-7001 eureka服務註冊中心Module

[1].新建工程  

 

 

 

[2].配置pom.xml

<?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>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

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

    <dependencies>

        <!--eureka-server服務端 -->

        <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].配合application.yml

 server:

  port: 7001

 

eureka:

  instance:

    hostname: localhost #eureka服務端的例項名稱

  client:

    register-with-eureka: false #false表示不向註冊中心註冊自己。

    fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務

    service-url:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/        #設定與Eureka Server互動的地址查詢服務和註冊服務都需要依賴這個地址。

[4].EurekaServer7001_App主啟動類

package com.hosystem.springcloud;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

 

@SpringBootApplication

@EnableEurekaServer //EurekaServer伺服器端啟動類,接受其它微服務註冊進來

public class EurekaServer7001_App

{

    public static void main(String[] args)

    {

        SpringApplication.run(EurekaServer7001_App.class, args);

    }

}

[5].Eureka Test

http://localhost:7001/

 

  No application available(沒有服務被發現),因為沒有註冊服務進來當然不可能有服務被發現.

(2).註冊eureka服務中心

  microservicecloud-provider-dept-8001將已有的部門微服務註冊進eureka服務中心。

[1].修改microservicecloud-provider-dept-8001

①.修改pom.xml

  新新增的部分:

   <!-- 將微服務provider側註冊進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>

  完整的部分:

<?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>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

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

 

    <dependencies>

        <dependency><!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->

            <groupId>com.hosystem</groupId>

            <artifactId>microservicecloud-api</artifactId>

            <version>${project.version}</version>

        </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>

 

        <!-- 將微服務provider側註冊進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>

    </dependencies>

 

</project>

②.修改application.yml

  新新增的部分:

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

  完整的部分:

server:

  port: 8001

 

mybatis:

  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑

  type-aliases-package: com.hosytem.springcloud.entities    # 所有Entity別名類所在包

  mapper-locations:

  - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案

 

#name spring.application.name=microservicecloud-dept  很重要很重要很重要

spring:

   application:

    name: microservicecloud-dept

   datasource:

    type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別

    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅動包

    url: jdbc:mysql://192.168.188.188:3306/cloudDB01              # 資料庫名稱

    username: root

    password: 123456

    dbcp2:

      min-idle: 5                                           # 資料庫連線池的最小維持連線數

      initial-size: 5                                       # 初始化連線數

      max-total: 5                                          # 最大連線數

      max-wait-millis: 200                                  # 等待連接獲取的最大超時時間

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

③.修改DeptProvider8001_App主啟動類

package com.hosystem.springcloud;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

 

@SpringBootApplication

@EnableEurekaClient //本服務啟動後會自動註冊進eureka服務中

public class DeptProvider8001_App

{

    public static void main(String[] args)

    {

        SpringApplication.run(DeptProvider8001_App.class, args);

    }

}

[2].測試

①.啟動EurekaServer

  需要先啟動EurekaServer在啟動provider

 

②.啟動DeptProvider 

③.顯示效果

 

  Application中MICROSERVICECLOUD-DEPT如何得到.

 

(3).actuator與註冊微服務資訊完善

[1].主機名稱:服務名稱修改  

  如何去掉主機名稱

 

①.修改application.yml檔案

  修改microservicecloud-provider-dept-8001的application.yml檔案。

  修改部分:

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

  instance:

    instance-id: microservicecloud-dept8001

 

  完整部分:

server:

  port: 8001

 

mybatis:

  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑

  type-aliases-package: com.hosytem.springcloud.entities    # 所有Entity別名類所在包

  mapper-locations:

  - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案

 

#name spring.application.name=microservicecloud-dept  很重要很重要很重要

spring:

   application:

    name: microservicecloud-dept

   datasource:

    type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別

    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅動包

    url: jdbc:mysql://192.168.188.188:3306/cloudDB01              # 資料庫名稱

    username: root

    password: 123456

    dbcp2:

      min-idle: 5                                           # 資料庫連線池的最小維持連線數

      initial-size: 5                                       # 初始化連線數

      max-total: 5                                          # 最大連線數

      max-wait-millis: 200                                  # 等待連接獲取的最大超時時間

 

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

  instance:

    instance-id: microservicecloud-dept8001

②.顯示效果

 

[2].訪問資訊有IP資訊提示

  如何設定訪問資訊有IP資訊提示

 

①.修改applicaiton.yml

  修改microservicecloud-provider-dept-8001的application.yml檔案。

  修改部分:

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

  instance:

    instance-id: microservicecloud-dept8001

    prefer-ip-address: true     #訪問路徑可以顯示IP地址

 

   完整程式碼

server:

  port: 8001

 

mybatis:

  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑

  type-aliases-package: com.hosytem.springcloud.entities    # 所有Entity別名類所在包

  mapper-locations:

  - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案

 

#name spring.application.name=microservicecloud-dept  很重要很重要很重要

spring:

   application:

    name: microservicecloud-dept

   datasource:

    type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別

    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅動包

    url: jdbc:mysql://192.168.188.188:3306/cloudDB01              # 資料庫名稱

    username: root

    password: 123456

    dbcp2:

      min-idle: 5                                           # 資料庫連線池的最小維持連線數

      initial-size: 5                                       # 初始化連線數

      max-total: 5                                          # 最大連線數

      max-wait-millis: 200                                  # 等待連接獲取的最大超時時間

 

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

  instance:

    instance-id: microservicecloud-dept8001

    prefer-ip-address: true     #訪問路徑可以顯示IP地址

②.顯示效果

 

[3].微服務info內容詳細資訊

  如何配置服務報告info

 

①.修改microservicecloud-provider-dept-8001

  修改microservicecloud-provider-dept-8001的Pom檔案

  修改部分:

<!-- actuator監控資訊完善 -->

<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>microservicecloud</artifactId>

        <groupId>com.hosystem</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

 

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

 

    <dependencies>

        <dependency><!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->

            <groupId>com.hosystem</groupId>

            <artifactId>microservicecloud-api</artifactId>

            <version>${project.version}</version>

        </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>

 

        <!-- 將微服務provider側註冊進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>

 

        <!-- actuator監控資訊完善 -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-actuator</artifactId>

        </dependency>

    </dependencies>

 

</project>

②.修改父工程microservicecloud

  總的父工程microservicecloud修改pom.xml新增構建build資訊。

  修改部分:

<!--構建的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>

 

   完整部分:

<?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>

 

    <groupId>com.hosystem</groupId>

    <artifactId>microservicecloud</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <!--注:這行最開始建立的時候是不存在的-->

    <modules>

        <module>microservicecloud-api</module>

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

        <module>microservicecloud-consumer-dept-80</module>

        <module>microservicecloud-eureka-7001</module>

    </modules>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <maven.compiler.source>1.8</maven.compiler.source>

        <maven.compiler.target>1.8</maven.compiler.target>

        <junit.version>4.12</junit.version>

        <log4j.version>1.2.17</log4j.version>

        <lombok.version>1.16.18</lombok.version>

    </properties>

 

 

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Dalston.SR1</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-dependencies</artifactId>

                <version>1.5.9.RELEASE</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

            <dependency>

                <groupId>mysql</groupId>

                <artifactId>mysql-connector-java</artifactId>

                <version>5.0.4</version>

            </dependency>

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>druid</artifactId>

                <version>1.0.31</version>

            </dependency>

            <dependency>

                <groupId>org.mybatis.spring.boot</groupId>

                <artifactId>mybatis-spring-boot-starter</artifactId>

                <version>1.3.0</version>

            </dependency>

            <dependency>

                <groupId>ch.qos.logback</groupId>

                <artifactId>logback-core</artifactId>

                <version>1.2.3</version>

            </dependency>

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <version>${junit.version}</version>

                <scope>test</scope>

            </dependency>

            <dependency>

                <groupId>log4j</groupId>

                <artifactId>log4j</artifactId>

                <version>${log4j.version}</version>

            </dependency>

        </dependencies>

    </dependencyManagement>

 

    <!--構建的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>

 

</project>

③.修改microservicecloud-provider-dept-8001

  修改microservicecloud-provider-dept-8001的yml檔案。

  修改部分:

info:

  app.name: hosystem-microservicecloud

  company.name: www.hosystem.com

  build.artifactId: $project.artifactId$

  build.version: $project.version$

 

  完整部分:

server:

  port: 8001

 

mybatis:

  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑

  type-aliases-package: com.hosytem.springcloud.entities    # 所有Entity別名類所在包

  mapper-locations:

  - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案

 

#name spring.application.name=microservicecloud-dept  很重要很重要很重要

spring:

   application:

    name: microservicecloud-dept

   datasource:

    type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別

    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驅動包

    url: jdbc:mysql://192.168.188.188:3306/cloudDB01              # 資料庫名稱

    username: root

    password: 123456

    dbcp2:

      min-idle: 5                                           # 資料庫連線池的最小維持連線數

      initial-size: 5                                       # 初始化連線數

      max-total: 5                                          # 最大連線數

      max-wait-millis: 200                                  # 等待連接獲取的最大超時時間

 

eureka:

  client: #客戶端註冊進eureka服務列表內

    service-url:

      defaultZone: http://localhost:7001/eureka

  instance:

    instance-id: microservicecloud-dept8001

    prefer-ip-address: true     #訪問路徑可以顯示IP地址

info:

  app.name: hosystem-microservicecloud

  company.name: www.hosystem.com

  build.artifactId: $project.artifactId$

  build.version: $project.version$

④.顯示效果

 

(4).eureka自我保護

[1].效果演示

 

[2].現象原因

  簡單來說,某時刻某個微服務不可用,eureka不會立刻清理,依舊會對該微服務的資訊進行儲存。

[3].自我保護簡介

  預設情況下,如果EurekaServer在一定時間內沒有接收到某個微服務例項的心跳,EurekaServer將會登出該例項(預設90秒)。但是當網路分割槽故障發生時,微服務與EurekaServer之間無法正常通訊,以上行為可能變得非常危險了——因為微服務本身其實是健康的,此時本不應該登出這個微服務。Eureka通過“自我保護模式”來解決這個問題——當EurekaServer節點在短時間內丟失過多客戶端時(可能發生了網路分割槽故障),那麼這個節點就會進入自我保護模式。一旦進入該模式,EurekaServer就會保護服務登錄檔中的資訊,不再刪除服務登錄檔中的資料(也就是不會登出任何微服務)。當網路故障恢復後,該Eureka Server節點會自動退出自我保護模式。

  在自我保護模式中,Eureka Server會保護服務登錄檔中的資訊,不再登出任何服務例項。當它收到的心跳數重新恢復到閾值以上時,該Eureka Server節點就會自動退出自我保護模式。它的設計哲學就是寧可保留錯誤的服務註冊資訊,也不盲目登出任何可能健康的服務例項。

  綜上,自我保護模式是一種應對網路異常的安全保護措施。它的架構哲學是寧可同時保留所有微服務(健康的微服務和不健康的微服務都會保留),也不盲目登出任何健康的微服務。使用自我保護模式,可以讓Eureka叢集更加的健壯、穩定。

  在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false 禁用自我保護模式。

(5).服務發現Discovery

  microservicecloud-provider-dept-8001服務發現Discovery。

  對於註冊進eureka裡面的微服務,可以通過服務發現來獲得該服務的資訊

 

[1].修改microservicecloud-provider-dept-8001

  修改microservicecloud-provider-dept-8001工程的DeptController

  修改部分:

//注:如果Autowired顯示 could not autowired.there is more than one bean of 'DiscoveryClient' type; 使用一下兩種方法都行

//方法1:那麼只需要新增@Qualifier("discoveryClient")即可

//方法2::當然也可以修改成 private DiscoveryClient discoveryClient; 但是我們也需要修改discovery()方法中clientdiscoveryClient

@Qualifier("discoveryClient")

@Autowired

private DiscoveryClient client;

 

@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)

public Object discovery()

{

    List<String> list = client.getServices();

    System.out.println("**********" + list);

 

    List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");

    for (ServiceInstance element : srvList) {

        System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"

                + element.getUri());

    }

    return this.client;

}

  完整部分:

package com.hosystem.springcloud.controller;

 

import com.hosystem.springcloud.entities.Dept;

import java.util.List;

 

import com.hosystem.springcloud.service.DeptService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.cloud.client.ServiceInstance;

import org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

 

 

@RestController

public class DeptController

{

    @Autowired

    private DeptService service;

 

    //注:如果Autowired顯示 could not autowired.there is more than one bean of 'DiscoveryClient' type; 使用一下兩種方法都行