单体 Spring Boot Maven 工程

最基本的 pom.xml 包含工程信息、Spring Boot 父工程、属性配置、依赖包、构建插件

  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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3.    <modelVersion>4.0.0</modelVersion>

  4.    <!-- 工程信息 -->

  5.    <groupId>com.anoyi</groupId>

  6.    <artifactId>demo</artifactId>

  7.    <version>1.0-SNAPSHOT</version>

  8.     <!-- Spring Boot 父工程 -->

  9.    <parent>

  10.        <groupId>org.springframework.boot</groupId>

  11.        <artifactId>spring-boot-starter-parent</artifactId>

  12.        <version>2.0.2.RELEASE</version>

  13.    </parent>

  14.    <properties>

  15.        <!-- 相关属性、第三方依赖版本号 -->

  16.    </properties>

  17.    <dependencies>

  18.        <!-- Spring Boot 依赖、其他依赖 -->

  19.    </dependencies>

  20.    <!-- 构建 -->

  21.    <build>

  22.        <plugins>

  23.            <plugin>

  24.                <groupId>org.springframework.boot</groupId>

  25.                <artifactId>spring-boot-maven-plugin</artifactId>

  26.            </plugin>

  27.        </plugins>

  28.    </build>

  29. </project>

微服务多 Spring Boot 应用依赖关系管理

  • 蓝色:仅 pom.xml 文件,无代码

  • 黄色:包含 pom.xml 文件,一些具有通用性的代码,如工具类等

  • 绿色:Spring Boot 应用工程,含有启动类,与上述单体应用类似

蓝色:自定义 Parent

为避免微服务下包的滥用,应该统一管理第三方依赖的版本,同时为了方便 mvn deploy 操作,可以加上公司内部 Maven 私服的信息。

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0"

  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  5.    <modelVersion>4.0.0</modelVersion>

  6.    <!-- 自定义 Parent -->

  7.    <groupId>com.anoyi</groupId>

  8.    <artifactId>parent</artifactId>

  9.    <version>1.0.0.RELEASE</version>

  10.    <packaging>pom</packaging>

  11.    <!-- 继承 Spring Boot -->

  12.    <parent>

  13.        <groupId>org.springframework.boot</groupId>

  14.        <artifactId>spring-boot-starter-parent</artifactId>

  15.        <version>2.0.2.RELEASE</version>

  16.    </parent>

  17.    <properties>

  18.        <!-- 第三方依赖版本号 -->

  19.        <common.version>1.0.0.RELEASE</commont.version>

  20.    </properties>

  21.    <dependencyManagement>

  22.        <dependencies>

  23.            <!-- 第三方依赖 -->

  24.            <dependency>

  25.                <groupId>com.anoyi</groupId>

  26.                <artifactId>common</artifactId>

  27.                <version>${common.version}</version>

  28.            </dependency>

  29.        </dependencies>

  30.    </dependencyManagement>

  31.    <!-- 公司内部私有 Maven 仓库 -->

  32.    <distributionManagement>

  33.        <repository>

  34.            <id>central</id>

  35.            <name>*****</name>

  36.            <url>*****</url>

  37.        </repository>

  38.    </distributionManagement>

  39. </project>

常用操作

  1. # 安装到本地、推送到 Maven 私服

  2. mvn clean install deploy

黄色:自定义依赖

比如一些通用的工具类包,为了避免代码在不用项目的复制,可以制作成一个 Maven 模块打包,用于其他项目引用。如果这个工具包还依赖了一些其他包,可以在上述 Parent 中统一管理这些包的版本。

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0"

  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  5.    <modelVersion>4.0.0</modelVersion>

  6.    <groupId>com.anoyi</groupId>

  7.    <artifactId>common</artifactId>

  8.    <version>1.0.0.RELEASE</version>

  9.    <packaging>jar</packaging>

  10.    <parent>

  11.        <groupId>com.anoyi</groupId>

  12.        <artifactId>parent</artifactId>

  13.        <version>1.0.0.RELEASE</version>

  14.    </parent>

  15.    <!-- 其他属性配置 -->

  16.    <properties>

  17.         <!-- 可选填 -->

  18.    </properties>

  19.    <!-- 公共依赖 -->

  20.    <dependencies>

  21.        <!-- 框架 -->

  22.        <dependency>

  23.            <groupId>org.springframework.boot</groupId>

  24.            <artifactId>spring-boot-starter-web</artifactId>

  25.        </dependency>

  26.        <!-- 工具类 -->

  27.        <dependency>

  28.            <groupId>com.alibaba</groupId>

  29.            <artifactId>fastjson</artifactId>

  30.        </dependency>

  31.        <!-- 其他依赖 -->

  32.    </dependencies>

  33. </project>

常用操作

  1. # 安装到本地、推送到 Maven 私服

  2. mvn clean install deploy

构建出来的 jar 包中仅包含编译后的 class 文件及依赖关系,非常轻量!

绿色:Spring Boot Application

