Maven Java project to build the best solution

Foreword

A project from scratch. The first step is to divide the system module.

Maven is a Java world's most popular project management tools. It provides inheritance and aggregation, and therefore can be divided into a plurality of program modules. It is consistent with the idea of ​​micro-services is a good tool to achieve micro-engineering services. A project from scratch. The first step is to divide the system module.

Maven is a Java world's most popular project management tools. It provides inheritance and aggregation, and therefore can be divided into a plurality of program modules. It is consistent with the idea of ​​micro-services is a good tool to achieve micro-engineering services.

Micro distinction of service aggregation module and Maven

Microservice can be deployed independently of a project, Maven's summarization can be multiple modules (projects) aggregated into a large project management, and this big project is still essentially a separate project. However, after polymerization maven, with a single click, you can perform the same operation in all projects. For example, when performing MVN clean command in the parent project, Maven sub will find it below, in order to perform clean-by operation.

Electricity supplier Micro service system

Currently, most systems have PC management interface and a mobile interface (portal module), that is, back office systems and front-shopping system. There are a variety of goods module, order module and user module.

  • Back-end management module (openapi)
  • Portal module (openapi)
  • Orders module
  • Commodity Module

How specific design project structure, need to look for different needs.

Project structure

Single application

In a single application first thing to consider is code reuse, which is more common in a system with the foreground and background. For example, you need to use a commodity front and rear interfaces.
The last reference GitHub open source project, single project structure is more reasonable.

 

Some components microservice below, but you can not say that it is a real microservice system, because orders and other business modules are not split.

 

 Micro Services Architecture

I used to work in a social e-commerce start-up company, the company's project design concept is very advanced, very meticulous micro-services. However, developers write code is very complicated, because each service has a new micro-project maintenance. This can cause a problem. If the developer's function involves multiple modules, he needs to open multiple windows idea to write code. Meanwhile, when using OpenAPI feign internal micro service interface call (e.g., order modules) need to be copied to another interface @ requestmapping OpenAPI.

As shown below, each of the micro services can exist as a module. Need not contain sub OpenAPI external, internal and requires microservice API is divided into two sub-modules: API and provider. Jar package provides API interface to the user.

 

Through practice, API interface and a sub-module may define @requestmapping, the controller module may be implemented provider interface provides the service. After the user import API sub-module, feign API interfaces and interface inheritance can be called.

Project to build

We have a simple self zmall idea to build the project as an example step of the project. github address:

Structure:

  • zmall (polymerization parent project, pom engineering, management relies version)
    • mall-common (common to each module tools)
    • mall-admin (openapi background management terminal Interface)
    • mall-portal (openapi movement end page interface)
    • mall-goods (goods module, micro-services, network calls are not outside)
      • mall-goods-api (micro-module interface definition Goods and services, can be packaged into a jar provided to the consumer side)
      • mall-goods-provider (trade service implementation module)
    • mall-order (Order module module, micro-services, network calls are not outside)
      • mall-order-api (micro-module interface definition line service, can be packaged into a jar provided to the consumer side)
      • mall-order-provider (Order module business to achieve)

Establish pom parent project

Build a Maven project, Maven coordinates generally defined as:

  • groupId:到项目级别,一般能明确公司和项目即可。如io.github.zebinh.zmall。以spring为例则为org.springframework
  • artifactId:到项目下的模块,一般使用短横线风格模块功能,如基础架构通用部分可以 用base-framework,业务部分可以加项目名,如mall-admin。以spring为例则为spring-core,spring-context等

建立完成后,POM的主要配置如下。作为统一项目管理。

<?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> <!-- POM父工程主要统一管理子模块的依赖版本问题,体现Maven的继承和聚合功能 --> <!-- 项目坐标 --> <groupId>io.github.zebinh.mall</groupId> <artifactId>zmall</artifactId> <version>1.0-SNAPSHOT</version> <!-- spring-boot-starter-parent统一管理版本 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <!-- 统一管理属性和版本,这里的配置在启动时起作用,类似mvn -D参数传递--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <skipTests>true</skipTests> <swagger2.version>2.7.0</swagger2.version> </properties> <!-- 依赖管理 --> <dependencyManagement> <dependencies> <!-- SwaggerUI API文档生成工具 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger2.version}</version> </dependency> </dependencies> </dependencyManagement> </project> 复制代码

