模块划分
微服务架构相对于单体架构最大的特点就是讲项目拆分成一个个单独的服务,实现了服务之间的解耦合,便于对服务进行维护。搭建项目的第一个步骤就是做好模块的划分。我个人比较喜欢按照项目的实体对象进行模块划分。划分结构如下图所示:
数据库及表的创建
数据库执行脚本在码云仓库有。
仓库地址:
https://gitee.com/jiefang666/uushop
创建模块
父工程
创建一个父工程进行基础的环境管理。开发工具使用IDEA,基于maven 工具进行模块和依赖管理。
创建方式:File - New Project -Maven
因为微服务架构的各个服务是基于spring boot 快速搭建的 所以我们父工程里面引入spring boot 的依赖,并且引入Spring cloud 的相关依赖 只需要在 pom.xml 里面添加如下依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 搭建微服务基础架构-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
搭建base模块
1 新建模块
基础模块主要引入一些通用的依赖,后面的模块需要用到这些依赖的时候直接把这个模块作为父类进行。其中主要包括 swagger的依赖,用于接口测试。web 依赖,用于启动服务。Lombok 用于自动生成getter /setter 和构造方法。
2 引入相关依赖
<dependencies>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
新建repository模块
在各个服务模块里面,有些模块需要用到操作数据库,新建这个模块来进行数据库依赖管理。需要进行数据库操作的模块直接继承这个模块就行。同时这个模块也提供一些通用的数据库操作的工具类 配置类。同时这个模块继承base模块。
相关依赖引入:
<parent>
<artifactId>base-service</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- MyBatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
新建common 模块
common 模块主要用来存放一些所有模块都能用到的工具类
common 模块继承 repository 模块
相关依赖如下:
<dependencies>
<!-- 数据校验 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.4.1.Final</version>
</dependency>
</dependencies>
具体服务模块
接下来就开始搭建具体的业务模块。请看下一篇博客