单体 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. Git本地仓库push至GitHub远程仓库每次输入账户密码问题解决(亲测可行)

    在使用git push命令将本地仓库内容推送至GitHub远程仓库的每一次git都要让我们输入GitHub的用户名和密码.这着实让我们心烦.我们会有疑问,我明明设置了公钥呀!怎么还需要输入账户和密码? ...

  2. day 7-8 协程

    不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去调 只要你用并发,就会有锁的问题,但是你不能一直去自己加锁吧那么我们 ...

  3. Day 4-3 os & sys模块

    常用方法: import os os.getcwd() # 获取当前程序的工作路径(python解释器的运行路径,不是脚本所在的路径.) os.listdir() # 获取当前程序根目录下的所有文件夹 ...

  4. Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-

    Maven项目报错:Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clea ...

  5. Python 基础知识----流程控制

    判断语句 循环语句 嵌套

  6. umount -fl用法

    umount, 老是提示:device is busy, 服务又不能停止的.可以用"umount -fl"解决! 挂载: mount - mount a filesystem mo ...

  7. 如何使用命令从linux服务器下载文件到windows

    1.直接使用命令从linux下载文件到windows //登录linux服务器导出mysql数据 mysqldump -hrm-2ze8mpi5i65429l1q.mysql.rds.aliyuncs ...

  8. Java多线程之单例模式(线程安全)

    package org.study2.javabase.ThreadsDemo.sync; /** * @Auther:GongXingRui * @Date:2018/9/20 * @Descrip ...

  9. 五、core开发

    一.支付方面的 https://www.cnblogs.com/stulzq/p/7606164.htmlhttps://www.cnblogs.com/guolianyu/

  10. 基于jQuery-ui实现多滑块slider

    效果图: 代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...