SpringCloud
為開發者提供了在分布式系統中的一些常用的元件(例如配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖定,決策競選,分布式會話叢集狀态)。使用Spring Cloud開發人員可以快速地完成實作這些模式的服務和應用程式。它們在任何分布式環境中都能很好地工作
Eureka
學過
Dubbo
的一定不會陌生
ZooKeeper
,那麼學
SpringCloud
的就不能陌生
Eureka
。它是
Netflix
開源的一款基于REST(Representational State Transfer)風格的服務發現架構,目前已被
SpringCloud
內建在其子項目
spring-cloud-netflix
中,用于
SpringCloud
的服務注冊發現功能。
- 純正的
應用,jar/war 包部署Servlet
- 使用
架構實作自身的Jersey
接口RESTfulHTTP
- peer 之間的同步與服務的注冊全部通過 HTTP 協定實作
- 利用
實作 心跳檢測、定時清理過期服務、節點同步等功能(動态添加删除節點、彈性)JDKTimer
- 使用
的Google
包管理記憶體緩存guava
- 目前 2.0 已經停止開源,但在
版中Finchley
所采用的是 1.9.x 版本(持續維護),是以完全不用擔心,同時SpringCloud
還提供了SpringCloud
、Eureka
、Consul
、ZooKeeper
等多款服務注冊發現功能的架構,完全不用擔心會在一棵樹上吊死Etcd
Eureka 的 Server 與 Client
-
提供服務注冊與發現,EurekaServer:
節點啟動後,會向Client
發送注冊事件,EurekaServer
系統資料庫中将會存儲所有可用服務節點的資訊,服務節點的資訊可以在界面中直覺的看到。基于EurekaServer
架構實作自身的Jersey
接口,隻要滿足規範任何語言程式都能向其進行注冊RESTfulHTTP
-
任何形式的用戶端,隻要滿足EurekaClient:
訂制的注冊标準。EurekaServer
你好 Eureka Server
搭建一個
EurekaServer
是最為容易的,隻需要一個依賴即可完成。在
Finchley
版本中
artifactId
與早期的有所差異,是以大家在使用
SpringCloud
一定要注意自己所使用的版本,如果詢問它人相關問題,也應該告知對方你所采用的版本
依賴
<!-- 此處我隻給出了 Eureka Server 需要用到的包依賴,
為了篇幅考慮 spring-boot-starter-parent 及 spring-cloud-dependencies
并未将其粘貼出來,有需要的可以點選文末的 GITHUB 連接配接進去 copy -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
複制
配置檔案
在
src/main/resources
目錄下建立一個
bootstrap.yml
的檔案,文中我已經将部分需要注意的配置項羅列出來,并附寫好相關說明,更多配置可以參考
EurekaInstanceConfigBean
、
EurekaClientConfigBean
、
EurekaServerConfigBean
。絕大多數情況下對于
EurekaServer
隻需了解每個配置的作用及其意義即可,因為錯誤的配置反而會破壞原有的保護特性
server:
port: 7071
eureka:
instance:
prefer-ip-address: true
# 生産環境中官方是不建議修改預設配置,因為那樣會破壞 eureka server 的保護模式
server:
# 關閉保護模式(生産環境不建議修改)
enable-self-preservation: false
# 清理間隔(預設是60 * 1000 毫秒)(生産環境不建議修改)
eviction-interval-timer-in-ms: 60000
# Eureka 拉取服務清單時間(預設:30秒)(生産環境不建議修改)
remote-region-registry-fetch-interval: 5
client:
# eureka server 沒必要自己把自己注冊上去,是以可以設定成 false
register-with-eureka: false
# 是否從Eureka Server上擷取注冊資訊,預設為true,此處建議修改成 false (單機設定的意義不大,如果設定成 true 啟動會去抓取一次系統資料庫,擷取不到更新緩存就會出錯(該錯誤不影響 eureka 正常使用))
fetch-registry: false
service-url:
# 預設注冊位址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
# 劃重點:此處的 defaultZone 千萬别寫成 default-zone
defaultZone: http://localhost:${server.port}/eureka/
# 從 Eureka 伺服器端擷取注冊資訊的間隔時間(預設:30秒)
registry-fetch-interval-seconds: 5
複制
主函數
與往常
SpringBoot
啟動類類似,不同的是多了一個
@EnableEurekaServer
注解,用來辨別目前工程是
EurekaServer
,到此我們 Server 就算是搭建完成了。
package com.battcn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Levin
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class, args);
}
}
複制
通路
http://localhost:7071/
能看到如下内容就代表項目搭建成功,圖中紅色的表示我們關閉了
Eureka
保護模式,在網絡波動的情況下
EurekaServer
可能無法及時的保護用戶端執行個體
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAjM2EzLcd3LcJzLcJzdllmVldWYtl2PnVGcq5Sb4MjahlmdjN2YvwlN0YTMwcjMtUGall3LcVmdhNXLwRHdo9CXt92YucWbpRWdvx2Yx5yazF2Lc9CX6MHc0RHaiojIsJye.jpeg)
你好 Eureka Client
搭建一個
EurekaClient
也是相當容易的,隻需要兩個小小的依賴即可完成。
依賴
<!-- 此處我隻給出了 Eureka Server 需要用到的包依賴,
為了篇幅考慮 spring-boot-starter-parent 及 spring-cloud-dependencies
并未将其粘貼出來,有需要的可以點選文末的 GITHUB 連接配接進去 copy -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
複制
配置檔案
下面羅列一部分需要注意的配置項,需要注意的都已經寫好相關說明,更多配置可以參考
EurekaInstanceConfigBean
、
EurekaClientConfigBean
、
InetUtilsProperties
以下配置适用于大部分情況
server:
port: 7072
# 如果不寫 Eureka Server 中的界面中 Application 就會是 Unknown 盡量寫
spring:
application:
name: eureka-client
cloud:
inetutils:
# 忽略指定網卡(支援正則),假設你的電腦有 VM 那麼該選項是非常有用的一個選項
ignored-interfaces: #忽略 docker0 網卡以及 veth 開頭的網卡
- docker0
- veth.*
preferred-networks: # 使用指定網絡位址,選擇 eth0 網卡,當然也可以直接寫 IP (192.168)
- eth0
eureka:
instance:
# 此處建議寫,不寫預設是機器名
prefer-ip-address: true
# 優先級小于 application.properties ,如果你想知道目前注冊上去的版本必須使用 application.properties 中的配置寫法
# 因為 bootstrap.yml 最早初始化,那時候還無法讀取到 pom.xml 中的屬性
instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
# ip-address 強行指定此執行個體 IP ,不是很推薦,絕大多數情況 prefer-ip-address 與 preferred-networks 配合都夠用了
client:
service-url:
# 劃重點:此處的 defaultZone 千萬别寫成 default-zone
defaultZone: http://localhost:7071/eureka/
複制
擴充知識
如果你覺得
EurekaServerUI
中預設顯示的
instance-id
讓你看着不爽,你可以試着在根目錄下建立一個
application.properties
填寫如下内容,因為
SpringCloud
和
SpringBoot
啟動裝載的配置有優先級之分,
bootstrap
檔案優先于
application
檔案之前裝載,是以一些配置隻在
application
中生效
# 預設值:XB-201704242009:eureka-client:7072
# 修改前(bootstrap 配置的 instance-id):192.168.13.22:7072
# 修改後(application 配置的 instance-id):192.168.13.22:7072:0.0.1-SNAPSHOT
# 是不是後者更為直覺,如果要用 @project.version@ 那麼必須在 pom.xml 插件做一下小小的配置,具體請看 GITHUB ,當然如果你不需要顯示 version 那麼該檔案你可以忽略
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}:@project.version@
複制
主函數
與往常
SpringBoot
啟動類類似,不同的是多了一個
@EnableDiscoveryClient
注解,用來辨別目前工程是
DiscoveryClient
。因為
SpringCloud
提供了非常多的服務注冊發現的元件,如
euerka
、
zookeeper
、
consul
、
etcd
、是以為統一辨別用戶端身份,是以它用的是
@EnableDiscoveryClient
注解
package com.battcn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Levin
*/
@EnableDiscoveryClient
@SpringBootApplication
public class MyEurekaClient {
public static void main(String[] args) {
SpringApplication.run(MyEurekaClient.class, args);
}
}
複制
總結
目前很多大佬都寫過關于
SpringCloud
的教程了,如有雷同,請多多包涵,本教程基于最新的
spring-cloud:Finchley.SR1
編寫...
個人部落格:http://blog.battcn.com/
全文代碼:https://github.com/battcn/spring-cloud2-learning/tree/master/chapter1