总想对微服务架构做一个小小的总结,不知如何下手,最近觉得还是从搭建微服务的过程来入手,对于springboot的maven项目从构建多模块架构进而衍化为常用的微服务架构来做个记录吧。
首先,创建多个springboot项目,项目结构如图:
裁剪后如右侧
创建完成后,先解释一下: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.0</modelVersion>
5
6 <groupId>com.sharp</groupId>
7 <artifactId>sharp-pom</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 <packaging>pom</packaging>
10
11 <name>sharp-pom</name>
12 <description>Demo project for Spring Boot</description>
13
14 <parent>
15 <groupId>org.springframework.boot</groupId>
16 <artifactId>spring-boot-starter-parent</artifactId>
17 <version>2.1.0.RELEASE</version>
18 <relativePath/>
19 </parent>
20
21 <properties>
22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24 <project.version>0.0.1-SNAPSHOT</project.version>
25 <java.version>1.8</java.version>
26 <mysql.version>8.0.12</mysql.version>
27 <mybatis.spring.version>1.3.2</mybatis.spring.version>
28 <jdbc.version>5.1.1.RELEASE</jdbc.version>
29 <druid.version>1.1.10</druid.version>
30 <skipTests>true</skipTests>
31 </properties>
32 <!-- dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)dependencyManagement里只是声明依赖,并不实现引入,
33 因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会
34 从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。 -->
35 <!-- 这里只是声明,子项目中用到时候应用不需要再次引入版本号,统一在此管理了版本 -->
36 <dependencyManagement>
37 <dependencies>
38 <dependency>
39 <groupId>com.alibaba</groupId>
40 <artifactId>druid</artifactId>
41 <version>${druid.version}</version>
42 </dependency>
43 <dependency>
44 <groupId>org.springframework</groupId>
45 <artifactId>spring-jdbc</artifactId>
46 <version>${jdbc.version}</version>
47 </dependency>
48 <dependency>
49 <groupId>mysql</groupId>
50 <artifactId>mysql-connector-java</artifactId>
51 <version>${mysql.version}</version>
52 </dependency>
53 <dependency>
54 <groupId>org.mybatis.spring.boot</groupId>
55 <artifactId>mybatis-spring-boot-starter</artifactId>
56 <version>${mybatis.spring.version}</version>
57 </dependency>
58 </dependencies>
59 <!-- 私服配置,子项目继承后不需要在子项目中配置 -->
60 </dependencyManagement>
61 <distributionManagement>
62 <repository>
63 <id>my-release</id>
64 <url>http://192.168.135.128:8081/repository/maven-releases/</url>
65 </repository>
66 <snapshotRepository>
67 <id>my-snapshots</id>
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] 找不到符号、程序包。。。不存在之类的,因为只是一个普通包,我们直接把它干掉就行
然后层层依赖,没有什么特别的,需要注意的是,非微服务的多模块,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.0</modelVersion>
5
6 <parent>
7 <groupId>com.sharp</groupId>
8 <artifactId>sharp-pom</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 </parent>
11 <artifactId>sharp-common</artifactId>
12 <packaging>jar</packaging>
13
14 <name>sharp-common</name>
15 <description>公共模块</description>
16
17
18 <dependencies>
19 <dependency>
20 <groupId>org.springframework.boot</groupId>
21 <artifactId>spring-boot-starter</artifactId>
22 </dependency>
23
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-test</artifactId>
27 <scope>test</scope>
28 </dependency>
29 </dependencies>
30 <!-- <build>
31 <plugins>
32 <plugin>
33 <groupId>org.springframework.boot</groupId>
34 <artifactId>spring-boot-maven-plugin</artifactId>
35 </plugin>
36 <plugin>
37 <groupId>org.apache.maven.plugins</groupId>
38 <artifactId>maven-surefire-plugin</artifactId>
39 <version>2.6</version>
40 <configuration>
41 <skipTests>true</skipTests>
42 </configuration>
43 </plugin>
44 </plugins>
45 </build> -->
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.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>
View Code
mapper项目无特别之处
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.0</modelVersion>
5 <parent>
6 <groupId>com.sharp</groupId>
7 <artifactId>sharp-pom</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 </parent>
10 <artifactId>sharp-mapper</artifactId>
11 <packaging>jar</packaging>
12
13 <name>sharp-mapper</name>
14 <description>Demo project for Spring Boot</description>
15
16 <dependencies>
17 <dependency>
18 <groupId>com.sharp</groupId>
19 <artifactId>sharp-common</artifactId>
20 <version>${project.parent.version}</version>
21 </dependency>
22 <dependency>
23 <groupId>com.sharp</groupId>
24 <artifactId>sharp-entity</artifactId>
25 <version>${project.parent.version}</version>
26 </dependency>
27 <dependency>
28 <groupId>org.springframework.boot</groupId>
29 <artifactId>spring-boot-starter</artifactId>
30 </dependency>
31 <dependency>
32 <groupId>org.mybatis.spring.boot</groupId>
33 <artifactId>mybatis-spring-boot-starter</artifactId>
34 </dependency>
35 <dependency>
36 <groupId>org.springframework.boot</groupId>
37 <artifactId>spring-boot-starter-test</artifactId>
38 <scope>test</scope>
39 </dependency>
40 </dependencies>
41
42 <!-- <build>
43 <plugins>
44 <plugin>
45 <groupId>org.springframework.boot</groupId>
46 <artifactId>spring-boot-maven-plugin</artifactId>
47 </plugin>
48 <plugin>
49 <groupId>org.apache.maven.plugins</groupId>
50 <artifactId>maven-surefire-plugin</artifactId>
51 <version>2.6</version>
52 <configuration>
53 <skipTests>true</skipTests>
54 </configuration>
55 </plugin>
56 </plugins>
57 </build>
58 -->
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.0</modelVersion>
5
6 <artifactId>sharp-service-api</artifactId>
7 <packaging>jar</packaging>
8
9 <name>sharp-service-api</name>
10 <description>Demo project for Spring Boot</description>
11
12 <parent>
13 <groupId>com.sharp</groupId>
14 <artifactId>sharp-pom</artifactId>
15 <version>0.0.1-SNAPSHOT</version>
16 </parent>
17
18 <properties>
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21 <java.version>1.8</java.version>
22 </properties>
23
24 <dependencies>
25 <dependency>
26 <groupId>org.springframework.boot</groupId>
27 <artifactId>spring-boot-starter</artifactId>
28 </dependency>
29 <dependency>
30 <groupId>com.sharp</groupId>
31 <artifactId>sharp-entity</artifactId>
32 <version>${project.parent.version}</version>
33 </dependency>
34 <dependency>
35 <groupId>org.springframework.boot</groupId>
36 <artifactId>spring-boot-starter-test</artifactId>
37 <scope>test</scope>
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.0</modelVersion>
5 <parent>
6 <groupId>com.sharp</groupId>
7 <artifactId>sharp-pom</artifactId>
8 <version>0.0.1-SNAPSHOT</version>
9 </parent>
10 <artifactId>sharp-service</artifactId>
11 <packaging>jar</packaging>
12
13 <name>sharp-sevice</name>
14 <description>Demo project for Spring Boot</description>
15 <dependencies>
16 <!-- <dependency>
17 <groupId>org.springframework.boot</groupId>
18 <artifactId>spring-boot-starter-amqp</artifactId>
19 </dependency>
20 <dependency>
21 <groupId>org.springframework.boot</groupId>
22 <artifactId>spring-boot-starter-data-mongodb</artifactId>
23 </dependency>
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-data-redis</artifactId>
27 </dependency> -->
28 <dependency>
29 <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-starter-web</artifactId>
31 </dependency>
32 <dependency>
33 <groupId>com.alibaba</groupId>
34 <artifactId>druid</artifactId>
35 </dependency>
36 <dependency>
37 <groupId>org.springframework</groupId>
38 <artifactId>spring-jdbc</artifactId>
39 </dependency>
40 <dependency>
41 <groupId>com.sharp</groupId>
42 <artifactId>sharp-entity</artifactId>
43 <version>${project.parent.version}</version>
44 </dependency>
45 <dependency>
46 <groupId>com.sharp</groupId>
47 <artifactId>sharp-common</artifactId>
48 <version>${project.parent.version}</version>
49 </dependency>
50 <dependency>
51 <groupId>com.sharp</groupId>
52 <artifactId>sharp-mapper</artifactId>
53 <version>${project.parent.version}</version>
54 </dependency>
55 <dependency>
56 <groupId>com.sharp</groupId>
57 <artifactId>sharp-service-api</artifactId>
58 <version>${project.parent.version}</version>
59 </dependency>
60 <dependency>
61 <groupId>org.mybatis.spring.boot</groupId>
62 <artifactId>mybatis-spring-boot-starter</artifactId>
63 </dependency>
64 <dependency>
65 <groupId>mysql</groupId>
66 <artifactId>mysql-connector-java</artifactId>
67 </dependency>
68
69 <dependency>
70 <groupId>org.springframework.boot</groupId>
71 <artifactId>spring-boot-starter-test</artifactId>
72 <scope>test</scope>
73 </dependency>
74 </dependencies>
75
76 <!-- <build>
77 <plugins>
78 <plugin>
79 <groupId>org.springframework.boot</groupId>
80 <artifactId>spring-boot-maven-plugin</artifactId>
81 </plugin>
82 </plugins>
83 </build> -->
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.0</modelVersion>
5
6 <artifactId>sharp-user</artifactId>
7 <packaging>jar</packaging>
8
9 <name>sharp-user</name>
10 <description>Demo project for Spring Boot</description>
11
12 <parent>
13 <groupId>com.sharp</groupId>
14 <artifactId>sharp-pom</artifactId>
15 <version>0.0.1-SNAPSHOT</version>
16 </parent>
17
18 <properties>
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21 <java.version>1.8</java.version>
22 </properties>
23
24 <dependencies>
25 <dependency>
26 <groupId>com.alibaba</groupId>
27 <artifactId>druid</artifactId>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-web</artifactId>
32 </dependency>
33 <dependency>
34 <groupId>com.sharp</groupId>
35 <artifactId>sharp-entity</artifactId>
36 <version>0.0.1-SNAPSHOT</version>
37 </dependency>
38 <dependency>
39 <groupId>com.sharp</groupId>
40 <artifactId>sharp-common</artifactId>
41 <version>0.0.1-SNAPSHOT</version>
42 </dependency>
43 <dependency>
44 <groupId>com.sharp</groupId>
45 <artifactId>sharp-service-api</artifactId>
46 <version>0.0.1-SNAPSHOT</version>
47 </dependency>
48 <!-- 微服务项目使不需要以下实现类的配置,只需要配置好dubbo即可 -->
49 <dependency>
50 <groupId>com.sharp</groupId>
51 <artifactId>sharp-service</artifactId>
52 <version>0.0.1-SNAPSHOT</version>
53 </dependency>
54 <dependency>
55 <groupId>com.sharp</groupId>
56 <artifactId>sharp-mapper</artifactId>
57 <version>0.0.1-SNAPSHOT</version>
58 </dependency>
59 <dependency>
60 <groupId>org.springframework.boot</groupId>
61 <artifactId>spring-boot-starter-test</artifactId>
62 <scope>test</scope>
63 </dependency>
64 </dependencies>
65
66 <build>
67 <plugins>
68 <plugin>
69 <groupId>org.springframework.boot</groupId>
70 <artifactId>spring-boot-maven-plugin</artifactId>
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"),其他都是常规的东西,结构如下:
测试OK!