开发者社区> 问答> 正文

单台tomcat多个使用spring boot druid的应用时候,启动报javax.manage

一台tomcat下部署2个应用,然后2个应用都是基于spring boot的web项目(都是用了druid,配置信息都一样的,就是项目名称不一样),

启动的时候一个应用启动成功,另外一个应用启动失败;错误日志如下:

[ ERROR] [2017-08-31 11:38:14] org.springframework.boot.SpringApplication [771] - Application startup failed org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.alibaba.druid.filter.stat.StatFilter@2951d67a] with key 'statFilter'; nested exception is javax.management.InstanceAlreadyExistsException: com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:628) at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:550) at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:432) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151) at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131) at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1307) at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:669) at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:217) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:632) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: javax.management.InstanceAlreadyExistsException: com.alibaba.druid.filter.stat:name=statFilter,type=StatFilter at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324) at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522) at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195) at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:674) at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:618) ... 48 common frames omitted

application.yml 中关于druid的配置如下: spring: datasource: name: dbname url: jdbc:mysql://ip:port/dbname username: root password: root

使用druid数据源

type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver druid: initial-size: 1 min-idle: 1 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 1800000 max-evictable-idle-time-millis: 25200000 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true max-open-prepared-statements: 20 use-global-data-source-stat: true web-stat-filter: enabled: false web-stat: enabled: false stat-view-servlet: enabled: true url-mappings: /druid/* login-username: username login-password: password

原提问者GitHub用户otherMou

展开
收起
山海行 2023-07-05 20:47:00 89 0
3 条回答
写回答
取消 提交回答
  • 在单台Tomcat上部署多个使用Spring Boot和Druid连接池的应用时,可能会遇到javax.management异常导致启动失败的问题。这个异常通常是由于多个应用同时尝试注册相同的JMX(Java Management Extensions)MBean而引起的冲突。

    为了解决这个问题,你可以在每个应用的配置文件中指定不同的JMX域(domain)。在application.yml文件中的spring.jmx.default-domain属性中,为每个应用设置一个唯一的域名,例如:

    spring:
      jmx:
        default-domain: app1
    
    spring:
      jmx:
        default-domain: app2
    

    通过为每个应用设置不同的JMX域,你可以确保每个应用的MBean名称是唯一的,从而避免冲突并成功启动所有应用。

    记得将app1app2替换为你实际的应用名称。希望这能帮助你解决问题!如果还有其他疑问,请随时提问。

    2023-07-30 14:12:09
    赞同 展开评论 打赏
  • 北京阿里云ACE会长

    如果您在单台Tomcat中部署了多个使用Spring Boot和Druid连接池的应用程序,并且在启动时遇到了javax.management.MalformedObjectNameException异常,可能是由于多个应用程序同时使用了相同的JMX端口或MBean名称导致的。

    JMX(Java Management Extensions)是Java平台提供的一种管理和监控API,它允许应用程序在运行时动态地获取和修改JVM和应用程序的运行状态。在使用Spring Boot和Druid连接池时,JMX通常用于监控和管理连接池的状态和性能。

    如果多个应用程序同时使用了相同的JMX端口或MBean名称,可能会导致冲突和异常。为了避免这种情况,您可以在每个应用程序的配置文件中,配置不同的JMX端口和MBean名称,以确保它们在运行时不会互相干扰。

    2023-07-30 13:36:32
    赞同 展开评论 打赏
  • 在application.yml里面配置上 spring: jmx: default-domain: applicationname

    default-domain的值不一样就可以了

    原回答者GitHub用户otherMou

    2023-07-06 11:46:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载

相关实验场景

更多