1. 程式人生 > 實用技巧 >微服務架構Spring Cloud Alibaba概述、主要功能和元件,建立統一的依賴管理

微服務架構Spring Cloud Alibaba概述、主要功能和元件,建立統一的依賴管理

目前市場上主流的第三套微服務架構解決方案:Spring Boot + Spring Cloud Alibaba

一、Spring Cloud Alibaba概述

2018 年 10 月 31 日的凌晨,這個偉大的日子裡,Spring Cloud Alibaba 正式入駐了 Spring Cloud 官方孵化器,並在 Maven 中央庫釋出了第一個版本。

Spring Cloud for Alibaba,它是由一些阿里巴巴的開源元件和雲產品組成的。這個專案的目的是為了讓大家所熟知的Spring 框架,其優秀的設計模式和抽象理念,以給使用阿里巴巴產品的 Java 開發者帶來使用 Spring Boot 和 SpringCloud 的更多便利。

Spring Cloud Alibaba 致力於提供微服務開發的一站式解決方案。此專案包含開發分散式應用微服務的必需元件,方便開發者通過 Spring Cloud 程式設計模型輕鬆使用這些元件來開發分散式應用服務。

依託 Spring Cloud Alibaba,您只需要新增一些註解和少量配置,就可以將 Spring Cloud 應用接入阿里微服務解決方案,通過阿里中介軟體來迅速搭建分散式應用系統。

注:學習 Spring Cloud Alibaba 之前,先學習一下之Spring Cloud spring全家桶視訊課程,效果更佳哦。

二、Spring Cloud Alibaba主要功能

  • 服務限流降級:預設支援 Servlet、Feign、RestTemplate、Dubbo 和 RocketMQ 限流降級功能的接入,可以在執行時通過控制檯實時修改限流降級規則,還支援檢視限流降級 Metrics 監控。
  • 服務註冊與發現:適配 Spring Cloud 服務註冊與發現標準,預設集成了 Ribbon 的支援。
  • 分散式配置管理:支援分散式系統中的外部化配置,配置更改時自動重新整理。
  • 訊息驅動能力:基於 Spring Cloud Stream 為微服務應用構建訊息驅動能力。
  • 阿里雲物件儲存:阿里雲提供的海量、安全、低成本、高可靠的雲端儲存服務。支援在任何應用、任何時間、任何地點儲存和訪問任意型別的資料。
  • 分散式任務排程:提供秒級、精準、高可靠、高可用的定時(基於 Cron 表示式)任務排程服務。同時提供分散式的任務執行模型,如網格任務。網格任務支援海量子任務均勻分配到所有 Worker(schedulerx-client)上執行。

三、Spring Cloud Alibaba元件

  • Sentinel:把流量作為切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。
  • Nacos:一個更易於構建雲原生應用的動態服務發現、配置管理和服務管理平臺。
  • RocketMQ:一款開源的分散式訊息系統,基於高可用分散式叢集技術,提供低延時的、高可靠的訊息釋出與訂閱服務。
  • Alibaba Cloud ACM:一款在分散式架構環境中對應用配置進行集中管理和推送的應用配置中心產品。
  • Alibaba Cloud OSS: 阿里雲物件儲存服務(Object Storage Service,簡稱 OSS),是阿里雲提供的海量、安全、低成本、高可靠的雲端儲存服務。您可以在任何應用、任何時間、任何地點儲存和訪問任意型別的資料。
  • Alibaba Cloud SchedulerX: 阿里中介軟體團隊開發的一款分散式任務排程產品,提供秒級、精準、高可靠、高可用的定時(基於 Cron 表示式)任務排程服務。

四、建立統一的依賴管理

Spring Cloud Alibaba 專案都是基於 Spring Cloud,而 Spring Cloud 專案又是基於 Spring Boot 進行開發,並且都是使用 Maven 做專案管理工具。在實際開發中,我們一般都會建立一個依賴管理專案作為 Maven 的 Parent 專案使用,這樣做可以極大的方便我們對 Jar 包版本的統一管理。

溫馨提示:
當前 Spring Cloud Alibaba 的 0.2.1.RELEASE 版本基於 Spring Cloud Finchley(F)開發,故在選擇 Spring Boot 版本時不要使用 2.1.0 及以上版本(因為 2.1.x 版本必須使用 Spring Cloud Greenwich,俗稱 G 版),請使用官方 Demo 中使用的 2.0.6.RELEASE,以免發生意想不到的問題(比如服務無法註冊到伺服器)。

建立一個工程名為hello-spring-cloud-alibaba-dependencies的專案,pom.xml配置檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
    </parent>

    <groupId>com.funtl</groupId>
    <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>hello-spring-cloud-alibaba-dependencies</name>
    <url>http://www.funtl.com</url>
    <inceptionYear>2018-Now</inceptionYear>

    <properties>
        <!-- Environment Settings -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Spring Settings -->
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
        <spring-cloud-alibaba.version>0.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <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>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!-- Compiler 外掛, 設定 JDK 版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

            <!-- 打包 jar 檔案時,配置 manifest 檔案,加入 lib 包的 jar 依賴 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <configuration>
                            <archive>
                                <manifest>
                                    <!-- Add directory entries -->
                                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                    <addClasspath>true</addClasspath>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- resource -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>

            <!-- install -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
            </plugin>

            <!-- clean -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
            </plugin>

            <!-- ant -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>

            <!-- dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <!-- Java Document Generate -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <!-- YUI Compressor (CSS/JS壓縮) -->
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>yuicompressor-maven-plugin</artifactId>
                    <version>1.5.1</version>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>compress</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <jswarn>false</jswarn>
                        <nosuffix>true</nosuffix>
                        <linebreakpos>30000</linebreakpos>
                        <force>true</force>
                        <includes>
                            <include>**/*.js</include>
                            <include>**/*.css</include>
                        </includes>
                        <excludes>
                            <exclude>**/*.min.js</exclude>
                            <exclude>**/*.min.css</exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <!-- 資原始檔配置 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <name>Aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>sonatype-repos</id>
            <name>Sonatype Repository</name>
            <url>https://oss.sonatype.org/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>sonatype-repos-s</id>
            <name>Sonatype Repository</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-repos</id>
            <name>Aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

  • parent:繼承了 Spring Boot 的 Parent,表示我們是一個 Spring Boot 工程
  • package:pom,表示該專案僅當做依賴專案,沒有具體的實現程式碼
  • spring-cloud-alibaba-dependencies:在properties配置中預定義了版本號為0.2.1.RELEASE,表示我們的 Spring Cloud Alibaba 對應的是 Spring Cloud Finchley 版本
  • build:配置了專案所需的各種外掛
  • repositories:配置專案下載依賴時的第三方庫
  • 學習完整的Spring Cloud Alibaba視訊教程,請點選此處。