Spring Cloud Alibaba introduction and project preparation

brief introduction

SpringCloud Alibaba is an open source microservice architecture solution of Alibaba group.

For better distributed system development, microservice architecture divides an application into multiple sub applications, and each service is a sub project that can run independently. It covers a lot of contents, including service governance, configuration management, current limiting and degradation, and N multi components supported by Alibaba open source Ecology (Dubbo, RocketMQ, etc.).

Spring cloud and Alibaba related components

  • Sentinel: Alibaba's open source product takes traffic as the entry point to protect the stability of services from multiple dimensions such as traffic control, fuse degradation and system load protection.
  • Nacos: Alibaba open source product, a dynamic service discovery, configuration management and service management platform that is easier to build cloud native applications.
  • RocketMQ: Apache RocketMQ ™ Java based distributed message and stream computing platform with high performance and high throughput.
  • Dubbo: Apache Dubbo ™ Is a high-performance Java RPC framework.
  • Seata: Alibaba open source product, an easy-to-use high-performance microservice distributed transaction solution.
  • Alibaba Cloud ACM: an application configuration center product that centrally manages and pushes application configurations in a distributed architecture environment.
  • Alibaba Cloud OSS: Alibaba cloud Object Storage Service (OSS) is a massive, secure, low-cost and highly reliable cloud storage service provided by Alibaba cloud. You can store and access any type of data in any application, anytime and anywhere.
  • Alibaba cloud scheduler X: a distributed task scheduling product developed by Alibaba middleware team, which provides second level, accurate, highly reliable and highly available timed (based on Cron expression) task scheduling services.
  • Alibaba Cloud SMS: a global SMS service with friendly, efficient and intelligent interconnected communication capabilities to help enterprises quickly build customer access channels.

major function

  • Service current limiting and degradation: by default, it supports the access of WebServlet, WebFlux, OpenFeign, RestTemplate, Spring Cloud Gateway, Zuul, Dubbo and RocketMQ current limiting and degradation functions. It can modify the current limiting and degradation rules in real time through the console at runtime, and it also supports the viewing of current limiting and degradation Metrics monitoring.
  • Service registration and discovery: it adapts to the Spring Cloud service registration and discovery standard and integrates Ribbon support by default.
  • Distributed configuration management: supports external configuration in distributed system, and automatically refreshes when the configuration changes.
  • Message driven capability: build message driven capability for microservice applications based on Spring Cloud Stream.
  • Distributed transaction: use @ GlobalTransactional annotation to solve the problem of distributed transaction efficiently and without business intrusion..
  • Alibaba cloud object storage: Alibaba cloud provides massive, secure, low-cost and highly reliable cloud storage services. Support the storage and access of any type of data in any application, any time and any place.
  • Distributed task scheduling: provide second level, accurate, highly reliable and highly available timed (based on Cron expression) task scheduling services. At the same time, it provides a distributed task execution model, such as grid tasks. Grid tasks support the uniform distribution of tasks to all workers (schedulerx clients) for execution.
  • Alibaba cloud SMS service: a global SMS service with friendly, efficient and intelligent interconnected communication capabilities helps enterprises quickly build customer access channels.

Component version relationship

Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Version Dubbo Version Seata Version
2.2.6.RELEASE 1.8.1 1.4.2 4.4.0 2.7.8 1.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE 1.8.0 1.4.1 4.4.0 2.7.8 1.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE 1.8.0 1.3.3 4.4.0 2.7.8 1.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE 1.7.1 1.2.1 4.4.0 2.7.6 1.2.0
2.2.0.RELEASE 1.7.1 1.1.4 4.4.0 2.7.4.1 1.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE 1.7.0 1.1.4 4.4.0 2.7.3 0.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE 1.6.3 1.1.1 4.4.0 2.7.3 0.7.1

Graduation version dependency

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
Spring Cloud 2020.0.1 2021.1 2.4.2
Spring Cloud Hoxton.SR9 2.2.6.RELEASE 2.3.2.RELEASE
Spring Cloud Greenwich.SR6 2.1.4.RELEASE 2.1.13.RELEASE
Spring Cloud Hoxton.SR3 2.2.1.RELEASE 2.2.5.RELEASE
Spring Cloud Hoxton.RELEASE 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Greenwich 2.1.2.RELEASE 2.1.X.RELEASE
Spring Cloud Finchley 2.0.4. Release (maintenance is stopped and upgrading is recommended) 2.0.X.RELEASE
Spring Cloud Edgware 1.5.1. Release (maintenance stopped, upgrade recommended) 1.5.X.RELEASE

Official version relationship description document: https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

Project preparation

Dependency management project (spring cloud Alibaba version parent)

