springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目...

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

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

      springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目..._第1张图片裁剪后如右侧springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目..._第2张图片springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目..._第3张图片

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

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

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5 
 6     <groupId>com.sharpgroupId>
 7     <artifactId>sharp-pomartifactId>
 8     <version>0.0.1-SNAPSHOTversion>
 9     <packaging>pompackaging>
10 
11     <name>sharp-pomname>
12     <description>Demo project for Spring Bootdescription>
13 
14     <parent>
15         <groupId>org.springframework.bootgroupId>
16         <artifactId>spring-boot-starter-parentartifactId>
17         <version>2.1.0.RELEASEversion>
18         <relativePath/> 
19     parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
24         <project.version>0.0.1-SNAPSHOTproject.version>
25         <java.version>1.8java.version>
26         <mysql.version>8.0.12mysql.version>
27         <mybatis.spring.version>1.3.2mybatis.spring.version>
28         <jdbc.version>5.1.1.RELEASEjdbc.version>
29         <druid.version>1.1.10druid.version>
30         <skipTests>trueskipTests>   
31     properties>
32 
35 
36 <dependencyManagement>
37     <dependencies>
38         <dependency>
39             <groupId>com.alibabagroupId>
40             <artifactId>druidartifactId>
41             <version>${druid.version}version>
42         dependency>
43         <dependency>
44             <groupId>org.springframeworkgroupId>
45             <artifactId>spring-jdbcartifactId>
46             <version>${jdbc.version}version>
47         dependency>
48         <dependency>
49             <groupId>mysqlgroupId>
50             <artifactId>mysql-connector-javaartifactId>
51             <version>${mysql.version}version>
52         dependency>
53         <dependency>
54          <groupId>org.mybatis.spring.bootgroupId>
55             <artifactId>mybatis-spring-boot-starterartifactId>
56             <version>${mybatis.spring.version}version>
57         dependency>
58     dependencies>
59     
60 dependencyManagement>
61     <distributionManagement>
62         <repository>
63             <id>my-releaseid>
64             <url>http://192.168.135.128:8081/repository/maven-releases/url>
65         repository>
66         <snapshotRepository>
67             <id>my-snapshotsid>
68             <url>http://192.168.135.128:8081/repository/maven-snapshots/url>
69         snapshotRepository>
70     distributionManagement>
71 project>
View Code

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




org.springframework.boot
spring-boot-maven-plugin


org.apache.maven.plugins
maven-surefire-plugin
2.6

true



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

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

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5 
 6     <parent>
 7         <groupId>com.sharpgroupId>
 8         <artifactId>sharp-pomartifactId>
 9         <version>0.0.1-SNAPSHOTversion>
10     parent>
11     <artifactId>sharp-commonartifactId>
12     <packaging>jarpackaging>
13 
14     <name>sharp-commonname>
15     <description>公共模块description>
16 
17 
18     <dependencies>
19      <dependency>
20          <groupId>org.springframework.bootgroupId>
21          <artifactId>spring-boot-starterartifactId>
22       dependency>
23  
24       <dependency>
25          <groupId>org.springframework.bootgroupId>
26          <artifactId>spring-boot-starter-testartifactId>
27          <scope>testscope>
28       dependency>
29     dependencies>
30     
46 project>
View Code

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.0modelVersion>

    <parent>
        <groupId>com.sharpgroupId>
        <artifactId>sharp-pomartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <artifactId>sharp-entityartifactId>
    <packaging>jarpackaging>

    <name>sharp-entityname>
    <description>Demo project for Spring Bootdescription>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency> 
        <dependency>
            <groupId>com.sharpgroupId>
            <artifactId>sharp-commonartifactId>
            <version>${project.parent.version}version>
        dependency>
    dependencies>

     

project>
View Code

mapper项目无特别之处

springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目..._第4张图片

pom

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5     <parent>
 6         <groupId>com.sharpgroupId>
 7         <artifactId>sharp-pomartifactId>
 8         <version>0.0.1-SNAPSHOTversion>
 9     parent>
10     <artifactId>sharp-mapperartifactId>
11     <packaging>jarpackaging>
12 
13     <name>sharp-mappername>
14     <description>Demo project for Spring Bootdescription>
15 
16     <dependencies>
17             <dependency>
18             <groupId>com.sharpgroupId>
19             <artifactId>sharp-commonartifactId>
20             <version>${project.parent.version}version>
21         dependency>
22         <dependency>
23             <groupId>com.sharpgroupId>
24             <artifactId>sharp-entityartifactId>
25             <version>${project.parent.version}version>
26         dependency>
27         <dependency>
28             <groupId>org.springframework.bootgroupId>
29             <artifactId>spring-boot-starterartifactId>
30         dependency>
31         <dependency>
32             <groupId>org.mybatis.spring.bootgroupId>
33             <artifactId>mybatis-spring-boot-starterartifactId>
34         dependency>
35         <dependency>
36             <groupId>org.springframework.bootgroupId>
37             <artifactId>spring-boot-starter-testartifactId>
38             <scope>testscope>
39         dependency>
40     dependencies>
41 
42     
59 
60 project>
View Code

service-api  pom

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5 
 6     <artifactId>sharp-service-apiartifactId>
 7     <packaging>jarpackaging>
 8 
 9     <name>sharp-service-apiname>
