各位看官可以關注博主個人博客,瞭解更多信息。
作者:Surpasser
鏈接地址:https://surpass.org.cn
學無止境,繼續前進
今天開始學習SpringCloud的相關知識,作爲初學者不想過多的深究原理和底層,把學會使用作爲本節的目標。
什麼是微服務架構
簡單地說,微服務是系統架構上的一種設計風格,它的主旨是將一個原本獨立的系統拆分成多個小型服務,這些小型服務都在各自獨立的進程中運行,服務之間通過基於HTTP的RESTful API進行通信協作。被拆分成的每一個小型服務都圍繞着系統中的某一項或一些羈合度較高的業務功能進行構建,並且每個服務都維護着自身的數據存儲、業務開發、自動化測試案例以及獨立部署機制。由於有了輕量級的通信協作基礎,所以這些微服務可以使用不同的語言來編寫。
單體架構和分佈式微服務的區別
單體架構的優點:
-
易於開發:開發人員使用當前開發工具在短時間內就可以開發出單體應用。
-
易於測試:因爲不需要依賴其他接口,測試可以節約很多時間。
-
易於部署:你只需要將目錄部署在運行環境中即可。
單體架構的缺點:
- 靈活度不夠:如果程序有任何修改,修改的不只是一個點,而是自上而下地去修改,測試時必須等到整個程序部署完後才能看出效果。在開發過程可能需要等待其他開發人員開發完成後才能完成部署,降低了團隊的靈活性。
- 降低系統的性能:原本可以直接訪問數據庫但是現在多了一層。即使只包含一個功能點,也需要在各個層寫上代碼。
- 系統啓動慢:一個進程包含了所有業務邏輯,涉及的啓動模塊過多,導致系統的啓動時間延長。
- 系統擴展性比較差:增加新東西的時候不能針對單個點增加,要全局性地增加。牽一髮而動全身。
SOA架構的優點:
-
把模塊拆分,使用接口通信,降低模塊之間的藕合度。
-
把項目拆分成若干個子項目,不同的團隊負責不同的子項目。
-
增加功能時只需要增加一個子項目,調用其他系統的接口即可。
-
可以靈活地進行分佈式部署。
SOA架構的缺點:
- 系統之間的交互需要使用遠程通信,接口開發增加工作量。
SpringCloud五大組件
- Eureka:註冊中心
- Zuul、Gateway:網關
- Ribbon:負載均衡
- Feign:服務調用
- Hystrix或Resilience4j:熔斷器
項目搭建
註冊中心的服務端和客戶端
版本說明:
Spring Boot | Spring Cloud |
---|---|
1.2.x | Angel版本 |
1.3.x | Brixton版本 |
1.4.x stripes | Camden版本 |
1.5.x | Dalston版本、Edgware版本 |
2.0.x | Finchley版本 |
2.1.x | Greenwich.SR2 |
SpringBoot和SpringCloud的版本必須對應。詳細請查看官方對應數據:https://start.spring.io/actuator/info,也許你會看着很亂,不要着急,找一個JSON轉換就OK啦。
創建Maven父工程,添加pom配置
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
創建Eureka Server子工程,配置pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在server啓動類添加註解@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
創建application.yml文件
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
#服務端server禁止自己註冊自己
register-with-eureka: false
#此客戶端是否獲取eureka服務器註冊表上的註冊信息,默認爲true
fetch-registry: false
service-url: #指定交互和通信的url
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
#當eureka server啓動的時候,不能從對等節點獲取instance註冊信息的情況,應等待多長時間。
wait-time-in-ms-when-sync-empty: 0
#服務端開啓自我保護模式。無論什麼情況,服務端都會保持一定數量的服務。避免client與server的網絡問題,而出現大量的服務被清除。
enable-self-preservation: false
創建Eureka Client子工程,配置pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--添加web,不添加啓動時自動停止-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
client啓動類添加註解@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
創建application.yml文件
server:
port: 8101
#客戶端必須起名字,否則註冊爲unknow
spring:
application:
name: client
eureka:
client:
service-url:
#填入server通信url
defaultZone: http://localhost:8001/eureka/
先後啓動Server和Client,訪問http://localhost:8001,可以看到如圖的界面:
- 界面爲Eureka Server的服務。
- 箭頭指的名字爲Eureka Client的服務。
訪問 http://127.0.0.1:8001/eureka/apps 能夠看到所有註冊到Eureka Server的Client信息。
訪問 http://127.0.0.1:8001/eureka/apps/+applicationName 能夠看到對應Client註冊到Eureka Server的信息。
註冊中心到這裏了,後面繼續!!!