spring-boot 可执行同源多个jar

Sprint-boot executable same sources multiple jars

我执行了以下步骤,以便 运行 我的 spring-boot 应用程序的 2 个实例在 同一主机上:

然后我在 application-1 启动时收到以下错误:service application already provided!

所以我意识到:

我阅读了 spring-boot 插件文档并尝试了 attachclassifier 和 [=33 的不成功组合=]embeddedLaunchScriptProperties.initInfoProvides 不同 spring 的选项-同一构建的启动 maven 插件执行。

我还尝试在不同的 Maven 配置文件上封装不同的执行。我还有一个原始 jar 文件和一个 spring-boot jar。

因此,如果有人知道如何实现我的目标(相同的来源,多个配置良好的 linux 服务,因为我 运行 在同一台主机上),我将非常感谢您的帮助。

下面是配置不成功的例子:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>classic</id>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <executable>true</executable>
      </configuration>
    </execution>
    <execution>
      <id>instance-0</id>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <finalName>${project.artifactId}-0</finalName>
        <executable>true</executable>
        <classifier>exec</classifier>
        <attach>false</attach>
      </configuration>
    </execution>
    <execution>
      <id>instance-1</id>
      <goals>
        <goal>repackage</goal>
      </goals>
      <configuration>
        <finalName>${project.artifactId}-1</finalName>
        <executable>true</executable>
        <classifier>exec</classifier>
        <attach>false</attach>
      </configuration>
    </execution>
  </executions>
</plugin>

谢谢

我使用 systemd 解决了这个问题:

  • build single executable jar(没有多个 spring 引导插件执行,没有 maven 配置文件,只是定期重新打包可执行目标)

  • 使其在绝对路径下可用 (/var/xxx/application.jar)

  • 使用以下配置创建任何 /etc/systemd/system/application-{i}.service

    [单位] 描述=应用-{i} 之后=syslog.target

    [服务] ExecStart=/var/xxx/application.jar --server.port=4500{i} -- logging.file=/var/log/xxx/application-{i}.log

    [安装] WantedBy=多-user.target

注意占位符 {i} 以避免服务名称、监听端口和日志文件发生冲突。

Systemd 解决了我的问题。

注意:如果您 运行 同一主机上的相同服务实例,您只关心我的问题。如果您 运行 每个主机一个服务实例(常见的微服务方法),则不需要所有这些配置。

希望这对遇到同样问题的人有所帮助。