序言

最近领导安排有活,将原先公司的核心项目进行拆分,由于该项目是个基于SSM的单体应用,整体代码量比较庞大,现在要求根据新系统需求对该老项目基于微服务架构进行拆分,由于新旧系统大量存在业务耦合,编码过程那叫一个苦逼。不过今天只是聊聊整个微服务的模块搭建,步骤很简单。

项目搭建

一、创建父工程

通常我都是基于maven构建一个简单父POM工程,简单说白了这个父工程没啥模块功能,主要是依赖的版本控制和依赖管理。而后边一些列的功能模块,都基于这个父工程进行创建,而这一个个的功能模块,就是一个个的SpringBoot项目。

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

    <groupId>com.gz.xf</groupId>
    <artifactId>hospital-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>hospital-common</module>
        <module>hospital-user</module>
        <module>hospital-setting</module>
    </modules>
    <packaging>pom</packaging>

    <!--统一管理Jar包版本-->
    <properties>
        <java.version>1.8</java.version>
        <cloud.version>Hoxton.RELEASE</cloud.version>
        <alibaba.version>2.1.0.RELEASE</alibaba.version>
        <mybatis-plus.version>3.4.2</mybatis-plus.version>
        <mysql.version>5.1.47</mysql.version>
        <swagger.version>2.7.0</swagger.version>
        <jwt.version>0.7.0</jwt.version>
        <fastjson.version>1.2.72</fastjson.version>
        <httpclient.version>4.5.12</httpclient.version>
        <easyexcel.version>2.1.1</easyexcel.version>
        <aliyun.version>4.3.3</aliyun.version>
        <oss.version>3.4.0</oss.version>
        <jodatime.version>2.10.1</jodatime.version>
        <mybatis-generator.version>3.4.0</mybatis-generator.version>
        <boot.version>2.2.4.RELEASE</boot.version>
    </properties>

    <!--锁定依赖,子模块继承不需要写group和version-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.30</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.72</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--mybatis-plus 持久层-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>${mybatis-generator.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <!--swagger-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <!--swagger ui-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>

            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>${easyexcel.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.1.13</version>
            </dependency>

            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>3.4.0</version>
            </dependency>

            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>3.10.2</version>
            </dependency>

            <!--日期时间工具-->
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.10.5</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

上图就是父工程中的整个POM依赖,新建的时候不需要SpringBoot初始化器,new一个新Maven工程即可。modules中的就是具体的子功能模块。

二、子模块引入依赖

由于上级存在父模块依赖的版本和依赖管理,在子模块只需引入指定依赖的groupIdartifactId,若当前子模块指定version,会覆盖父工程的版本;若没指定,则沿用父工程的版本。例如:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

该图是用户模块中的相关依赖,我没指定具体的版本

三、改配置

SpringCloud和SpringBoot的联系这里就不做介绍了,由于在SpringBoot应用中,遵循约定>配置>编码,所以在引入父工程相关依赖后,我们在子模块中首先会修改yml文件或properties文件,编写dao->service->controller

总结

微服务项目搭建过程基本套路都差不多,不过由于咱还是职场新人,有些基本功是需要信手拈来的。所以,自行搭建微服务项目显然是必不可少的一项基本功。