最终的目标是构建出可运行的 jar 包,就需要打包所有依赖的代码文件到一起,使用 Spring Boot Maven 插件就能轻易完成。

  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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3.    <modelVersion>4.0.0</modelVersion>

  4.    <groupId>com.anoyi</groupId>

  5.    <artifactId>server-general</artifactId>

  6.    <version>1.0-SNAPSHOT</version>

  7.    <!-- 自定义 Parent 工程 -->

  8.    <parent>

  9.        <groupId>com.anoyi</groupId>

  10.        <artifactId>parent</artifactId>

  11.        <version>1.0.0.RELEASE</version>

  12.    </parent>

  13.    <dependencies>

  14.        <!-- Spring Boot 依赖、自定义依赖 或 其他依赖 -->

  15.        <dependency>

  16.            <groupId>com.anoyi</groupId>

  17.            <artifactId>common</artifactId>

  18.        </dependency>

  19.    </dependencies>

  20.    <!-- 构建可执行的 jar 包 -->

  21.    <build>

  22.        <plugins>

  23.            <plugin>

  24.                <groupId>org.springframework.boot</groupId>

  25.                <artifactId>spring-boot-maven-plugin</artifactId>

  26.            </plugin>

  27.        </plugins>

  28.    </build>

  29. </project>

常用操作

  1. # 构建可执行 jar 包到 target 目录

  2. mvn clean package

业务代码复用

解耦业务,合理拆分微服务模块,使用 RPC 框架,能有效的复用代码。

轻量级微服务架构,容器化环境,PRC 框架可以使用 spring-boot-starter-grpc

微服务下 Spring Boot Maven 工程依赖关系管理的更多相关文章

  1. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  2. 构建微服务:Spring boot

    构建微服务:Spring boot 在上篇文章构建微服务:Spring boot 提高篇中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jp ...

  3. SpringCloud(8)微服务监控Spring Boot Admin

    1.简介 Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件.Spring Boot Admin 分为 Server 端和 Client 端,Spring ...

  4. spring boot 2.0.3+spring cloud (Finchley)8、微服务监控Spring Boot Admin

    参考:Spring Boot Admin 2.0 上手 Spring Boot Admin 用于管理和监控一个或多个Spring Boot程序,在 Spring Boot Actuator 的基础上提 ...

  5. 小马哥-Java 微服务实践 - Spring Boot 系列-01Java 微服务实践 - Spring Boot 系列(一)初体验

    课程github地址 https://github.com/mercyblitz/segmentfault-lessons 传统的web应用架构.微服务是一种架构.不限定什么语言 单体应用和微服务的对 ...

  6. Java微服务之Spring Boot on Docker

    本文学习前提:Java, Spring Boot, Docker, Spring Cloud 一.准备工作 1.1 安装Docker环境 这一部分请参考我的另一篇文章<ASP.NET Core ...

  7. 构建微服务:Spring boot 入门篇

    什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而 ...

  8. HTTP RESTful服务开发 spring boot+Maven +Swagger

    这周配合第三方平台整合系统,需要提供HTTP REST服务和使用ActiveMQ推送消息,研究了下,做个笔记. 1.使用eclipse创建Spring Boot项目  创建Spring Boot项目( ...

  9. Java 微服务实践 - Spring Boot 系列

    https://segmentfault.com/l/1500000009515571

随机推荐

  1. Azure系列2.1.6 —— BlobProperties

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  2. K8S集群 NOT READY的解决办法 1.13 错误信息:cni config uninitialized

    今天给同事 一个k8s 集群 出现not ready了 花了 40min 才搞定 这里记录一下 避免下载 再遇到了 不清楚. 错误现象:untime network not ready: Networ ...

  3. pHP生成唯一单号

    这几天一直在写个人使用的用户中心,虽然期间遇到不少的问题,但还是一点点的都解决了,也从制作期间学到不少的知识,今天就说一说利用PHP生成订单单的方法. 订单号,大家都不陌生,无论从在网上购物,还是在线 ...

  4. git fetch 和git pull 的差别

    1.git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令: git fetch orgin master //将远程仓库的master分支下载到本地当前branch中 git ...

  5. SSH的使用

    1.如何设置SSH的超时时间 使用SSH客户端软件登录linux服务器后,执行 echo $TMOUT可以查看SSH链接超时时间: 使用vim /etc/profile可以编辑配置页面 修改TMOUT ...

  6. 使用fastjson将对象和字符串进行转换

    依赖包: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</ar ...

  7. dom定位的三种元素

    1.通过id #XXX 2.通过标签  xxx 3.通过类  .xxx

  8. 洛谷 P1102 A−B数对

    题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的 A+BA+BA+B ProblemProblemProblem ,改用 A−BA-BA−B 了哈哈! 好吧,题目是这 ...

  9. 【数学建模】day08-数理统计III

    2. 回归分析 回归分析与曲线拟合区分. 曲线拟合是,根据得到的若干有关变量的一组数据,寻找因变量与(一个或几个)自变量之间的一个函数,使这个函数对那组数据拟合得好.通常,函数的形式可以由经验.先验知 ...

  10. HTML5获取地理位置信息

    <!DOCTYPE html> <html> <head> <title>Location</title> <meta charset ...