持续创作,加速成长!这是我参与「掘金日新计划 · 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 Version | Spring Cloud Version | Spring Boot Version |
|---|---|---|
| 2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
| 2.2.7.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
| 2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
| 2.2.6.RELEASE | Spring Cloud Hoxton.SR9 | 2.3.2.RELEASE |
| 2.1.4.RELEASE | Spring Cloud Greenwich.SR6 | 2.1.13.RELEASE |
| 2.2.1.RELEASE | Spring Cloud Hoxton.SR3 | 2.2.5.RELEASE |
| 2.2.0.RELEASE | Spring Cloud Hoxton.RELEASE | 2.2.X.RELEASE |
| 2.1.2.RELEASE | Spring Cloud Greenwich | 2.1.X.RELEASE |
| 2.0.4.RELEASE(停止维护,建议升级) | Spring Cloud Finchley | 2.0.X.RELEASE |
| 1.5.1.RELEASE(停止维护,建议升级) | Spring Cloud Edgware | 1.5.X.RELEASE |
每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本(经过验证,自行搭配各组件版本不保证可用)如下表所示(最新版本用*标记)。
| Spring Cloud Alibaba Version | Sentinel Version | Nacos Version | RocketMQ Version | Dubbo Version | Seata Version |
|---|---|---|---|---|---|
| 2021.0.1.0* | 1.8.3 | 1.4.2 | 4.9.2 | 2.7.15 | 1.4.2 |
| 2.2.7.RELEASE | 1.8.1 | 2.0.3 | 4.6.1 | 2.7.13 | 1.3.0 |
| 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 |
创建项目
打开开发工具idea,新建项目vip-cloud
配置项目
删除src目录然后修改pom.xml文件。pom.xml文件中除了默认生成了以外,做了一些内容的添加,直接覆盖掉自己的的pom文件即可。如果项目名称和包名称不一样,可以对标签artifactId、groupId修改成自己的项目名称和包名称。但是配置文件中依赖以及依赖的版本建议先不要修改,等搭建完成后,能成功启动了,再根据自己的需要修改成自己的版本。
<?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。
点击下一步,创建子模块。点击完成。
删除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。
步骤和上一步创建子模块是一样的,只是要注意木模块的选择。
重复上面的步骤,先在
vip-cloud下创建一个子模块vip-framework,在vip-framework模块下创建一个子模块vip-calculate-common。vip-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-framework、vip-calculate是vip-cloud子项目也是pom工程。vip-calculate-biz是vip-calculate的子项目,是实际开发业务的项目。vip-calculate-common是vip-framework的子项目,是包含实际代码的项目,此工程用于定义基础 pojo 类、枚举、工具类等。