亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在同一個 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 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號