簡介
SpringCloud是一個基於SpringBoot實現的微服務架構開發工具。它爲微服務架構中涉及的配置管理、服務治理、斷路器、智能路由等操做提供了一種簡單的開發方式。html
Spring Cloud 的github地址https://github.com/Netflix/Eurekajava
SpringCloud包含的子項目中Spring Cloud NetFlix :核心組件,對多個Netflix OSS 開源套件進行整合。git
一. 服務註冊與發現github
圖中Eureka Server一般爲框架中構建的註冊中心,每一個service consumer 即服務單元向註冊中心登記本身提供的服務,將主機與端口號、版本號、通訊協議等一些附加信息告知註冊中心。
服務方調用調用服務提供方的接口時,需向服務註冊中心諮詢服務,並獲取服務,以實現訪問。web
註冊中心Eureka Server :spring
Service Provider緩存
Service Consumertomcat
二 搭建服務註冊中心架構
一、首先創建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 + 21
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