preface

About the extraction of public dependency module of micro-service, unified version management, unified private server configuration, etc., we have tried a variety of schemes, sorted out and optimized for many times, please share.

Desired results:

1. Unified third-party library version management

2, unified private server configuration (upload, download, download plug-in)

3, SpringCloud SpringBoot version dependency management unified configuration

The body of the

Build a separate Maven project for Common and internally generate a core module.

There are two cores, one for the outermost common-parent (pom artifacId’s name) and one for the inner common

The diagram

Common engineering

Common’s features:
  • Common classes, common utility classes
  • Common tripartite dependencies
  • Common packaging is a JAR
Common-parent functions:
  • Unified version management
  • Springboot SpringCloud SpringAlibaba version management
  • Private server configuration
  • Note: Common-parent packaging is poM
The outer layer of pom. The XML

<? The 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.2.8. RELEASE < / version > < / parent > < the groupId > com. Xiaoxi < / groupId > < artifactId > common - parent < / artifactId > < version > 1.0 - the SNAPSHOT < / version > <description>common-parent</description> <packaging>pom</packaging> <modules><module>core</module></modules> <! <properties> <mybatis-plus.version>3.4.0</mybatis-plus.version> < dynamic - the datasource. Version > 3.3.2 rainfall distribution on 10-12 < / dynamic - the datasource. Version > < p6sy. Version > 3.8.7 < / p6sy version > The < mysql - version > 8.0.13 < / mysql - the version > < knife4j. Version > 2.0.7 < / knife4j version > < lombok version > 1.18.12 < / lombok version > < hutool. Version > 5.3.8 < / hutool version > < Java version > 1.8 < / Java version > < seata version > 1.4.0 < / seata version > < nacos. Version > 2.2.1. RELEASE < / nacos version > Piler < maven.com. Source > 1.8 < / maven.com piler. Source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > <spring-cloud.version>Hoxton.SR6</spring-cloud.version> < spring - cloud - alibaba. Version > 2.2.1. RELEASE < / spring - cloud - alibaba. Version > < / properties > <! <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <type>pom</type> <version>${spring-cloud.version}</version> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>${seata.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql-connector.version}</version> </dependency> </dependencies> </dependencyManagement> <! <id> Maven_xiaoxi </ ID > The < url > http://192.168.148.237:8081/repository/maven-public/ < / url > < releases > < enabled > true < / enabled > <updatePolicy>always</updatePolicy> </releases> <snapshots> <updatePolicy>always</updatePolicy> <enabled>true</enabled> </snapshots> </repository> </repositories> <! <pluginRepository> <id> Maven_xiaoxi </ ID > The < url > http://192.168.148.237:8081/repository/maven-public/ < / url > < releases > < updatePolicy > always < / updatePolicy > <enabled>true</enabled> </releases> <snapshots> <updatePolicy>always</updatePolicy> <enabled>true</enabled> </snapshots>  </pluginRepository> </pluginRepositories> <! <distributionManagement> <repository> <id> Maven_xiaoxi </id> The < url > http://192.168.148.237:8081/repository/maven-releases/ < / url > < / repository > < snapshotRepository > < id > maven_xiaoxi < id > < url > http://192.168.148.237:8081/repository/maven-snapshots/ < / url > < / snapshotRepository > </distributionManagement> </project>Copy the code
Pom.xml for the inner core

<? The 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" > < the parent > < artifactId > common - parent < / artifactId > < groupId > com. Xiaoxi < / groupId > < version > 1.0 - the SNAPSHOT < / version > < / parent > The < modelVersion > 4.0.0 < / modelVersion > < version > 1.0 - the SNAPSHOT < / version > < packaging > jar < / packaging > <artifactId>common</artifactId> <! <dependencies> <dependency> <groupId>mysql</groupId> <artifactId> </ connector-java > <version>${mysql-connector.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> < artifactId > spring - the boot - starter - web < / artifactId > < version > 2.2.8. RELEASE < / version > < / dependency > < the dependency > <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>${dynamic-datasource.version}</version> </dependency> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>${p6sy.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> < artifactId > spring - the cloud - starter - openfeign < / artifactId > < version > 2.2.3. RELEASE < / version > < / dependency > < the dependency > <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <exclusions> <exclusion> <artifactId>httpclient</artifactId> <groupId>org.apache.httpcomponents</groupId> </exclusion> </exclusions> <version>${nacos.version}</version> </dependency> <! -- seata --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-seata</artifactId> < version > 2.2.0. RELEASE < / version > < exclusions > < exclusion > < groupId >. IO seata < / groupId > <artifactId>seata-spring-boot-starter</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>${seata.version}</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>com.auth0</groupId> <artifactId> Java -jwt</artifactId> <version>3.15.0</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies> </project>Copy the code

release

For each publication, it is best to clean and then deploy in maven at the outermost layer. (Because they are father and son)

Micro service

xxxx-interface

