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

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

springboot多模块分层 springboot 多模块依赖_apache

裁剪后如右侧

springboot多模块分层 springboot 多模块依赖_apache_02

springboot多模块分层 springboot 多模块依赖_springboot多模块分层_03

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

接下来对各个模块进行处理

sharp-pom 只需要保留pom.xml文件即可,注意打包方式一定选择pom,具体内容如下:

springboot多模块分层 springboot 多模块依赖_spring_04

springboot多模块分层 springboot 多模块依赖_spring_05

1 <?xml version="1.0" encoding="UTF-8"?>
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
6 com.sharp
7 sharp-pom
8 0.0.1-SNAPSHOT
9 pom
11 sharp-pom
12 Demo project for Spring Boot
 
15 org.springframework.boot
16 spring-boot-starter-parent
17 2.1.0.RELEASE

22 UTF-8
23 UTF-8
24 0.0.1-SNAPSHOT
25 1.8
26 8.0.12
27 1.3.2
28 5.1.1.RELEASE
29 1.1.10
30 true

39 com.alibaba
40 druid
41 ${druid.version}
 
44 org.springframework
45 spring-jdbc
46 ${jdbc.version}
 
49 mysql
50 mysql-connector-java
51 ${mysql.version}
 
54 org.mybatis.spring.boot
55 mybatis-spring-boot-starter
56 ${mybatis.spring.version}
 
63 my-release
64 http://192.168.135.128:8081/repository/maven-releases/
 
67 my-snapshots
68 http://192.168.135.128:8081/repository/maven-snapshots/
 
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暂时无其他内容

springboot多模块分层 springboot 多模块依赖_spring_04

springboot多模块分层 springboot 多模块依赖_spring_05

1 <?xml version="1.0" encoding="UTF-8"?>
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
 
7 com.sharp
8 sharp-pom
9 0.0.1-SNAPSHOT
 
11 sharp-common
12 jar
14 sharp-common
15 公共模块

20 org.springframework.boot
21 spring-boot-starter
 
25 org.springframework.boot
26 spring-boot-starter-test
27 test
 
View Code
entity pom

springboot多模块分层 springboot 多模块依赖_spring_04

springboot多模块分层 springboot 多模块依赖_spring_05

4.0.0
com.sharp
sharp-pom
0.0.1-SNAPSHOT
sharp-entity
jar
sharp-entity
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
com.sharp
sharp-common
${project.parent.version}
View Code

mapper项目无特别之处

springboot多模块分层 springboot 多模块依赖_spring_10

pom

springboot多模块分层 springboot 多模块依赖_spring_04

springboot多模块分层 springboot 多模块依赖_spring_05

1 <?xml version="1.0" encoding="UTF-8"?>
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
6 com.sharp
7 sharp-pom
8 0.0.1-SNAPSHOT
10 sharp-mapper
11 jar
13 sharp-mapper
14 Demo project for Spring Boot 
18 com.sharp
19 sharp-common
20 ${project.parent.version}
 
23 com.sharp
24 sharp-entity
25 ${project.parent.version}
 
28 org.springframework.boot
29 spring-boot-starter
32 org.mybatis.spring.boot
33 mybatis-spring-boot-starter
 
36 org.springframework.boot
37 spring-boot-starter-test
38 test
 
View Code
service-api  pom

springboot多模块分层 springboot 多模块依赖_spring_04

springboot多模块分层 springboot 多模块依赖_spring_05

1 <?xml version="1.0" encoding="UTF-8"?>
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
6 sharp-service-api
7 jar
9 sharp-service-api
10 Demo project for Spring Boot 
13 com.sharp
14 sharp-pom
15 0.0.1-SNAPSHOT
 
19 UTF-8
20 UTF-8
 
26 org.springframework.boot
27 spring-boot-starter
 
30 com.sharp
31 sharp-entity
32 ${project.parent.version}
 
35 org.springframework.boot
36 spring-boot-starter-test
37 test
 
View Code
service pom

springboot多模块分层 springboot 多模块依赖_spring_04

springboot多模块分层 springboot 多模块依赖_spring_05

1 <?xml version="1.0" encoding="UTF-8"?>
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
om.sharp
7 sharp-pom
8 0.0.1-SNAPSHOT
10 sharp-service
11 jar
13 sharp-sevice
14 Demo project for Spring Boot
 
29 org.springframework.boot
30 spring-boot-starter-web
 
33 com.alibaba
34 druid
 
37 org.springframework
38 spring-jdbc
41 com.sharp
42 sharp-entity
43 ${project.parent.version}
 
46 com.sharp
47 sharp-common
48 ${project.parent.version}
 
51 com.sharp
52 sharp-mapper
53 ${project.parent.version}
 
56 com.sharp
57 sharp-service-api
58 ${project.parent.version}
 
61 org.mybatis.spring.boot
62 mybatis-spring-boot-starter
 
65 mysql
66 mysql-connector-java
 
70 org.springframework.boot
71 spring-boot-starter-test
72 test
 
View Code
web user pom

springboot多模块分层 springboot 多模块依赖_spring_05

1 <?xml version="1.0" encoding="UTF-8"?>
 
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4.0.0
6 sharp-user
7 jar
9 sharp-user
10 Demo project for Spring Boot
 com.sharp
14 sharp-pom
15 0.0.1-SNAPSHOT
 
19 UTF-8
20 UTF-8
21 1.8 
26 com.alibaba
27 druid
 
30 org.springframework.boot
31 spring-boot-starter-web
34 com.sharp
35 sharp-entity
36 0.0.1-SNAPSHOT
 
39 com.sharp
40 sharp-common
41 0.0.1-SNAPSHOT
 
44 com.sharp
45 sharp-service-api
46 0.0.1-SNAPSHOT
 
50 com.sharp
51 sharp-service
52 0.0.1-SNAPSHOT
55 com.sharp
56 sharp-mapper
57 0.0.1-SNAPSHOT
 
60 org.springframework.boot
61 spring-boot-starter-test
62 test
 
69 org.springframework.boot
70 spring-boot-maven-plugin
 
View Code
application.properties

springboot多模块分层 springboot 多模块依赖_spring_05

1 spring.profiles.active=local2
3 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver5 spring.datasource.url=jdbc:mysql://192.168.xxx.xxx:3306/sharp6 spring.datasource.username=root7 spring.datasource.password=********8
9 #mybatis配置10 #指定全局配置文件位置11 mybatis.config-location=classpath:mybatis/mybatis-config.xml12 #指定别名包13 mybatis.type-aliases-package=com.sharp.forward.entity14 #指定xml文件位置15 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
View Code

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

springboot多模块分层 springboot 多模块依赖_spring_19

测试OK!