Build Spring Cloud Alibaba environment

Spring Cloud Alibaba is the mainstream distributed microservice architecture at present. This paper mainly explains how to build Spring Cloud Alibaba environment in IDEA, introduces the relationship between various versions of Spring Cloud Alibaba and the preparations before building Spring Cloud.

Spring Cloud Alibaba official document (Chinese)

Spring Cloud official website

Pre preparation

Spring Cloud Alibaba needs to rely on the Java environment and configure Maven environment to ensure the following versions:

  1. JDK 1.8+: install
  2. Maven 3.2.x+: install

Version Description

View the on the official website Version Description

Component version relationship

The version relationship in the figure below does not need to be set by yourself. Spring Cloud Alibaba dependencies have been configured in the Spring Cloud Alibaba version manager, and the dependency can be introduced

Graduation version dependency (recommended)

Different versions of spring cloud and Alibaba have different version dependencies. You need to view them when creating them Official website description , select RELEASE version

Construction method

pom.xml

<properties>
    <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
    <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
    <spring.cloud.version>Hoxton.SR9</spring.cloud-version>
</properties>

<dependencyManagement>
    <dependencies>
        <!--Spring Cloud Alibaba Version Manager-->
        <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>
        <!--Spring Boot Version Manager-->
        <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 Version Manager-->
        <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>

Spring Boot dependencies and Spring Boot starter parent in the Spring Boot version manager are the same. Both names can be used

Spring scaffold creation

Build project: contract > configuration > coding

Parent project

  1. Create a new parent project in the IDEA (the parent project is used to agree the version of the entire spring cloud project)

  1. Select Spring Initializr

  1. Fill in the project information. Note: select Maven POM and Java version (JDK)
    Selecting Maven POM to create a project will not create src file, only pom.xml file, which is enough for the parent project

  1. Select the Spring Boot Version (select it casually and need to be modified later)

  1. Check the project name and save location, and click Finish. Wait for the creation to be completed, and the creation will be completed when the interface appears

  1. After creation, open the pom.xml file, add the following contents, and add the dependencies of SpringCloudAlibaba, SpringBoot and SpringCloud

pom.xml

<properties>
    <spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
    <spring.boot.version>2.3.2.RELEASE</spring.boot.version>
    <spring.cloud.version>Hoxton.SR9</spring.cloud-version>
</properties>

<dependencyManagement>
    <dependencies>
        <!--Spring Cloud Alibaba Version Manager-->
        <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>
        <!--Spring Boot Version Manager-->
        <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 Version Manager-->
        <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>

Spring Boot dependencies and Spring Boot starter parent in the Spring Boot version manager are the same. Both names can be used

  1. After the parent project is built, other components such as service discovery and registration, configuration center, message bus, load balancing, circuit breaker and data monitoring can be introduced into the pom.xml of the parent project if necessary. This is omitted first

Sub module

  1. Create a new module in the parent project

  1. Select Maven and click Next.

  1. Fill in sub module name (service name)

Compared with the scaffold officially provided by Spring, the Alibaba cloud scaffold It will be faster and more convenient to create. Alibaba cloud focuses on micro service projects

IDEA settings

Add the Services tab of SpringBoot

Quickly manage the status of each service

  1. Edit configuration

  1. Add SpringBoot service

  1. Find SpringBoot

  1. After clicking OK, a service will appear below to view the microservice status of Spring Boot

Service renaming

  1. Right click the service and select the red box in the figure below to open it

  1. Modify configuration information

Tags: Java Spring Cloud

Posted on Tue, 02 Nov 2021 05:27:00 -0400 by Nicholas