欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud 搭建微服务 — 统一依赖管理

程序员文章站 2022-05-31 19:58:52
...

概述


Spring Cloud 项目都是基于 Spring Boot 进行开发,目前大部分公司都会使用 Maven 来构建管理项目,所以在实际开发中,一般都会创建一个 Maven 依赖管理项目作为 Parent 项目,这样的话可以很方便对项目中的 Jar 包版本进行统一管理。

 
 

创建统一依赖管理工程


创建目录 hello-spring-cloud-dependencies,并在该目录下创建 pom.xml

完整的 pom.xml 如下:

<?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>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
  </parent>

  <groupId>com.antoniopeng</groupId>
  <artifactId>hello-spring-cloud-dependencies</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>hello-spring-cloud-dependencies</name>
  <url>http://www.antoniopeng.com</url>
  <inceptionYear>2019-Now</inceptionYear>

  <properties>
    <!-- Environment Settings -->
    <java.version>1.8</java.version>
    <mysql.version>5.1.46</mysql.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- Spring Settings -->
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    <spring-boot-admin.version>2.0.1</spring-boot-admin.version>
    <spring-boot-alibaba-druid.version>1.1.10</spring-boot-alibaba-druid.version>
    <spring-boot-mapper.version>2.0.2</spring-boot-mapper.version>
    <spring-boot-pagehelper.version>1.2.5</spring-boot-pagehelper.version>
    <zipkin.version>2.10.1</zipkin.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- Spring Boot Begin -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>${spring-boot-alibaba-druid.version}</version>
      </dependency>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>${spring-boot-mapper.version}</version>
      </dependency>
      <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>${spring-boot-pagehelper.version}</version>
      </dependency>
      <!-- Spring Boot End -->

      <!-- Spring Cloud Begin -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- Spring Cloud End -->

	  <!-- Spring Boot Admin(服务监控) Begin -->
      <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>${spring-boot-admin.version}</version>
      </dependency>
      <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>${spring-boot-admin.version}</version>
      </dependency>
      <!-- Spring Boot Admin End -->

      <!-- ZipKin(服务链路追踪) Begin -->
      <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin</artifactId>
        <version>${zipkin.version}</version>
      </dependency>
      <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-server</artifactId>
        <version>${zipkin.version}</version>
      </dependency>
      <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-ui</artifactId>
        <version>${zipkin.version}</version>
      </dependency>
      <!-- ZipKin End -->
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <!-- Compiler 插件, 设定 JDK 版本 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <showWarnings>true</showWarnings>
        </configuration>
      </plugin>

      <!-- 打包 jar 文件时,配置 manifest 文件,加入 lib 包的 jar 依赖 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
          </archive>
        </configuration>
        <executions>
          <execution>
            <configuration>
              <archive>
                <manifest>
                  <!-- Add directory entries -->
                  <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                  <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                  <addClasspath>true</addClasspath>
                </manifest>
              </archive>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-clean-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
      </plugin>
    </plugins>

    <pluginManagement>
      <plugins>
        <!-- Java Document Generate -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-javadoc-plugin</artifactId>
          <executions>
            <execution>
              <phase>prepare-package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <!-- YUI Compressor (CSS/JS压缩) -->
        <plugin>
          <groupId>net.alchim31.maven</groupId>
          <artifactId>yuicompressor-maven-plugin</artifactId>
          <version>1.5.1</version>
          <executions>
            <execution>
              <phase>prepare-package</phase>
              <goals>
                <goal>compress</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <encoding>UTF-8</encoding>
            <jswarn>false</jswarn>
            <nosuffix>true</nosuffix>
            <linebreakpos>30000</linebreakpos>
            <force>true</force>
            <includes>
              <include>**/*.js</include>
              <include>**/*.css</include>
            </includes>
            <excludes>
              <exclude>**/*.min.js</exclude>
              <exclude>**/*.min.css</exclude>
            </excludes>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <!-- 资源文件配置 -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
  </build>

  <distributionManagement>
    <repository>
      <id>nexus-releases</id>
      <name>Nexus Release Repository</name>
      <url>http://{ip}:{port}/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://{ip}:{port}/repository/maven-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

  <repositories>
    <repository>
      <id>nexus</id>
      <name>Nexus Repository</name>
      <url>http://{ip}:{port}/repository/maven-public/</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>

    <repository>
      <id>aliyun-repos</id>
      <name>Aliyun Repository</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>

    <repository>
      <id>sonatype-repos</id>
      <name>Sonatype Repository</name>
      <url>https://oss.sonatype.org/content/groups/public</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>sonatype-repos-s</id>
      <name>Sonatype Repository</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <releases>
        <enabled>false</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>

    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <name>Nexus Plugin Repository</name>
      <url>http://{ip}:{port}/repository/maven-public/</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
        <enabled>true</enabled>
      </releases>
    </pluginRepository>

    <pluginRepository>
      <id>aliyun-repos</id>
      <name>Aliyun Repository</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
</project>

 
 

pom.xml 节点


parent
继承了 Spring Boot 的 Parent,表示我们是一个 Spring Boot 工程
 
packaging:pom
表示该项目仅当做依赖项目,没有具体的实现代码
 
spring-cloud-dependencies
在 properties 配置中预定义了版本号为 Finchley.RC1 ,表示我们的 Spring Cloud 使用的是 F 版
 
resources
资源文件配置,防止读取不到配置文件
 
build
配置了项目所需的各种插件
 
repositories
配置项目下载依赖时的第三方库
 
pluginRepositories
配置项目下载插件时的第三方库