環境:java1.8,ideajava
聚合工程優點:web
1.統一maven操做。能夠在一個maven工程管理多個子工程(每一個子工程可單獨打包,重啓,調試。也可經過聚合工程一塊兒管理)。redis
2.統一管理依賴版本。能夠藉助父工程(dependencyManagement)來管理依賴包的版本,子工程就直接引用包而不用添加版本信息。spring
3.統一引入公共依賴,而不須要每一個子項目都去重複引入。apache
4.防止pom.xml過長。json
指望達到的效果:瀏覽器
1.新建maven聚合父工程project-aggregation
file-new-project-maven服務器
快速建立一個maven工程,刪除無關文件,只保留pom.xmlmybatis
pom.xml文件內容以下app
<?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"> <!-- 基本信息 --> <description>SpringBoot 多模塊構建示例</description> <modelVersion>4.0.0</modelVersion> <name>project-aggregation</name> <packaging>pom</packaging> <!-- 項目說明:這裏做爲聚合工程的父工程 --> <groupId>com.pu</groupId> <artifactId>project-aggregation</artifactId> <version>1.0-SNAPSHOT</version> <!-- 繼承說明:這裏繼承SpringBoot提供的父工程 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent> <!-- 模塊說明:這裏聲明多個子模塊 --> <modules> <module>helloworld</module> <module>mybatistest</module> </modules> <!-- 在properties中統一控制依賴包的版本,更清晰--> <properties> <java.version>1.8</java.version> <fastjson.version>1.2.32</fastjson.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> <feign.version>2.1.1.RELEASE</feign.version> <eureka-client.version>2.1.1.RELEASE</eureka-client.version> <zkclient.version>0.10</zkclient.version> <poi.version>3.13</poi.version> <lombok.version>1.18.6</lombok.version> </properties> <!--在此處統一加,子項目pom就能夠不用引入了--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> </dependencies> <!--dependencyManagement用於管理依賴版本號--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>${eureka-client.version}</version> </dependency> <!--feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${feign.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement> <!--該插件做用是打一個可運行的包,必需要寫在須要打包的項目裏。這裏的父模塊不須要打包運行,因此刪掉該插件。--> <!--<build>--> <!--<plugins>--> <!--<plugin>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-maven-plugin</artifactId>--> <!--</plugin>--> <!--</plugins>--> <!--</build>--> </project>
2.建子模塊module項目helloworld和mybatistest
父工程右鍵--》new-->module
finish,
helloworld的pom.xml
<?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.pu</groupId> <artifactId>helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>helloworld</name> <description>Demo project for Spring Boot</description> <!-- 繼承本項目的父工程 --> <parent> <groupId>com.pu</groupId> <artifactId>project-aggregation</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath><!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
同理建另外一個module mybatistest,它的pom.xml
<?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.example</groupId> <artifactId>mybatistest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatistest</name> <description>Demo project for Spring Boot</description> <!-- 繼承本項目的父工程 --> <parent> <groupId>com.pu</groupId> <artifactId>project-aggregation</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
至此,聚合工程建立完畢,目錄結構以下:
3.編寫測試類
helloController.java
package com.pu.helloworld; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class helloController { @RequestMapping(value="/hello") //required=false 表示url中能夠不傳入id參數,此時就使用默認參數 public String say(@RequestParam(value="id",required=false,defaultValue = "hello world") String input){ return "your input :"+input; } }
4.啓動測試
url測試
idea菜單:Tools-->HTTP Client-->Test RESTful Web Service
單擊左上角運行
5.打包helloworld
helloworld工程目錄
依次點clean-->compile-->package
結果
壓縮軟件打開
相關jar包已經打進去了
配置文件
配置pom仍是helloworld的pom.xml,並無添加父pom內容,由於上面打包時已經把所須要的jar包打進去了
6.服務器發佈
[testapp@localhost ~/javaProject/service-helloworld]$ pwd /home/ap/testapp/javaProject/service-helloworld [testapp@localhost ~/javaProject/service-helloworld]$ ll 總用量 52336 -rw-rw-r-- 1 testapp testapp 524 6月 19 15:49 application.yml -rw-rw-r-- 1 testapp testapp 53573143 6月 19 15:48 helloworld-0.0.1-SNAPSHOT.jar drwxrwxr-x 2 testapp testapp 21 6月 19 15:55 log -rw-rw-r-- 1 testapp testapp 3209 6月 19 15:49 logback-boot.xml -rw-rw-r-- 1 testapp testapp 249 6月 19 15:54 start.sh -rw-rw-r-- 1 testapp testapp 92 6月 19 15:51 stop.sh [testapp@localhost ~/javaProject/service-helloworld]$ cat application.yml spring: application: name: service-helloword server: port: 8704 eureka: client: serviceUrl: #指向註冊中心 defaultZone: http://192.168.111.133:8888/eureka/ instance: # 每間隔1s,向服務端發送一次心跳,證實本身依然」存活「 lease-renewal-interval-in-seconds: 1 # 告訴服務端,若是我2s以內沒有給你發心跳,就表明我「死」了,將我踢出掉。 lease-expiration-duration-in-seconds: 2 logging: config: classpath:logback-boot.xml[testapp@localhost ~/javaProject/service-helloworld]$ cat start.sh nohup java -jar /home/ap/testapp/javaProject/service-helloworld/helloworld-0.0.1-SNAPSHOT.jar --spring.config.location=/home/ap/testapp/javaProject/service-helloworld/application.yml --spring.application.index=1 --server.port=8704 >/dev/null 2>&1 & [testapp@localhost ~/javaProject/service-helloworld]$ sh start.sh [testapp@localhost ~/javaProject/service-helloworld]$ cd log [testapp@localhost ~/javaProject/service-helloworld/log]$ cat sys.log 2019-06-19 15:55:17,106 INFO (SpringApplication.java:675)- No active profile set, falling back to default profiles: default 2019-06-19 15:55:38,897 INFO (StartupInfoLogger.java:59)- Started HelloworldApplication in 44.58 seconds (JVM running for 60.889) 2019-06-19 15:57:14,643 DEBUG (helloController.java:32)- your input :hello world [testapp@localhost ~/javaProject/service-helloworld/log]$
註冊中心
瀏覽器測試
附
logback-boot.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- %m輸出的信息,%p日誌級別,%t線程名,%d日期,%c類的全名,%i索引【從數字0開始遞增】,,, --> <!-- appender是configuration的子節點,是負責寫日誌的組件。 --> <!-- ConsoleAppender:把日誌輸出到控制檯 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d %p (%file:%line\)- %m%n</pattern> <!-- 控制檯也要使用UTF-8,不要使用GBK,不然會中文亂碼 --> <charset>UTF-8</charset> </encoder> </appender> <!-- RollingFileAppender:滾動記錄文件,先將日誌記錄到指定文件,當符合某個條件時,將日誌記錄到其餘文件 --> <!-- 如下的大概意思是:1.先按日期存日誌,日期變了,將前一天的日誌文件名重命名爲XXX%日期%索引,新的日誌仍然是sys.log --> <!-- 2.若是日期沒有發生變化,可是當前日誌的文件大小超過1KB時,對當前日誌進行分割 重命名--> <appender name="syslog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>log/sys.log</File> <!-- rollingPolicy:當發生滾動時,決定 RollingFileAppender 的行爲,涉及文件移動和重命名。 --> <!-- TimeBasedRollingPolicy: 最經常使用的滾動策略,它根據時間來制定滾動策略,既負責滾動也負責出發滾動 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 活動文件的名字會根據fileNamePattern的值,每隔一段時間改變一次 --> <!-- 文件名:log/sys.2017-12-05.0.log --> <fileNamePattern>log/sys.%d.%i.log</fileNamePattern> <!-- 每產生一個日誌文件,該日誌文件的保存期限爲30天 --> <maxHistory>30</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- maxFileSize:這是活動文件的大小,默認值是10MB,本篇設置爲1KB,只是爲了演示 --> <maxFileSize>1KB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <!-- pattern節點,用來設置日誌的輸入格式 --> <pattern> %d %p (%file:%line\)- %m%n </pattern> <!-- 記錄日誌的編碼 --> <charset>UTF-8</charset> <!-- 此處設置字符集 --> </encoder> </appender> <!-- 控制檯輸出日誌級別 --> <root level="info"> <appender-ref ref="STDOUT"/> </root> <!-- 指定項目中某個包,當有日誌操做行爲時的日誌記錄級別 --> <!-- com.appley爲根包,也就是隻要是發生在這個根包下面的全部日誌操做行爲的權限都是DEBUG --> <!-- 級別依次爲【從高到低】:FATAL > ERROR > WARN > INFO > DEBUG > TRACE --> <logger name="com.pu" level="DEBUG"> <appender-ref ref="syslog"/> </logger> </configuration>
application.yml
spring: application: name: service-helloword server: port: 8704 eureka: client: serviceUrl: #指向註冊中心 defaultZone: http://192.168.111.133:8888/eureka/ instance: # 每間隔1s,向服務端發送一次心跳,證實本身依然」存活「 lease-renewal-interval-in-seconds: 1 # 告訴服務端,若是我2s以內沒有給你發心跳,就表明我「死」了,將我踢出掉。 lease-expiration-duration-in-seconds: 2 logging: config: classpath:logback-boot.xml