<? The 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" > < the parent > < the groupId > com. Xiaoxi < / groupId > < artifactId > common - parent < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < / parent > < the groupId > com. Example < / groupId > < version > 1.0 - the SNAPSHOT < / version > < modelVersion > 4.0.0 < / modelVersion > <artifactId>test1-interface</artifactId> <dependencies> <dependency> <groupId>com.xiaoxi</groupId> <artifactId>common</artifactId> <version> 1.0-snapshot </version> </dependency> </dependencies> </ repositories> < repository > < id > maven_xiaoxi < / id > < url > http://192.168.148.237:8081/repository/maven-public/ < / url > < releases > <enabled>true</enabled> <updatePolicy>always</updatePolicy> </releases> <snapshots> <updatePolicy>always</updatePolicy> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>Copy the code

As you can see, the interface inherits common-parent because it is on the private server, so you need to configure a private pull repository anyway. But the advantage is that you inherit common-parent, which means that you don’t have to configure publishing anymore.

xxxx-service

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > com. Xiaoxi < / groupId > < artifactId > common - parent < / artifactId > 1.0 the SNAPSHOT < version > < / version > < / parent > < groupId > com. Example < / groupId > < artifactId > test1 - service < / artifactId > <version>0.0.1 -snapshot </version> <name>test1-service</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> < the groupId > com. Example < / groupId > < artifactId > test1 - interface < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < / dependency > </dependencies> <! -- Use Maven profiles to dynamically modify Spring profiles to switch between multiple sets of profiles --> <profiles> <profile> <! -- Local environment --> <id>local</id> <properties> <profiles. Active >local</profiles. Active > <skipDocker>true</skipDocker> <dockerHost></dockerHost> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <! -- Development environment --> <id>dev</id> <properties> <profiles. Active >dev</profiles. Active > <skipDocker>true</skipDocker> < dockerHost > http://192.168.148.236:2375 < / dockerHost > < / properties > < / profile > < profile > <! Test environment --> <id>test</id> <properties> <profiles. Active >test</profiles. Active > <skipDocker>false</skipDocker> < dockerHost > http://192.168.148.235:2375 < / dockerHost > < / properties > < / profile > < profile > <! -- Production environment --> <id>prod</id> <properties> <profiles. Active >prod</profiles. Active > <skipDocker>false</skipDocker> <dockerHost>http://xxxx:2375</dockerHost> </properties> </profile> </profiles> <! -- Maven plugin published in docker --> <build> <! ${project.artifactid}</finalName> <plugins> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>maven_xiaoxi</id> The < url > http://192.168.148.237:8081/repository/maven-public/ < / url > < releases > < enabled > true < / enabled > <updatePolicy>always</updatePolicy> </releases> <snapshots> <updatePolicy>always</updatePolicy> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>Copy the code

Service, now much cleaner. You can save a lot of configuration by simply inheriting common-parent with private server pull. Dependencies are only required between microservice modules and their own interfaces.

The dependency relationship is:

xxxx-service -> xxxx-interface -> common

The inheritance relationship is

xxx-interface -> common-parent

xxx-service -> common-parent

About the implementation of a service that wants a custom version

A problem was found in the process of sorting and optimization, that is:

The seATA version configured in our public dependencies is 1.4.0, and the final version in the Service is 1.1.0.

Why is that? This is due to maven’s dependency loading order.

There is a good article, share it

Here is also a brief description:

We can roughly divide dependencies into these levels:

  • At the corresponding level

Direct dependence of their own engineering

  • The superior

Parent inheritance of its own project

  • At a lower level

Dependencies of dependent libraries dependencies of third-party libraries that their own projects depend on

  • Version management

Version management of their own projects

The priorities are as follows:

Local > Local Version Management > Superior > Superior Version Management > Lower (no Lower Version Management)

In contrast to our public dependencies library:

The version of common-parent that parent inherits takes precedence over the version of the dependent third party common

That is, common is 1.4.0, parent is 1.1.0 (SpringAlibaba), and eventually 1.1.0 takes precedence.

Below is the figure

So what about this situation?

We know that maven’s dependencies will override the previous configuration if there are multiple identical ones.

Springalibaba has been defined in our common-parent dependencyManagement, so we can re-specify seATA version 1.4.0 below. The final version of seATA for Common-parent is 1.4.0.

The loading sequence is as follows:

  • The original:

Parent (None) > Parent (None) > Parent (None) > Parent (common-parent 1.1.0) > Lower (Common 1.4.0) (No Lower Version)

  • Now:

Local (None) > Local Version Management (None) > Parent (None) > Parent Version Management (Common-parent 1.4.0) > Lower (Common 1.4.0)

About the author

The author is a programmer who loves learning, open source, sharing, spreading positive energy, likes playing basketball, and has a lot of hair. –

Warmly welcome everyone to pay attention to, like, comment exchange!

Jane: www.jianshu.com/u/d234d1569…

github:github.com/fly7632785

CSDN:blog.csdn.net/fly7632785

The Denver nuggets: juejin. Cn/user / 993614…