spring cloud脚手架项目(二)模块化项目搭建以及maven分环境打包部署

前言

个人从17年毕业,算上实习等等经历,也是机缘巧合,对于从零开始搭建项目这件事情在每一家公司都经历过了。到今天为止,也算是有点心得。现在就讲一下我从零开始搭建一个微服务应用的理解。
文章maven多环境参照:https://blog.csdn.net/jisu30miao1225/article/details/80745035
https://blog.csdn.net/u010277958/article/details/93243978

模块化搭建

现在的应用项目,都是追求一个模块化的概念,恰巧idea这个写代码工具的项目管理也是模块化的,和现在的情况如出一辙,很利于项目的管理。我觉得也是lide流行的原因之一吧。
还有一个不得不提的就是maven的,他对于项目的构建非常重要,我在大学写java项目的时候,还需要自己去网上把jar包下载下来,完全不知道maven,当时构建一个项目非常的痛苦,各种jar包依赖很折磨人,通过maven可以很方便的管理这些jar包,环境,以及最终的打包

项目分层

我把我的脚手架项目分为5个模块,如图所示:
在这里插入图片描述
通过继承关系来管理这几个模块
common:通用的工具类,配置类存放位置
dao:数据库连接层,只有数据集pojo和mapper文件
core:逻辑层,用于执行各种需要的逻辑
service:接口层,只有接口和返回值,别的都没有,用于微服务调用的jar包提供,所以在层级中可以看出是单独在那里的底层jar包之一,我在管理这一层的时候也是本着尽可能提供少的其他jar包而进行管理的,目前只有一个feign的jar
start:只有一个spring boot的start文件。用于项目启动

文件配置

在这里插入图片描述
如图所示,我们的项目由一个主pom管理所有的子pom文件,作为一个jar包的管理和使用。

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chen</groupId>
    <artifactId>SpringCloudScaffold</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>chen_common</module>
        <module>chen_dao</module>
        <module>chen_core</module>
        <module>chen_service</module>
        <module>chen_start</module>
    </modules>
    <!-- 常量定义 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.source.version>1.8</java.source.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <project-common.version>1.0.0-SNAPSHOT</project-common.version>
   </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--本地jar-->
            <dependency>
                <groupId>com.chen</groupId>
                <artifactId>chen_dao</artifactId>
                <version>${project-common.version}</version>
            </dependency>
            <dependency>
                <groupId>com.chen</groupId>
                <artifactId>chen_common</artifactId>
                <version>${project-common.version}</version>
            </dependency>
            <dependency>
                <groupId>com.chen</groupId>
                <artifactId>chen_core</artifactId>
                <version>${project-common.version}</version>
            </dependency>
            <dependency>
                <groupId>com.chen</groupId>
                <artifactId>chen_service</artifactId>
                <version>${project-common.version}</version>
            </dependency>
            <dependency>
                <groupId>com.chen</groupId>
                <artifactId>chen_start</artifactId>
                <version>${project-common.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

举例2个一个子pom文件类型
common模块:
是最高层依赖jar包

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>SpringCloudScaffold</artifactId>
        <groupId>com.chen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>chen_common</artifactId>
    <name>chen_common</name>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

start模块:
也是最底层依赖jar,只有一个spring boot启动类和各个环节的yml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>SpringCloudScaffold</artifactId>
        <groupId>com.chen</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>chen_start</artifactId>
    <name>chen_start</name>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.chen</groupId>
            <artifactId>chen_core</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>stg</id>
            <properties>
                <spring.profiles.active>stg</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prd</id>
            <properties>
                <spring.profiles.active>prd</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

</project>

yml

spring:
  profiles:
    active: @spring.profiles.active@

配置说明

1.可以看到@spring.profiles.active@这个在application.yml的配置文件其中的属性和start模块中maven中profiles的配置相互对应,每次maven进行install的时候回选择其中的一个配置填入对应的配置信息,我们一般分为本地,日常,预发,线上这样4个环境,然后idea也会很人性化的给你对应的profiles环境进行切换。这也是我们进行jenkins打包的时候会配置的启动命令,带上对应的profiles参数配置即可
2.build配置就是用于maven clean和install使用的
3.每一个子pom都有一个parent直接继承主pom,这样你在主pom中导入的jar包也可以在子pom中使用,idea也有方便的可以进行跳转,有利于jar包的调整
4.spring boot快速jar包引入可以通过主pom中的parent中连接到spring boot 2.0.3relese版本中对应的jar,这样在导入其他spring组件的时候不需要关心其对应的jar包版本了

项目地址

github:https://github.com/alex9567/SpringCloudScaffold

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值