Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 父pom和微服务 我们有几个项目是微服务,每个项目都是独立的(在单独的spring引导服务器上运行,公开rest服务,使用单独的DB模式…) 我们使用maven来管理依赖关系_Java_Maven_Microservices - Fatal编程技术网

Java 父pom和微服务 我们有几个项目是微服务,每个项目都是独立的(在单独的spring引导服务器上运行,公开rest服务,使用单独的DB模式…) 我们使用maven来管理依赖关系

Java 父pom和微服务 我们有几个项目是微服务,每个项目都是独立的(在单独的spring引导服务器上运行,公开rest服务,使用单独的DB模式…) 我们使用maven来管理依赖关系,java,maven,microservices,Java,Maven,Microservices,让父pom将每个微服务声明为模块是一个好主意吗?因此,有助于管理公共依赖项(如每个项目中使用的lib servlet api witch,将其全部删除并仅在父pom中声明)多模块父pom的“问题”在于,如果没有复杂的概要文件,它会将模块锁定在同一发布周期中(假设您正在使用,您应该使用) 我使用Maven的方式是有一个父pom声明: 通用依赖项(日志API、JUnit等) 通用插件 节中的所有依赖项 该部分中的所有插件 每个模块都将父pom视为其父模块,但父模块对这些模块一无所知 这样做的好处

让父pom将每个微服务声明为模块是一个好主意吗?因此,有助于管理公共依赖项(如每个项目中使用的lib servlet api witch,将其全部删除并仅在父pom中声明)

多模块父pom的“问题”在于,如果没有复杂的概要文件,它会将模块锁定在同一发布周期中(假设您正在使用,您应该使用)

我使用Maven的方式是有一个父pom声明:

  • 通用依赖项(日志API、JUnit等)
  • 通用插件
  • 节中的所有依赖项
  • 该部分中的所有插件
每个模块都将父pom视为其父模块,但父模块对这些模块一无所知

这样做的好处来自上面最后两个项目,即“管理”部分。“管理”部分中包含的任何内容都需要在希望使用特定依赖项或插件的模块中重新声明

例如,父级可能如下所示:

<project>

  <groupId>com.example</groupId>
  <artifactId>parent</artifactId>
  <version>1.0.00-SNAPSHOT</version>

  ...

  <dependencies>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>            

  </dependencies>

  <dependencyManagement>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>        

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>2.1</version>
    </dependency>

  </dependencyManagement>

  <plugins>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>

  <plugins>

  <pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>

  </pluginManagement>

</project>
<project>

  <parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.00-SNAPSHOT</version>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>module</artifactId>
  <version>1.0.00-SNAPSHOT</version>

  <dependencies>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>          
    </dependency>        

  </dependencies>

  <plugins>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
    </plugin>

  </plugins>

</project>

com.example
父母亲
1.0.00-快照
...
org.slf4j
slf4j api
1.7.7
朱尼特
朱尼特
4.11
测试
公地郎
公地郎
2.6
公地收藏
公地收藏
2.1
org.apache.maven.plugins
maven编译器插件
3.1
1.8
1.8
org.apache.maven.plugins
maven汇编插件
2.4
假的
src/main/assembly/assembly.xml
组装
包裹
单一的
模块可能如下所示:

<project>

  <groupId>com.example</groupId>
  <artifactId>parent</artifactId>
  <version>1.0.00-SNAPSHOT</version>

  ...

  <dependencies>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>            

  </dependencies>

  <dependencyManagement>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>        

    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>2.1</version>
    </dependency>

  </dependencyManagement>

  <plugins>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>

  <plugins>

  <pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>

  </pluginManagement>

</project>
<project>

  <parent>
    <groupId>com.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.00-SNAPSHOT</version>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>module</artifactId>
  <version>1.0.00-SNAPSHOT</version>

  <dependencies>

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>          
    </dependency>        

  </dependencies>

  <plugins>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
    </plugin>

  </plugins>

</project>

com.example
父母亲
1.0.00-快照
com.example
模块
1.0.00-快照
公地郎
公地郎
org.apache.maven.plugins
maven汇编插件
该单元将:

  • 依赖于
    org.slf4j:slf4j api:1.7.7:compile
    junit:junit:4.11:test
    commons-lang:commons-lang:2.6:compile
  • 有插件
    org.apache.maven.plugins:maven汇编插件:2.4

我会避免父级pom中的依赖关系。如果您的(独立)微服务之一需要其他东西,这会很尴尬。让父级知道每个微服务是很奇怪的

不过,如果您愿意,您可以坚持使用dependencyManagement来建议默认版本/范围。父pom对于定义插件、存储库等非常方便

相反,我会将一组常见的依赖项组合到一个特定的工件中,该工件可能只是一个具有依赖项的pom。然后您可以依赖,比如“com.example/common db dependencies/1.2”,以包括一组标准的数据库依赖项,如hibernate、apache derby和JPA规范(或您正在使用的任何规范)。不使用JPA/SQL的服务可以避免这种依赖关系


但不要纠缠自己。如果你试图涵盖每种情况,很容易过度使用依赖关系结构。因此,只尝试标准化大多数服务真正使用的内容。

这里有一个依赖关系和依赖关系管理问题。假设你的一个微服务想要升级到较新版本的common由于某些原因…你不能这样做,因为你有家长。我确实理解减少重复的诱惑,比如插件配置。在微服务中,我们需要更多地考虑每个服务的独立性


一些配置,比如说你的存储库或发布配置等可以是通用的。

我肯定会使用父项目

我已经在这两种结构上工作了多年……微服务和非微服务,模块化和非模块化,Ant,Maven和Gradle

我们需要理解,使用父pom并不意味着谈论不耦合和独立的微服务:

  • 它们仍然可以独立且不使用父pom耦合
  • 即使您使用的是父pom,它们仍然可以在发布版本中单独构建和更新
我听说过“一个微服务可能需要为一个依赖项使用不同的版本”,你可以,只需要在特定的微服务pom中重写依赖项

我们需要关注“什么是好处,什么是缺点”

  • 控制和标准化:我可以管理常见的依赖项(使用依赖项管理)在一个单一的点上,它使得在所有模块之间展开依赖项更改变得更容易,是的,我们可能需要不同的第三方版本,但同时我们需要避免失去对所有依赖项的控制,因此可以允许例外,但它们需要与“标准化”相平衡
  • 强>组管理:我仍然可以只发布一个模块,但我也可以以更简单的方式管理多个模块的发布,而不必逐个发布模块,而只需开发正在开发的模块,在这种情况下,我仍然有一个单独的入口点,所有的公共依赖项都可以是与PAR相提并论的。耳鼻喉科
还有更多:

  • 通用第三方和平台依赖关系管理
  • 通用第三方和平台标准化
  • 完全控制依赖关系生态系统,整个a