SpringCloud (一)——基本的搭建

簡介
SpringCloud是一個基於SpringBoot實現的微服務架構開發工具。它爲微服務架構中涉及的配置管理、服務治理、斷路器、智能路由等操做提供了一種簡單的開發方式。html

Spring Cloud 的github地址https://github.com/Netflix/Eurekajava

SpringCloud包含的子項目中Spring Cloud NetFlix :核心組件,對多個Netflix OSS 開源套件進行整合。git

  • Eureka:服務治理組件,包含註冊中心、服務註冊與發現機制的實現。
  • Hystrix:容錯管理組件,實現斷路器模式,幫助服務依賴中出現的延遲和爲故障提供強大的容錯能力。
  • Ribbon:客戶端負載均衡的服務調用組件。
  • Feign:基於Ribbon和Hystrix的聲明式服務調用組件
  • Zuul:網關組件,提供智能路由】訪問過濾等功能。
  • Archaius:外部化配置組件。

Spring Cloud Eureka

一. 服務註冊與發現github

圖中Eureka Server一般爲框架中構建的註冊中心,每一個service consumer 即服務單元向註冊中心登記本身提供的服務,將主機與端口號、版本號、通訊協議等一些附加信息告知註冊中心。
服務方調用調用服務提供方的接口時,需向服務註冊中心諮詢服務,並獲取服務,以實現訪問。web

註冊中心Eureka Serverspring

  • 服務名稱分類組織服務清單
  • 以心跳的方式去監測清單中服務是否可用,不可用則從服務清單中剔除。
  • 服務端-沒有存儲,內存保持,每服務實例須要發送心跳去續約
  • 客戶端-在內存中緩存着eureka的註冊信息,所以沒必要每請求到eureka查找服務
  • eureka之間會作註冊服務同步,從而保證狀態一致,客戶端只需訪問一個eureka

Service Provider緩存

  • 會向Eureka Server作Register(服務註冊)、Renew(服務續約)、Cancel(服務下線)等操做

Service Consumertomcat

  • 會向Eureka Server獲取註冊服務列表,並消費服務
  • 向註冊中心註冊自身提供的服務,並週期性的發送心跳來更新服務租約

二 搭建服務註冊中心架構

一、首先創建SpringBoot工程,在pom.xml中加入配置信息app

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除內置的tomcat-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>
    </dependencies>

二、經過@EnableEurekaServer 註解啓動一個服務註冊中心,即在SpringBoot應用中添加便可

@EnableEurekaServer
@SpringBootApplication
public class RegisterApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(RegisterApplication.class);
    }

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

在默認設置下,避免服務註冊中心將本身做爲客戶端註冊本身,因而在application.properties中增長以下配置

server:
  port: 9000
  context-path: /registry
spring:
  application:
    name: registry-server
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/${server.context-path}/eureka/
security:
  basic:
    enable: true

  user:
    name: admin
    password: admin

啓動服務,註冊界面報錯信息:


Eureka server和client之間每隔30秒會進行一次心跳通訊,告訴server,client還活着。由此引出兩個名詞:
Renews threshold:server指望在每分鐘中收到的心跳次數
Renews (last min):上一分鐘內收到的心跳次數。
前文說到禁止註冊server本身爲client,無論server是否禁止,閾值(threshold)是1。client個數爲n,閾值爲1+2n(此爲一個server且禁止自注冊的狀況)
若是是多個server,且開啓了自注冊,那麼就和client同樣,是對於其餘的server來講就是client,是要
2的
閾值:1+21
renews:
1)自注冊 2 + 2
1
2)非自注冊:2*1
Eurake有一個配置參數eureka.server.renewalPercentThreshold,定義了renews 和renews threshold的比值,默認值爲0.85。當server在15分鐘內,比值低於percent,即少了15%的微服務心跳,server會進入自我保護狀態,Self-Preservation。在此狀態下,server不會刪除註冊信息,這就有可能致使在調用微服務時,實際上服務並不存在。

參考文章https://www.cnblogs.com/breath-taking/articles/7940364.html

相關文章
相關標籤/搜索