单体 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. for循环游标

  2. php foreach跳出本次/当前循环与终止循环方法

    continue:跳出本次循环 break:终止循环 exit:用来结束程序执行 return: 用来结束一段代码     $arr= array('le','yang','jun','lecode' ...

  3. 源码追踪,解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题

    在windows系统本地运行spark的wordcount程序,会出现一个异常,但不影响现有程序运行. >>提君博客原创  http://www.cnblogs.com/tijun/  & ...

  4. day 7-7 线程池与进程池

    一. 进程池与线程池 在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多,这 ...

  5. TField OnValidate 事件

    Occurs just before the data is written to the record buffer. Write an OnValidate event handler to va ...

  6. Ajax之Jquery封装使用举例2(Json和JsonArray处理)

    本例主要使用ajax进行异步数据请求,并针对返回数据为json和jsonarray类型的数据处理. 本例中只有前端的代码,后端代码不是本文重点,故省略. 后端接口返回数据为: Json: {" ...

  7. easy install 与pip

    easy_insall的作用和perl中的cpan, ruby中的gem类似,都提供了在线一键安装模块的傻瓜方便方式,而pip是easy_install的改进版, 提供更好的提示信息,删除packag ...

  8. 微服务 Micro services

    微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...

  9. Springboot学习问题记录

    1.spring boot与cloud构建微服务,返回数据从json变成了xml 问题:本身spingboot项目是用@RestController注解,返回结果也是json格式,但是结合spring ...

  10. fpm 打包教程

    常用yum命令: Yum安装时需要安装到指定的文件夹,则需要 --installroot yum install --installroot=/usr/src/ vim 常用rpm命令: 常用yum仓 ...