如图为基于maven搭建的 微服务springboot多模块项目经典模式图
图中,“父模块”聚合了多个子模块,包括api、model、及server模块(当然在实际项目中可以有更多的模块,而且模块的命名可以有所不同)。这三个模块的依赖层级关系是:server依赖model,model依赖api,最终构成了典型的Maven聚合型多模块项目。
SpringBoot项目搭建流程
打开IDEA开发软件,然后选择File->New>New Project命令,即创建新项目。
点击图中的Next按钮,进入Maven多项目模块的命名界面,将坐标命名填写完整。
最后点击inish按钮。进入项目的初始化页面。
指定整个项目资源的编码及项目编译时采用的JDK版本。。配置信息如下:
<!--定义项目整体资源编码及JDk 配置信息为1.8-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
配置好以后,开始创建各个子模块及每个子模块最基本的一些依赖配置信息,首先是创建子模块api.
依次选择model选项,并填写名称,点击next,最终成功创建。
创建完成可以查看到父级POM文件多了 配置>api
然后我们在api模块的pom文件里加入整个项目都将使用的依赖配置,即Lombok和 Jackson解析依赖。注意:使用Lombok之前你使用的IDEA已经安装了Lombok插件。
<properties>
<lombok.version>1.16.6</lombok.version>
<jackson-annotations-version>2.6.5</jackson-annotations-version>
</properties>
<dependencies>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations-version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
配置完成。再按照上面创建api子模块的方式,同样去创建子模块model。 点击项目,选择右键快捷菜单new命令,创建子模块model,并添加api子模块及spring-Mybatis依赖。具体操作如下:
然后子模块model依赖信息配置如下:
<properties>
<mybatis-spring-boot.version>1.1.1</mybatis-spring-boot.version>
<mybatis-pagehelper.version>4.1.2</mybatis-pagehelper.version>
</properties>
<dependencies>
<!--api-->
<dependency>
<groupId>com.debug.middleware</groupId>
<artifactId>api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!--spring-mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
</dependencies>
按照同样的方式创建最后一个模块,即核心的server模块。可以说一个项目或者服务的大部分业务逻辑代码都将在这个模块中完成。最终新建完成的server模块如图所示:
其中在server模块中加入了相关的依赖包括spring Boot依赖,日志log4j,及mysql、druid等最基本的依赖配置。详细信息如下:
<?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">
<parent>
<artifactId>middleware</artifactId>
<groupId>com.debug.middleware</groupId>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server</artifactId>
<packaging>jar</packaging>
<properties>
<start-class>com.debug.middleware.server.MainApplication</start-class>
<spring-boot.version>1.5.6.RELEASE</spring-boot.version>
<spring-session.version>1.2.0.RELEASE</spring-session.version>
<mysql.version>8.0.15</mysql.version>
<druid.version>1.0.16</druid.version>
<guava.version>19.0</guava.version>
</properties>
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--日志-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
<!--model-->
<dependency>
<groupId>com.debug.middleware</groupId>
<artifactId>model</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!--guava-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.3.RELEASE</version>
</dependency>
<!--for test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!--<scope>test</scope>-->
</dependency>
</dependencies>
<build>
<finalName>book_middleware_${project.parent.version}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
至此,项目的各个模块已经初步搭建好了。其中,在server模块中指定了整个项目的启动类(也称应用接口)MainApplication。为了能使用Springboot内置容器将整个项目“跑”起来,需要对MainApplication进行改造。改造后的代码如下:
package com.debug.middleware.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @className: 启动类
* @PackageName: com.debug.middleware.server
* @author: youjp
* @create: 2020-03-29 16:00
* @description: TODO 启动类
* @Version: 1.0
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
其中注解@SpringootApplication 用于表示被注解的类为整个应用的入口启动类,到时候只需要单击该左边的运行按钮,即可将整个应用运行起来。
为了将项目最终发步为完整的项目,我们为项目引入application.yml配置文件。
spring:
profiles:
active: test
application:
name: middleware
main:
allow-bean-definition-overriding: true
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
jmx:
enabled: false
server:
port: 9031
#logging\u65E5\u5FD7\u914D\u7F6E
# mybatis 配置
# mappers.xml文件配置
mybatis:
mapper-locations: classpath*:mybatis/mappers/**/**/*.xml
# PO包路径配置
type-aliases-package: com.deug.middleware
# 驼峰命名转换xx_xx xxXx
configuration:
map-underscore-to-camel-case: true
logging:
path: ./logs/${spring.application.name}
file: middleware
level:
org:
springframework: info
com:
fasterxml:
jackson: info
debug:
middleware: debug
application-test.yml
spring:
# 数据库配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_middleware?serverTimezone=GMT%2b8&characterEncoding=utf-8
username: root
password: 123456
日志配置文件:log4j.properties ,放在resource文件下即可
#Console Log
log4j.rootLogger=INFO,console,debug,info,warn,error
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - %5p [%t] --- %c{1}: %m%n
#A1--Print log to Console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=${LOG_PATTERN}
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.Threshold=INFO
log4j.appender.info.File=${LOG_PATH}/${LOG_FILE}_info.log
log4j.appender.info.DatePattern='.'yyyy-MM-dd
log4j.appender.info.layout = org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=${LOG_PATTERN}
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.Threshold=ERROR
log4j.appender.error.File=${LOG_PATH}/${LOG_FILE}_error.log
log4j.appender.error.DatePattern='.'yyyy-MM-dd
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=${LOG_PATTERN}
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.File=${LOG_PATH}/${LOG_FILE}_debug.log
log4j.appender.debug.DatePattern='.'yyyy-MM-dd
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=${LOG_PATTERN}
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.File=${LOG_PATH}/${LOG_FILE}_warn.log
log4j.appender.warn.DatePattern='.'yyyy-MM-dd
log4j.appender.warn.layout = org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=${LOG_PATTERN}
最终server模块下resources目录的配置文件包含了3个文件。如图所示:
至此整个应用搭建 就完成了。
写个hello World吧
接下来写个hello word,用于检查上一节搭建的项目所发布的接口是否能被正常访问。
1.在server模块下分别创建controller包和entity包。并在controller包下创建BookController 类,在entity包下创建Book类。
BookController类开发的代码如下:
package com.debug.middleware.server.controller;
import com.debug.middleware.server.entity.Book;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @className:
* @PackageName: com.debug.middleware.server.controller
* @author: youjp
* @create: 2020-03-29 22:21
* @description: TODO 书籍控制器
* @Version: 1.0
*/
@RestController
@RequestMapping("/book")
public class BookController {
private static final Logger log=LoggerFactory.getLogger(BookController.class);
@RequestMapping(value = "info",method = RequestMethod.GET)
public Book info(Integer no, String name){
Book book=new Book();
log.info("no:"+no+"name:"+name);
book.setBookNo(no);
book.setName(name);
return book;
}
}
Book类:
package com.debug.middleware.server.entity;
import lombok.Data;
/**
* @className:
* @PackageName: com.debug.middleware.server.entity
* @author: youjp
* @create: 2020-03-29 22:19
* @description: TODO
* @Version: 1.0
*/
@Data
public class Book {
/**
* 书编号
*/
private Integer bookNo;
/**
* 书名
*/
private String name;
}
点击MainApplication启动项目。然后打开浏览器请求,地址栏输入
http://localhost:9031/book/info?no=1&name=jp 可查看结果: