【微服务专题】深入理解与实践微服务架构(四)之父模块项目创建

474 阅读4分钟

我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第4篇文章,点击查看活动详情

Spring官方的微服务模块划分参考架构,如下图所示:

因此,一个完整的微服务系统底座的基本功能应该包含:

  • API 网关:微服务 API 托管、认证和鉴权、负载均衡等。
  • 资源管理:计算、存储、网络资源的管理。
  • 编排(解决服务部署问题) :调度、部署和升级。
  • 熔断、服务降级、限流(解决服务容错问题)
  • 消息总线(解决服务间调用问题) :轻量级的 MQ 或 HTTP。
  • 服务中心(解决服务发现问题) :服务注册、发现、订阅。
  • 监控和告警:监控每个服务的状态,必要时产生告警。
  • 日志和审计:日志的汇总,分类和查询。
  • 调用链跟踪:问题跟踪调试框架。

为了制作快速可用的微服务脚手架,在简化微服务结构的同时需要合理地拆分服务。

项目分层方案如下所示:

  • api-gateway -- 网关服务
  • auth-server -- 认证与鉴权中心服务
  • common -- 公共模块
  • discovery-server -- 注册与配置中心服务
  • infrastruct -- 基础设施
  • modules -- 功能模块
  • mq-server -- 消息队列服务
  • web -- 前端应用
  • rpc-server -- RPC调用服务

版本依赖选择

首先因为Spring Cloud Aliaba是基于Spring Cloud 2.x版本进行优化开发的版本,因此对于版本选择有一定要求。

由于 Spring Boot 1 和 Spring Boot 2 在 Actuator 模块的接口和注解有很大的变更,且 spring-cloud-commons 从 1.x.x 版本升级到 2.0.0 版本也有较大的变更,因此我们采取跟 SpringBoot 版本号一致的版本:

  • 1.5.x 版本适用于 Spring Boot 1.5.x
  • 2.0.x 版本适用于 Spring Boot 2.0.x
  • 2.1.x 版本适用于 Spring Boot 2.1.x
  • 2.2.x 版本适用于 Spring Boot 2.2.x
  • 2020.x 版本适用于 Spring Boot 2.4.x
  • 2021.x 版本适用于 Spring Boot 2.6.x

Spring Cloud 最新版本依赖

2021.0.2版本

2022-04-27

  • Spring Cloud Stream 3.2.3
  • Spring Cloud Config 3.1.2 (issues)
  • Spring Cloud Starter Build 2021.0.2
  • Spring Cloud Contract 3.1.2 (issues)
  • Spring Cloud Gateway 3.1.2 (issues)
  • Spring Cloud Netflix 3.1.2 (issues)
  • Spring Cloud Kubernetes 2.1.2 (issues)
  • Spring Cloud Function 3.2.4
  • Spring Cloud Sleuth 3.1.2 (issues)
  • Spring Cloud Task 2.4.2 (issues)
  • Spring Cloud Commons 3.1.2 (issues)
  • Spring Cloud Openfeign 3.1.2 (issues)
  • Spring Cloud Circuitbreaker 2.1.2 (issues)

2021.0.1版本

See the project page for all issues included in the release.

2022-02-17

  • Spring Cloud Config 3.1.1 (issues)
  • Spring Cloud Zookeeper 3.1.1
  • Spring Cloud Circuitbreaker 2.1.1 (issues)
  • Spring Cloud Kubernetes 2.1.1 (issues)
  • Spring Cloud Commons 3.1.1 (issues)
  • Spring Cloud Sleuth 3.1.1 (issues)
  • Spring Cloud Contract 3.1.1 (issues)
  • Spring Cloud Task 2.4.1
  • Spring Cloud Gateway 3.1.1 (issues)
  • Spring Cloud Openfeign 3.1.1 (issues)
  • Spring Cloud Starter Build 2021.0.1
  • Spring Cloud Netflix 3.1.1 (issues)

组件版本关系

每个 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

毕业版本依赖关系(推荐使用)

下表为按时间顺序发布的 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 VersionSpring Cloud VersionSpring Boot VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2021.0.1.0Spring Cloud 2021.0.12.6.31.8.31.4.24.9.22.7.151.4.2

因为最新的log4j2的CVE漏洞问题,后续会对相应的组件进行版本升级,但搭建时还是以推荐的稳定版本为主。

创建父模块

我们采用父子多级Maven项目的方式创建微服务项目,这里父模块使用Maven项目创建,避免SpringBoot项目结构的bug并且方便管理。

创建父POM项目(File -> new Project -> maven -> Next):

image-20220423145304647

点击 finish 完成项目创建:

image-20220430231249684

删除不需要的文件(因为是父POM、只要剩下.idea文件夹和pom.xml文件就行了、MD文件后续再补)

添加父模块依赖

<?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.deepinsea</groupId>
    <artifactId>spring-cloud-alibaba-starter</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>service-discovery-nacos</module>
    </modules>
​
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!-- 统一spring cloud alibaba版本依赖 -->
        <spring.cloud.alibaba-version>2021.0.1.0</spring.cloud.alibaba-version>
        <spring.cloud-version>2021.0.1</spring.cloud-version>
        <spring.boot.version>2.6.3</spring.boot.version>
        <lombok.version>1.18.16</lombok.version>
        <hutool.version>5.7.19</hutool.version>
        <fastjson.version>1.2.75</fastjson.version>
        <druid.version>1.1.22</druid.version>
        <mysql.version>8.0.23</mysql.version>
        <mybatis.plus.version>3.4.2</mybatis.plus.version>
        <maven.plugin.version>2.5.0</maven.plugin.version>
    </properties>
​
    <!-- springboot父依赖版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
    </parent>
​
    <!-- 锁定spring cloud alibaba子模块版本 -->
    <dependencyManagement>
        <dependencies>
            <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>
            <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>
​
    <!-- 编译和打包配置 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${maven.plugin.version}</version>
            </plugin>
        </plugins>
    </build>
</project>

dependencyManagement标签提供了一种管理依赖版本号的方式,它的作用是:子模块继承之后锁定版本+子modlue不用写groupId和versio。注意,当子模块有定义依赖版本时,还是会优先导入子模块的依赖版本的。

欢迎点赞,谢谢大佬ヾ(◍°∇°◍)ノ゙