springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)

  总想对微服务架构做一个小小的总结,不知如何下手,最近觉得还是从搭建微服务的过程来入手,对于springboot的maven项目从构建多模块架构进而衍化为常用的微服务架构来做个记录吧。

  首先,创建多个springboot项目,项目结构如图:

      springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)裁剪后如右侧springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)

  创建完成后,先解释一下:sharp-pom是父类工程,其他所有工程都要继承该工程,sharp-pom主要用来管理版本和声明一些jar的引用,本博主没有做聚合工程,这里说明一下聚合和继承的关系:继承是父POM为了抽取统一的配置信息和依赖版本控制,方便子POM直接引用,简化子POM的配置。聚合(多模块)则是为了方便一组项目进行统一的操作而作为一个大的整体,所以要真正根据这两者不同的作用来使用,不必为了聚合而继承同一个父POM,也不比为了继承父POM而设计成多模块。

接下来对各个模块进行处理
sharp-pom 只需要保留pom.xml文件即可,注意打包方式一定选择pom,具体内容如下:

 <?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.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <name>sharp-pom</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.version>0.0.1-SNAPSHOT</project.version>
<java.version>1.8</java.version>
<mysql.version>8.0.12</mysql.version>
<mybatis.spring.version>1.3.2</mybatis.spring.version>
<jdbc.version>5.1.1.RELEASE</jdbc.version>
<druid.version>1.1.10</druid.version>
<skipTests>true</skipTests>
</properties>
<!-- dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)dependencyManagement里只是声明依赖,并不实现引入,
因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会
从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。 -->
<!-- 这里只是声明,子项目中用到时候应用不需要再次引入版本号,统一在此管理了版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${jdbc.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
</dependencies>
<!-- 私服配置,子项目继承后不需要在子项目中配置 -->
</dependencyManagement>
<distributionManagement>
<repository>
<id>my-release</id>
<url>http://192.168.135.128:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>my-snapshots</id>
<url>http://192.168.135.128:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>

接下来的common、entity、mapper三个项目主要作为后面的依赖包均不需要启动类和测试类,都可以直接干掉,其中mapper有配置文件故而也要保留src/main/resources目录,其他两项只需要保留src/main/java即可,这些项目打包后是一个很普通的jar包,所以pom中不需要引入maven的打包插件,如果引入的话启动会报错。。。java:[1,17] 找不到符号、程序包。。。不存在之类的,因为只是一个普通包,我们直接把它干掉就行

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

  然后层层依赖,没有什么特别的,需要注意的是,非微服务的多模块,controller层直接依赖service层,所以service层不需要启动,service层也可以直接搞成如common、entity一样的结构,只是一个普通的依赖包即可,如果是作为微服务的一个服务,那么它要想注册中心注册,就需要在该项目进行相关数据库、mybatis及dubbo、zookeeper等的配置,现在这一步我们只是作为一个普通jar包依赖,故而相关数据库等的配置都在访问层进行即在sharp-user项目中进行配置。

common项目pom,除了pom暂时无其他内容

 <?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> <parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sharp-common</artifactId>
<packaging>jar</packaging> <name>sharp-common</name>
<description>公共模块</description> <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>
<!-- <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build> -->
</project>

entity pom

<?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> <parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sharp-entity</artifactId>
<packaging>jar</packaging> <name>sharp-entity</name>
<description>Demo project for Spring Boot</description> <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>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies> <!-- <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
--> </project>

mapper项目无特别之处

springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)

pom

 <?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>
<parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sharp-mapper</artifactId>
<packaging>jar</packaging> <name>sharp-mapper</name>
<description>Demo project for Spring Boot</description> <dependencies>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-entity</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!-- <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
--> </project>

service-api  pom

 <?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> <artifactId>sharp-service-api</artifactId>
<packaging>jar</packaging> <name>sharp-service-api</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-entity</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </project>

service pom

 <?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>
<parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sharp-service</artifactId>
<packaging>jar</packaging> <name>sharp-sevice</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-entity</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-mapper</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-service-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!-- <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> --> </project>

web user pom

 <?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> <artifactId>sharp-user</artifactId>
<packaging>jar</packaging> <name>sharp-user</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 微服务项目使不需要以下实现类的配置,只需要配置好dubbo即可 -->
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

application.properties

 spring.profiles.active=local

 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.xxx.xxx:3306/sharp
spring.datasource.username=root
spring.datasource.password=******** #mybatis配置
#指定全局配置文件位置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
#指定别名包
mybatis.type-aliases-package=com.sharp.forward.entity
#指定xml文件位置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

注意启动项上一定要加注解@MapperScan("com.sharp.forward.mapper"),其他都是常规的东西,结构如下:

springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)

测试OK!

上一篇:springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目


下一篇:ASP.NET Core微服务+Tabler前端框架搭建个人博客1--开始前想说的话