单体 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. 1244. Minimum Genetic Mutation

    描述 A gene string can be represented by an 8-character long string, with choices from "A", ...

  2. bootstrap模态框关闭后清除模态框的数据

    https://segmentfault.com/q/1010000008789123 bootstrap模态框第二次打开时如何清除之前的数据? 我用了bootstrap模态框的remote功能,在弹 ...

  3. vue.js 添加 fastclick的支持

    fastclick:处理移动端click事件300毫秒延迟 1.兼容性 iOS 3及更高版本的移动Safari iOS 5及更高版本的Chrome Android上的Chrome(ICS) Opera ...

  4. 校园电商项目(1) 基于SSM

    第一步:搭好环境 我这里使用Eclipse做本次的项目,tomcat.maven啥的怎么弄就跳过了ヾ(o・ω・)ノ 第二步:创建工程 我们首先创建一个maven项目,选择最后一个,创建完之后发现报错, ...

  5. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class cn.edu.

    详细信息   https://www.cnblogs.com/xuwenjin/p/8832522.html 解决办法: 在实体类上面加上注解 @JsonIgnoreProperties(value ...

  6. python爬虫之多线程、多进程、GIL锁

    背景: 我们知道多线程要比多进程效率更高,因为线程存在于进程之内,打开一个进程的话,首先需要开辟内存空间,占用内存空间比线程大.这样想也不怪,比如一个进程用10MB,开10个进程就得100MB的内存空 ...

  7. SSM框架整合系列——第一步

    环境: JDK8 idea2018.2 maven3.5 spring和springMVC是天然集成,所以只需要解决mybatis和spring的整合问题,重点整合mybatis和spring的两个东 ...

  8. Ajax之Jquery封装使用举例

    <html> <head> <meta charset="UTF-8"> <title>登陆页面</title> < ...

  9. vue axios 封装(三)

    封装三: import axios from 'axios' import { Message, MessageBox } from 'element-ui' import store from '. ...

  10. Condition线程通信(七)

    前言:对于线程通信,使用synchronized时使用wait.notify和notifyAll来实行线程通信.而使用Lock如何处理线程通信呢?答案就是本片的主角:Condition. 一.Cond ...