This pom project is mainly used to manage the versions used by 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>

    <groupId>com.gitee</groupId>
    <artifactId>spring-cloud-alibaba-version-parent</artifactId>
    <version>0.0.1</version>

    <properties>
        <!-- Coding and JDK edition -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!-- SpringBoot SpringCloud Spring Cloud Alibaba Three piece set -->
        <spring.boot.version>2.5.2</spring.boot.version>
        <spring.cloud.version>2020.0.3</spring.cloud.version>
        <spring.cloud.alibaba.version>2021.1</spring.cloud.alibaba.version>
        <!-- JSON Transformation dependency -->
        <fastjson.version>1.2.78</fastjson.version>
        <!-- tool kit -->
        <hutool.version>5.7.3</hutool.version>
        <mapstruct.version>1.2.0.Final</mapstruct.version>
        <lombok.version>1.18.20</lombok.version>
        <!-- Swagger -->
        <springfox.boot.starter.version>3.0.0</springfox.boot.starter.version>
    </properties>

    <!-- Developer -->
    <developers>
        <developer>
            <name>wuwentao</name>
            <email>lovelyWu98k@gmail.com</email>
        </developer>
    </developers>

    <!-- Introducing dependency management -->
    <dependencyManagement>
        <dependencies>
            <!-- spring boot rely on -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring cloud rely on -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring cloud alibaba rely on -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- Construction mode -->
    <build>
        <finalName>${project.name}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <!-- Packaged plug-in management -->
        <pluginManagement>
            <plugins>
                <!-- Spring Boot Package plug-ins -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring.boot.version}</version>
                    <configuration>
                        <finalName>${project.build.finalName}</finalName>
                        <layers>
                            <enabled>true</enabled>
                        </layers>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>
Spring cloud Alibaba common

The jar project is mainly used to store the common contents of business services, such as unified response entity, unified exception handling, etc.

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>com.gitee</groupId>
        <artifactId>spring-cloud-alibaba-version-parent</artifactId>
        <version>0.0.1</version>
    </parent>

    <groupId>com.gitee</groupId>
    <artifactId>spring-cloud-alibaba-common</artifactId>
    <version>0.0.1</version>

    <dependencies>
        <!-- Tool dependency -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
		<!-- fastjson serialize -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <!-- quote Swagger class -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${springfox.boot.starter.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>
Request unified response return class definition

This class is created for the return value object returned to the front end or called after the Controller completes processing.

Response.java

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * Request unified response entity class
 *
 * @param <T> Return result type
 * @author wentao.wu
 */
@Data
@ApiModel(value = "Request response entity class")
public class Response<T> {
    @ApiModelProperty(value = "Request success response code")
    private String code;
    @ApiModelProperty(value = "Request success response message")
    private String msg;
    @ApiModelProperty(value = "Request failure response code")
    private String errorCode;
    @ApiModelProperty(value = "Request failure response message")
    private String errorMsg;
    @ApiModelProperty(value = "Return results")
    private T result;
}
Unified business service exception definition

Create this class of main user identification. The exception is thrown by the business service. In the later stage, the global exception can be handled uniformly.

ServiceException.java

import lombok.Getter;

/**
 * Service business runtime exception
 *
 * @author wentao.wu
 */
@Getter
public class ServiceException extends RuntimeException {
    public ServiceException(String errorCode, String errorMsg) {
        super();
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * Exception coding
     */
    private String errorCode;
    /**
     * Abnormal information
     */
    private String errorMsg;
}

matters needing attention

All later projects rely on the above two projects.

catalogue

title Portal
Introduction to Nacos Server installation and deployment Not updated
Sentinel server introduction to installation and deployment Not updated
Introduction to installation and deployment of Seata Server Not updated
Rocketmq server installation and deployment Not updated
SkyWalking introduction to installation and deployment Not updated
Spring Cloud Alibaba introduction and project preparation go to
Spring Cloud Alibaba uses Nacos as the service registry go to
Spring Cloud Alibaba uses Nacos as the configuration management center go to
Spring Cloud Alibaba uses RestTemplate for service consumption go to
Spring Cloud Alibaba uses Feign for service consumption go to
Spring Cloud Alibaba uses Seata to solve distributed transactions go to
Spring Cloud Alibaba uses the Gateway as the service Gateway go to
Spring Cloud Alibaba uses Sentinel for data mining & downgrade go to
Spring Cloud Alibaba uses SkyWalking for distributed link tracking go to
Spring Cloud Alibaba uses RocketMQ for message production go to
Spring Cloud Alibaba uses RocketMQ for message consumption go to
Continuously updating Continuous update
Source code storage address

gitee: https://gitee.com/SimpleWu/spring-cloud-alibaba-example.git
cnblogs: https://www.cnblogs.com/SimpleWu

Posted by scmeeker on Thu, 28 Oct 2021 01:35:35 -0700