【问题标题】:Sprint-boot executable same sources multiple jarsSpring-boot可执行相同源多个jar
【发布时间】:2016-10-29 18:26:48
【问题描述】:

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

  • 使用 spring-boot maven 插件重新打包目标构建 jar
  • 将 application.jar 复制到我的远程服务器上的 application-0.jar
  • 使用 APP_NAME=application-0 创建了一个 application-0.conf 文件
  • 将 application-0.jar 作为服务启动
  • 将 application.jar 复制到我的远程服务器上的 application-1.jar
  • 使用 APP_NAME=application-1 创建了一个 application-1.conf 文件
  • 将 application-1.jar 作为服务启动

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

于是我意识到:

  • linux 服务名称是硬编码在 jar 中的,所以无论你复制 jar 多少次并重命名它,服务都将保持不变(在我们的例子中是应用服务)
  • APP_NAME 对服务名称没有影响。 .conf 文件中的 APP_NAME 分别设置为 application-0 和 application-1。我还是遇到了这个问题。

我阅读了 spring-boot 插件文档并尝试在不同的 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>

谢谢

【问题讨论】:

  • 该文档说“Spring Boot 应用程序可以使用 init.d 或 systemd 作为 Unix/Linux 服务轻松启动”。你是怎么做到的?
  • 嗨,Alex,我使用了 init.d(将 application.jar 符号链接到 /etc/init.d/application.jar)

标签: spring-boot executable spring-boot-maven-plugin


【解决方案1】:

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

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

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

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

    [单位] 描述=应用程序-{i} After=syslog.target

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

    [安装] WantedBy=multi-user.target

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

Systemd 解决了我的问题。

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

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

【讨论】:

    猜你喜欢
    • 2014-11-27
    • 2018-05-26
    • 1970-01-01
    • 2014-12-18
    • 2014-11-28
    • 2017-11-21
    • 2017-01-20
    • 1970-01-01
    • 2015-05-04
    相关资源
    最近更新 更多