建立子模块

以zmall-admin为例,建立一个spring boot工程,作为zmall的子模块。idea创建工程时记得修改以下几个点。

  • 新建工程时注意模块名转为包名时,加一个点号分隔。

 

UTOOLS1581847583152.png

 

  • 创建完后,需要修改pom文件的parent坐标等。

建立带有子模块的模块

类似mall-goods,其也是一个pom工程,其下有多个子模块。创建时需要注意以下问题。

  • 模块名加上短横线

 

UTOOLS1581848171291.png

 

  • 使用spring initializer建子模块mall-goods-provider时,需要加长到子模块目录
  • 以上都需要修改pom文件

当然,pom文件中也可以使用repositories配置仓库

maven仓库查找顺序:maven允许在pom文件和settings.xml文件中配置仓库的位置,其会根据maven仓库的id的字典序号来搜索仓库。因此如你只是临时使用阿里云仓库代替中央仓库,在pom.xml中配置id为a开头的阿里云仓库,这样就能排在前边解析了。

Maven仓库和私服设置

Maven的用户配置文件.m2目录下的settings.xml一般配置如下几项:

  • localRepository:统一Maven本地仓库
  • servers:配置私服用户名和密码
  • repositories, pluginRepositories:私服或远程仓库配置,一般可以包含在profiles中做多配置,使用activeProfiles激活指定配置
  • profiles:配置多个,激活指定的一个
  • distributionManagement:使用mvn deploy时会部署到指定的私服,但是一般开发中不会指定。而是使用git下载代码mvn install安装到本地仓库。
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <!-- maven本地仓库位置 -->
    <localRepository>/develop/mavenRepository</localRepository>
    
    <!-- 私服用户密码,稳定版和快照版分开 -->
    <servers>
        <server>
            <id>nexus-releases</id>
            <username>zebinh</username>
            <password>abcdefg</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>zebinh</username>
            <password>abcdefg</password>
        </server>
    </servers>
    
    <!-- 使用!非,除了私服外,都使用阿里云仓库加速 -->
    <mirrors>
        <mirror>
            <id>mirror</id>
             <mirrorOf>!nexus-releases,!nexus-snapshots</mirrorOf> 
            <name>mirror</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
    </mirrors>
    
    <!-- 多配置 -->
    <profiles>
        <profile
        
            <id>nexus</id>
            
            <repositories>
                <repository>
                    <id>nexus-releases</id>
                    <url>https://repo.aliyun.com/repository/1234-release/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>nexus-snapshots</id>
                    <url>https://repo.aliyun.com/repository/1234-snapshot/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus-releases</id>
                    <url>https://repo.aliyun.com/repository/1234-release/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
                <pluginRepository>
                    <id>nexus-snapshots</id>
                    <url>https://repo.aliyun.com/repository/1234-snapshot/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
            
        </profile>
    </profiles>

    <!-- 激活指定的配置 -->
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

</settings>
复制代码

搭建项目骨架

理论指导依据:

 

UTOOLS1582426999897.png

 

各层的命名:

 

UTOOLS1582426816862.png

 

首先搭建mall-goods-api

Mal admin和Mal portal是openapi,因此它们下面的包需要由模块命名,如goods.controller和order.controller。然而,商场商品API是一种商品的微服务,不需要划分模块。
API是接口层,用于管理和门户调用。输出VO对象。因此需要定义URL路径和VO对象,mal商品提供者模块实现API接口。具体如下:

UTOOLS1582428554306.png

 

接着搭建mall-admin openapi

上面搭好了goods微服务,mall-admin则可以引入mall-goods-api来调用goods模块了。如下图:

 

UTOOLS1582431393011.png

 

至此,maven工程搭建结束。代码github地址:github.com/zebinh/zmal…

 

Guess you like

Origin www.cnblogs.com/aishangJava/p/12350065.html