SpringCloud 框架基本模塊梳理(一)
前言
本片文章作爲本系列的初始,將會搭建 註冊中心 和 網關 共兩個服務,同時會從個人來闡述技術選型,歡迎討論,輕噴。
一、環境&組件版本介紹
JDK 8, MAVEN 3, idea編譯器
SpringBoot: 2.2.5.RELEASE
SpringCloud: Hoxton.SR3
二、搭建註冊中心
1、技術選型
一提到分佈式系統註冊中心的技術選型,就會有人跟我提一下CAP(Consistency(一致性)、 Availability(可用性)、Partition tolerance(分區容錯性))理論,然後會告訴我三者當中只能滿足兩種情況 要麼CA要麼CP,至於爲啥,通常都是聽的一知半解,我就不對這個理論發表什麼看法了,畢竟年輕。
由於我接觸的少,備選的方案不多,主要有:Zookeeper, Eureka, Consul(本系列會選取eureka和consul來搭建環境)關於3種技術的闡述,此處就簡單聊聊,有興趣問度娘。
Zookeeper: 主節點依賴,通信異常的時候可能會導致服務長時間不可用,在集羣環境下的表現異常強大(這是個人感覺)且區分角色,通常分3種,集羣數以3的倍數爲佳
Eureka: 只要自身服務沒問題,註冊進來的服務掛不掛並不影響其他服務。
Consul: 只知道是大佬們的東西,目前沒有過接觸。
2、基本流程
本篇文章以eureka爲註冊中心,因爲此時consul還在下載。
Idea創建一個乾淨的SpringCloud工程
整理一下項目結構
pom文件,版本對應一下文章。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</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>
創建一個module,命名爲eureka,
引入spring-cloud-starter-netflix-eureka-server依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
啓動項添加註解@EnableEurekaServer來表名身份
配置一下yml文件,沒有就新建
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
healthcheck:
enabled: true
server:
enable-self-preservation: true
截至目前,註冊中心搭建完畢,可以啓動訪問一下。
3、效果展示
[eureka截圖]
三、搭建網關
1、技術選型
關於網關,備選的有zuul和gateway (還是瞭解的少了)此處我選擇gateway,畢竟5.0了,也是要往前走走的。值得提一下的是,在Hoxton.SR3下,gateway有一個斷言的特性(不知道之前的版本有沒有)同時增加 RetryFilter, RouteDefinition, 以及支持低於 1 req/s 條件限流。
2、基本流程
再一次創建一個module, 命名爲gateway,引入依賴(此處是重點,不要隨便引入其他jar)
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<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-actuator</artifactId>
</dependency>
</dependencies>
配置yml文件
server:
port: 8888
spring:
profiles:
active: path-route #使用哪個配置文件
application:
name: gateway #服務名
cloud:
#設置路由規則
gateway:
discovery:
locator:
enabled: true
routes:
- id: apiuser
# lb代表從註冊中心獲取服務
uri: lb://provide
predicates:
# 以P開頭的請求都將轉發到uri設置的路由
- Path=/p/**
filters:
- StripPrefix=1
這裏着重介紹一下gateway的相關配置
discovery.locator.enabled : true
開啓服務註冊,配置可替代啓動項@EnableEurekaClient
routes-id 唯一的路由規則標識
routes.uri 跳轉的服務地址 1p://服務名 -> 可以從註冊中心匹配服務
routes.predicates-Path 斷言規則— eg: - Path=/p/** 表示以P開頭的請求都將轉發到uri設置的路由
截至目前,網關gateway搭建完畢,可以啓動訪問一下。
3、效果展示
爲了驗證網關, 我這邊創建了一個簡單的服務,輸出 Hello World。
直接訪問服務
網關訪問服務
四、本篇結束