Spring Cloud微服务项目搭建(一)

382 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第11天,点击查看活动详情

前言

编程是一个需要不断学习的职业,但是需要学习的技术很多。往往我们花费了很多精力在学习上,但是如果没有好的学习方式,就会造成知识不成体系,过不了多久就会忘记。个人的想法是,自己独立开发一个项目,以项目需要为驱动去学习技术点,同时落地到自己的项目中,也可以以技术点为驱动进行学些,学习完之后想办法也落地到自己的实际项目中。这里指的项目不是自己在公司做的项目,可以以公司的项目为背景,或者自己模拟一个产品,从0开始自己独立开发一个项目,因为毕竟不可能自己把所有的技术点在自己公司的真实项目上完全落地。

如果要落地所学的知识点,那么就需要一个项目,目前企业中大量使用的是微服务架构。那么就从0开始搭建一套微服务开发框架,以便以后落地一些自己学习的技术。

微服务搭建

本次将介绍从0开始如何搭建一个微服务开发架构。整个项目采用Maven工具进行构建。

环境说明

  • IDEA

  • JDK1.8

  • Spring Boot-2.2.5.RELEASE

  • SpringCloud-Hoxton.SR3

  • SpringCloud Alibaba-2.2.1.RELEASE

Spring Boot、Spring Cloud、Spring Cloud Alibaba的版本之间有对应的映射关系,不可随便选择,否则可能会发生不明的bug,官网也明确规定了各个版本的适配:github.com/alibaba/spr…

版本适配关系

下表为按时间顺序发布的 Spring Cloud Alibaba 以及对应的适配 Spring Cloud 和 Spring Boot 版本关系(由于 Spring Cloud 版本命名有调整,所以对应的 Spring Cloud Alibaba 版本号也做了对应变化)。毕业版本依赖关系(推荐使用)

Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version
2021.0.1.0Spring Cloud 2021.0.12.6.3
2.2.7.RELEASESpring Cloud Hoxton.SR122.3.12.RELEASE
2021.1Spring Cloud 2020.0.12.4.2
2.2.6.RELEASESpring Cloud Hoxton.SR92.3.2.RELEASE
2.1.4.RELEASESpring Cloud Greenwich.SR62.1.13.RELEASE
2.2.1.RELEASESpring Cloud Hoxton.SR32.2.5.RELEASE
2.2.0.RELEASESpring Cloud Hoxton.RELEASE2.2.X.RELEASE
2.1.2.RELEASESpring Cloud Greenwich2.1.X.RELEASE
2.0.4.RELEASE(停止维护,建议升级)Spring Cloud Finchley2.0.X.RELEASE
1.5.1.RELEASE(停止维护,建议升级)Spring Cloud Edgware1.5.X.RELEASE

每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本(经过验证,自行搭配各组件版本不保证可用)如下表所示(最新版本用*标记)。

Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2021.0.1.0*1.8.31.4.24.9.22.7.151.4.2
2.2.7.RELEASE1.8.12.0.34.6.12.7.131.3.0
2.2.6.RELEASE1.8.11.4.24.4.02.7.81.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE1.8.01.4.14.4.02.7.81.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE1.8.01.3.34.4.02.7.81.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.0
2.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE1.7.01.1.44.4.02.7.30.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE1.6.31.1.14.4.02.7.30.7.1

创建项目

打开开发工具idea,新建项目vip-cloud

image.png

image.png

image.png

配置项目

删除src目录然后修改pom.xml文件。pom.xml文件中除了默认生成了以外,做了一些内容的添加,直接覆盖掉自己的的pom文件即可。如果项目名称和包名称不一样,可以对标签artifactIdgroupId修改成自己的项目名称和包名称。但是配置文件中依赖以及依赖的版本建议先不要修改,等搭建完成后,能成功启动了,再根据自己的需要修改成自己的版本。

<?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.vip</groupId>
    <artifactId>vip-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
        <cloud-alibaba.version>2.2.1.RELEASE</cloud-alibaba.version>
        <lombok.version>1.18.4</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!--SpringBoot的依赖-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- springCloud的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- springCloud Alibaba 的依赖-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <scope>provided</scope>
            </dependency>
    </dependencyManagement>
</project>

创建子模块

左键选中自己的项目vip-cloud。右键New一个Module。

image.png

image.png 点击下一步,创建子模块。点击完成。

image.png

删除src目录。在pom.xml文件中添加<packaging>pom</packaging>

<?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">
    <parent>
        <artifactId>vip-cloud</artifactId>
        <groupId>com.vip</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>vip-calculate</artifactId>
    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

左键选中刚才新建的子模块。右键New一个Module。

步骤和上一步创建子模块是一样的,只是要注意木模块的选择。

image.png 重复上面的步骤,先在vip-cloud下创建一个子模块vip-framework,在vip-framework模块下创建一个子模块vip-calculate-commonvip-calculate-common子模块主要是添加公共依赖以及公共的类(比如pojo、util等)。在其他的业务子模块中添加该公共模块的依赖。

vip-caculate-common的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">
    <parent>
        <artifactId>vip-framework</artifactId>
        <groupId>com.vip</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>vip-calculate-common</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- spring boot web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

比如在子模块vip-calculate-biz中添加依赖。

<?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">
    <parent>
        <artifactId>vip-calculate</artifactId>
        <groupId>com.vip</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>vip-calculate-biz</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.vip</groupId>
            <artifactId>vip-calculate-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

以上的项目结构是。vip-cloud父项目pom工程,vip-frameworkvip-calculatevip-cloud子项目也是pom工程。vip-calculate-bizvip-calculate的子项目,是实际开发业务的项目。vip-calculate-commonvip-framework的子项目,是包含实际代码的项目,此工程用于定义基础 pojo 类、枚举、工具类等。