10     <description>Demo project for Spring Bootdescription>
11 
12     <parent>
13         <groupId>com.sharpgroupId>
14         <artifactId>sharp-pomartifactId>
15         <version>0.0.1-SNAPSHOTversion>
16     parent>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
21         <java.version>1.8java.version>
22     properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.bootgroupId>
27             <artifactId>spring-boot-starterartifactId>
28         dependency>
29         <dependency>
30         <groupId>com.sharpgroupId>
31         <artifactId>sharp-entityartifactId>
32         <version>${project.parent.version}version>
33         dependency>
34         <dependency>
35             <groupId>org.springframework.bootgroupId>
36             <artifactId>spring-boot-starter-testartifactId>
37             <scope>testscope>
38         dependency>
39     dependencies>
40 
41 
42 project>
View Code

service pom

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5     <parent>
 6         <groupId>com.sharpgroupId>
 7         <artifactId>sharp-pomartifactId>
 8         <version>0.0.1-SNAPSHOTversion>
 9     parent>
10     <artifactId>sharp-serviceartifactId>
11     <packaging>jarpackaging>
12 
13     <name>sharp-sevicename>
14     <description>Demo project for Spring Bootdescription>
15     <dependencies>
16         
28         <dependency>
29             <groupId>org.springframework.bootgroupId>
30             <artifactId>spring-boot-starter-webartifactId>
31         dependency>
32          <dependency>
33             <groupId>com.alibabagroupId>
34             <artifactId>druidartifactId>
35         dependency>
36         <dependency>
37             <groupId>org.springframeworkgroupId>
38             <artifactId>spring-jdbcartifactId>
39         dependency>
40         <dependency>
41         <groupId>com.sharpgroupId>
42         <artifactId>sharp-entityartifactId>
43         <version>${project.parent.version}version>
44         dependency>
45         <dependency>
46         <groupId>com.sharpgroupId>
47         <artifactId>sharp-commonartifactId>
48         <version>${project.parent.version}version>
49         dependency>
50         <dependency>
51         <groupId>com.sharpgroupId>
52         <artifactId>sharp-mapperartifactId>
53         <version>${project.parent.version}version>
54         dependency>
55         <dependency>
56         <groupId>com.sharpgroupId>
57         <artifactId>sharp-service-apiartifactId>
58         <version>${project.parent.version}version>
59         dependency>
60         <dependency>
61             <groupId>org.mybatis.spring.bootgroupId>
62             <artifactId>mybatis-spring-boot-starterartifactId>
63         dependency>
64         <dependency>
65             <groupId>mysqlgroupId>
66             <artifactId>mysql-connector-javaartifactId>
67         dependency>
68 
69         <dependency>
70             <groupId>org.springframework.bootgroupId>
71             <artifactId>spring-boot-starter-testartifactId>
72             <scope>testscope>
73         dependency>
74     dependencies>
75 
76     
84 
85 
86 project>
View Code

 web user pom

 1 xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0modelVersion>
 5 
 6     <artifactId>sharp-userartifactId>
 7     <packaging>jarpackaging>
 8 
 9     <name>sharp-username>
10     <description>Demo project for Spring Bootdescription>
11 
12     <parent>
13         <groupId>com.sharpgroupId>
14     <artifactId>sharp-pomartifactId>
15     <version>0.0.1-SNAPSHOTversion>
16     parent>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
21         <java.version>1.8java.version>
22     properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>com.alibabagroupId>
27             <artifactId>druidartifactId>
28         dependency>
29         <dependency>
30             <groupId>org.springframework.bootgroupId>
31             <artifactId>spring-boot-starter-webartifactId>
32         dependency>
33         <dependency>
34             <groupId>com.sharpgroupId>
35             <artifactId>sharp-entityartifactId>
36             <version>0.0.1-SNAPSHOTversion>
37         dependency>
38         <dependency>
39             <groupId>com.sharpgroupId>
40             <artifactId>sharp-commonartifactId>
41             <version>0.0.1-SNAPSHOTversion>
42         dependency>
43         <dependency>
44             <groupId>com.sharpgroupId>
45             <artifactId>sharp-service-apiartifactId>
46             <version>0.0.1-SNAPSHOTversion>
47         dependency>
48         
49         <dependency>
50             <groupId>com.sharpgroupId>
51             <artifactId>sharp-serviceartifactId>
52             <version>0.0.1-SNAPSHOTversion>
53         dependency>
54         <dependency>
55             <groupId>com.sharpgroupId>
56             <artifactId>sharp-mapperartifactId>
57             <version>0.0.1-SNAPSHOTversion>
58         dependency>
59         <dependency>
60             <groupId>org.springframework.bootgroupId>
61             <artifactId>spring-boot-starter-testartifactId>
62             <scope>testscope>
63         dependency>
64     dependencies>
65 
66     <build>
67         <plugins>
68             <plugin>
69                 <groupId>org.springframework.bootgroupId>
70                 <artifactId>spring-boot-maven-pluginartifactId>
71             plugin>
72         plugins>
73     build>
74 
75 
76 project>
View Code

application.properties

 1 spring.profiles.active=local
 2 
 3 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 5 spring.datasource.url=jdbc:mysql://192.168.xxx.xxx:3306/sharp
 6 spring.datasource.username=root
 7 spring.datasource.password=********
 8 
 9 #mybatis配置
10 #指定全局配置文件位置
11 mybatis.config-location=classpath:mybatis/mybatis-config.xml
12 #指定别名包
13 mybatis.type-aliases-package=com.sharp.forward.entity
14 #指定xml文件位置
15 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
View Code

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

springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目..._第5张图片

 测试OK!

你可能感兴趣的:(springboot的maven多模块项目架构微服务搭建--跳过springmvc单一项目直接构建多模块并衍化为微服务项目...)