为了账号安全,请及时绑定邮箱和手机立即绑定

如何在同一个 Tomcat 上部署多个带有外部配置的 Spring Boot 应用程序?

如何在同一个 Tomcat 上部署多个带有外部配置的 Spring Boot 应用程序?

MMMHUHU 2022-07-20 20:16:42
当我想在同一个 tomcat 上部署多个 Spring-Boot 应用程序时,我面临着多个问题。1) 两个应用程序必须独立运行,同时共享同一个 tomcat。在同一个 tomcat 上部署两个 Spring-Boot 应用程序时,出现以下异常:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.2019-03-06 17:31:01 ERROR o.s.boot.SpringApplication - Application run failedorg.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [HikariDataSource (HikariPool-2)] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: com.zaxxer.hikari:name=dataSource,type=HikariDataSource    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:625)    at org.springframework.jmx.export.MBeanExporter.lambda$registerBeans$2(MBeanExporter.java:551)    at java.util.HashMap.forEach(HashMap.java:1289)    at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:551)    at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:434)    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:863)    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)2) 我需要为两个应用程序提供外部配置,并且需要明确每个应用程序都使用正确的配置。
查看完整描述

1 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

1) 当两个 Spring-Boot 应用程序在同一个 tomcat 上运行时,您通常会遇到数据源的实例化可能会失败的问题,因为已经存在同名的实例。这是您在问题中描述的例外。


这可以通过为每个 Spring-Boot 应用程序添加一个唯一的名称来解决,例如在


application.yml


spring:

  application:

    name: application-name-1

  jmx:

    default-domain: application-name-1

2)为每个Spring-Boot应用程序提供外部配置可以通过tomcat上下文配置为每个应用程序单独完成。假设所有应用程序都部署为.wareg app1.war,app2.war我们需要为两个应用程序配置上下文,如下所示:


创建以下文件(如果丢失,则创建目录)


tomcat-base-dir

  /conf

    /catalina

      /localhost #must be the same as specified in the Host tag in the server.xml

        app1.xml #must have the same name as the .war application file

        app2.xml

内容为app1.xml


<?xml version="1.0" encoding="UTF-8"?>

<!-- docBase must contain be the name of the application to be configured -->

<Context docBase="app1.war"> 

    <Parameter name="spring.config.location" value="${catalina.base}/conf/app1.yml" />

</Context>

这将配置应用程序app1.war以使用该文件app1.yml进行配置。对 app2 执行相同的操作。实际的配置文件app1.yml可以位于值中指定的任何路径。


查看完整回答
反对 回复 2022-07-20
  • 1 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号