天天看點

SpringCloud全家桶 (第一期:服務注冊與服務發現)

文章目錄

    • 一: 什麼是SpringCloud
    • 二:建立注冊中心
    • 三:建立服務發現
    • 四:保護模式

一: 什麼是SpringCloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.

翻譯如下:

Spring Cloud為開發人員提供了快速建構分布式系統中的一些常見模式的工具(例如配置管理、服務發現、斷路器、智能路由、微代理、控制總線、一次性令牌、全局鎖、上司層選舉、分布式會話、叢集狀态)。分布式系統的協調導緻了鍋爐闆模式,使用Spring Cloud開發人員可以快速建立實作這些模式的服務和應用程式。它們在任何分布式環境中都能很好地工作,包括開發人員自己的筆記本電腦、裸機資料中心和雲計算等托管平台。

SpringCloud主要架構:

  • 服務發現——Netflix Eureka
  • 服務調用——Netflix Feign
  • 熔斷器——Netflix Hystrix
  • 服務網關——Netflix Zuul
  • 分布式配置——Spring Cloud Config
  • 消息總線 —— Spring Cloud Bus

二:建立注冊中心

1.先建立一個maven項目:如下

SpringCloud全家桶 (第一期:服務注冊與服務發現)

直接下一步即可

SpringCloud全家桶 (第一期:服務注冊與服務發現)

2,建立一個eureka(注冊中心)項目:

SpringCloud全家桶 (第一期:服務注冊與服務發現)

選擇

Spring Initializr

,點選next

SpringCloud全家桶 (第一期:服務注冊與服務發現)

選擇

Eureka Server

,點選下一步即可,

SpringCloud全家桶 (第一期:服務注冊與服務發現)

建立完項目中,

pom

中導入依賴:(一般建立完都有)

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ml</groupId>
    <artifactId>m_eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>m_eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

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

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

建立一個

application.yml

檔案,加入以下配置

server:
  port: 8761

spring:
  profiles:
    active: dev  # 指定環境(可省略)
  application:
    name: eureka   # 服務名稱(各服務之間調用會用到)

    
eureka:
  instance:
    #注冊中心服務的主機,預設是localhost
    hostname: localhost
  client:
    #是否将目前微服務注冊到Eureka服務中。自己是注冊中心,是以無需注冊。在搭建 Eureka Server 雙節點或叢集的時候,要把 eureka.client.register-with-eureka 和 eureka.client.fetch-registry 均改為 true(預設)。
    register-with-eureka: false
    #是否從Eureka中擷取注冊資訊。自己是注冊中心,是以無需擷取。
    fetch-registry: false
    #Eureka用戶端與與Eureka服務端進行互動的位址Map表
    service-url:
      #預設http://localhost:8761/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
           

eureka中自帶的有注冊中心頁面,加入

@EnableEurekaServer

注解,開啟Eureka服務,如圖:

SpringCloud全家桶 (第一期:服務注冊與服務發現)

eureka server叢集配置

啟動後,通路

http://127.0.0.1:8761

,進入注冊中心:

SpringCloud全家桶 (第一期:服務注冊與服務發現)

三:建立服務發現

1.建立discovery項目,如圖:

SpringCloud全家桶 (第一期:服務注冊與服務發現)

2,步驟和eureka類似,選擇

Eureka Discovery Client

SpringCloud全家桶 (第一期:服務注冊與服務發現)

pom

中導入依賴:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ml</groupId>
    <artifactId>discovery</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>discovery</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

建立

application.yml

,加入以下配置

server:
  port: 8086

spring:
  profiles:
   active: dev  # 指定環境(可省略)
  application:
    name: discovery   # 服務名稱(各服務之間調用會用到)
   # 以上配置跟eureka沒什麼差別

eureka:
  client:
    #Eureka服務的位址
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    #用于表示在猜測主機名時,伺服器的IP位址應該與作業系統報告的主機名相對應。(注冊服務和用戶端如果在一台機器上則無需配置)
    prefer-ip-address: true
           

加完配置後,還需要在啟動類上加上注解

@EnableDiscoveryClient

SpringCloud全家桶 (第一期:服務注冊與服務發現)
package com.ml.discovery;

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

@SpringBootApplication
@EnableEurekaClient //可以省略,因為pom中引入spring-cloud-starter-netflix-eureka-client,就會被預設當做用戶端
@EnableDiscoveryClient //開啟服務發現
public class DiscoveryApplication {

    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }

}
           

啟動discovery服務,咱們再看下有沒有注冊到eureka注冊中心上呢??答案:如下圖

SpringCloud全家桶 (第一期:服務注冊與服務發現)

四:保護模式

停掉其中的某服務,再預設等待1分鐘,注冊執行個體清單的上方會出現紅色的字型提示,内容為:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

如果在Eureka Server的首頁看到以下這段提示,則說明Eureka已經進入了保護模式。

解釋如下:

Eureka Server在運作期間,會統計心跳失敗的比例在15分鐘之内是否低于85%,如果出現低于的情況(在單機調試的時候很容易滿足,實際在生産環境上通常是由于網絡不穩定導緻),Eureka Server會将目前的執行個體注冊資訊保護起來,同時提示這個警告。保護模式主要用于一組用戶端和Eureka Server之間存在網絡分區場景下的保護。一旦進入保護模式,Eureka Server将會嘗試保護其服務系統資料庫中的資訊,不再删除服務系統資料庫中的資料(也就是不會登出任何微服務)。

【了解】

關閉保護模式(如無特殊需要,不建議):

Eureka server的

application.yml

eureka:  
  server:  
  	#是否開啟安全保護,預設是true開啟。
    enableSelfPreservation: false  
    #清理間隔(機關毫秒,預設是60*1000)
    eviction-interval-timer-in-ms: 6000
           

微服務的

application.yml

eureka:  
  instance:
  	#租期更新時間間隔(預設30秒)
    leaseRenewalIntervalInSeconds: 10  
    #租期到期時間(預設90秒)
    leaseExpirationDurationInSeconds: 30  
           

SpringCloud文檔:點選檢視

開心一刻

一天晚上,媽媽哄她10歲的兒子獨自到他自己的房裡睡覺,小家夥就是要媽媽陪着睡,無奈的媽媽就說“你羞不羞,這麼大的人還要媽媽陪着睡!”

“爸爸不是更大了還要你天天陪着睡!”兒子理直氣壯地說。

SpringCloud全家桶 (第一期:服務注冊與服務發現)

如果覺得不錯,幫忙點個贊,您的點贊将是我的動力!

下一篇:Feign的使用

繼續閱讀