单体 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. scp复制文件到远程服务器上

    scp -P 22 -r 2028792_www  root@120.79.172.45:/usr/local/src Linux scp命令用于Linux之间复制文件和目录. scp是 secure ...

  2. Azure系列2.1.10 —— CloudBlobClient

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

  3. [转帖]NUMA

    作者:ibless 来源:CSDN 原文:https://blog.csdn.net/ibless/article/details/80114009 其实 很早之前对这一块有了解 比较多的的是 CCN ...

  4. 在windows 7上安装TensorFlow

    TensorFlow是一个开源软件库,用于各种感知和语言理解任务的机器学习.目前被50个团队用于研究和生产许多Google商业产品,如语音识别.Gmail.Google 相册和搜索,其中许多产品曾使用 ...

  5. MySQL系列:数据表基本操作(2)

    1. 指定数据库 mysql> use portal; 2. 数据库表基本操作 2.1 查看数据表 mysql> show tables; +------------------+ | T ...

  6. 老男孩python学习自修第八天【函数式编程】

    1.可变参数,将传参自动汇总成列表 2.可变参数,将参数自动汇总成字典 实战如下: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ def show(*arg ...

  7. mysql分页查询按某类型置顶 按某类型置尾 再按优先级排序

    近段时间接到一个新需求: 第一优先级:未满的标的顺位高于已满标的顺位.第二优先级:新手标的顺位高于其他标的的顺位. 第三优先级:标的剩余可投金额少的顺位高于标的剩余可投金额多的. 我是直接通过sql语 ...

  8. no module named 'win32api'问题

    运行scrapy时,报错no module named 'win32api' 解决办法: https://github.com/mhammond/pywin32/releases 下载对于python ...

  9. BZOJ5418[Noi2018]屠龙勇士——exgcd+扩展CRT+set

    题目链接: [Noi2018]屠龙勇士 题目大意:有$n$条龙和初始$m$个武器,每个武器有一个攻击力$t_{i}$,每条龙有一个初始血量$a_{i}$和一个回复值$p_{i}$(即只要血量为负数就一 ...

  10. jquery 循环绑定click的问题

    之前循环数据,通过live绑定click, 发觉每个click绑定的链接参数都是一样的. 后来改用 直接的 click绑定,就好了. $.each(ship.PPRList, function (i, ...