
單體 Spring Boot Maven 工程
最基本的 pom.xml 包含工程信息、Spring Boot 父工程、屬性配置、依賴包、構(gòu)建插件
<?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.anoyi</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Spring Boot 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<properties>
<!-- 相關(guān)屬性、第三方依賴版本號 -->
</properties>
<dependencies>
<!-- Spring Boot 依賴、其他依賴 -->
</dependencies>
<!-- 構(gòu)建 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
微服務(wù)多 Spring Boot 應(yīng)用依賴關(guān)系管理

- 藍色:僅 pom.xml 文件,無代碼
- 黃色:包含 pom.xml 文件,一些具有通用性的代碼,如工具類等
- 綠色:Spring Boot 應(yīng)用工程,含有啟動類,與上述單體應(yīng)用類似
藍色:自定義 Parent
為避免微服務(wù)下包的濫用,應(yīng)該統(tǒng)一管理第三方依賴的版本,同時為了方便 mvn deploy 操作,可以加上公司內(nèi)部 Maven 私服的信息。
<?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.anoyi</groupId>
<artifactId>parent</artifactId>
<version>1.0.0.RELEASE</version>
<packaging>pom</packaging>
<!-- 繼承 Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<properties>
<!-- 第三方依賴版本號 -->
<common.version>1.0.0.RELEASE</commont.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 第三方依賴 -->
<dependency>
<groupId>com.anoyi</groupId>
<artifactId>common</artifactId>
<version>${common.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 公司內(nèi)部私有 Maven 倉庫 -->
<distributionManagement>
<repository>
<id>central</id>
<name>*****</name>
<url>*****</url>
</repository>
</distributionManagement>
</project>
常用操作
# 安裝到本地、推送到 Maven 私服
mvn clean install deploy
黃色:自定義依賴
比如一些通用的工具類包,為了避免代碼在不用項目的復(fù)制,可以制作成一個 Maven 模塊打包,用于其他項目引用。如果這個工具包還依賴了一些其他包,可以在上述 Parent 中統(tǒng)一管理這些包的版本。
<?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.anoyi</groupId>
<artifactId>common</artifactId>
<version>1.0.0.RELEASE</version>
<packaging>jar</packaging>
<parent>
<groupId>com.anoyi</groupId>
<artifactId>parent</artifactId>
<version>1.0.0.RELEASE</version>
</parent>
<!-- 其他屬性配置 -->
<properties>
<!-- 可選填 -->
</properties>
<!-- 公共依賴 -->
<dependencies>
<!-- 框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 工具類 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- 其他依賴 -->
</dependencies>
</project>
常用操作
# 安裝到本地、推送到 Maven 私服
mvn clean install deploy
構(gòu)建出來的 jar 包中僅包含編譯后的 class 文件及依賴關(guān)系,非常輕量!
綠色:Spring Boot Application
最終的目標是構(gòu)建出可運行的 jar 包,就需要打包所有依賴的代碼文件到一起,使用 Spring Boot Maven 插件就能輕易完成。
<?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.anoyi</groupId>
<artifactId>server-general</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 自定義 Parent 工程 -->
<parent>
<groupId>com.anoyi</groupId>
<artifactId>parent</artifactId>
<version>1.0.0.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot 依賴、自定義依賴 或 其他依賴 -->
<dependency>
<groupId>com.anoyi</groupId>
<artifactId>common</artifactId>
</dependency>
</dependencies>
<!-- 構(gòu)建可執(zhí)行的 jar 包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
常用操作
# 構(gòu)建可執(zhí)行 jar 包到 target 目錄
mvn clean package
業(yè)務(wù)代碼復(fù)用
解耦業(yè)務(wù),合理拆分微服務(wù)模塊,使用 RPC 框架,能有效的復(fù)用代碼。
輕量級微服務(wù)架構(gòu),容器化環(huán)境,PRC 框架可以使用 spring-boot-starter-grpc