两个springboot项目如何部署在同一个服务上,两个spring启动应用程序部署在同一台服务器上...

部署两个含有spring-boot-starter-actuator依赖的Spring Boot应用到同一Tomcat服务器时遇到JMX冲突问题。通过为每个应用配置独特的JMX域解决了问题。

现在有两个spring boot的maven项目,需要部署到同一个外部的服务上(tomcat),他们都包含spring-boot-starter-actuator(健康检查)的依赖,但是出现下面的异常

我有多个spring引导应用程序(maven项目),其中包含“spring-boot-starter-actuator”依赖项,用于检查每个应用程序的运行状况。在tomcat服务器中部署这两个应用程序时,我会得到以下异常 

2016-05-02 22:30:44.627 ERROR 12734 --- [ost-startStop-3] o.s.b.a.e.jmx.EndpointMBeanExporter : Could not register MBean for endpoint [dumpEndpoint] org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.springframework.boot.actuate.endpoint.jmx.DataEndpointMBean@67e7c413] with key 'dumpEndpoint'; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Endpoint,name=dumpEndpoint at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:625) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter.registerEndpoint(EndpointMBeanExporter.java:182) [spring-boot-actuator-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter.locateAndRegisterEndpoints(EndpointMBeanExporter.java:162) [spring-boot-actuator-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter.doStart(EndpointMBeanExporter.java:153) [spring-boot-actuator-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter.start(EndpointMBeanExporter.java:276) [spring-boot-actuator-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:51) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:346) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:149) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:112) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:851) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:140) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) [spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:149) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:129) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE] at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175) [spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5155) [catalina.jar:8.0.21] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.21] at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) [catalina.jar:8.0.21] at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) [catalina.jar:8.0.21] at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) [catalina.jar:8.0.21] at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940) [catalina.jar:8.0.21] at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1738) [catalina.jar:8.0.21] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0] at java.lang.Thread.run(Thread.java:744) [na:1.8.0] Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Endpoint,name=dumpEndpoint at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437) ~[na:1.8.0] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898) ~[na:1.8.0] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966) ~[na:1.8.0] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900) ~[na:1.8.0] at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324) ~[na:1.8.0] at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522) ~[na:1.8.0] at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:678) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:621) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE] ... 32 common frames omitted

//***************************************************************************************************************************************************************************

解决办法

 

需要为每个项目配置JMX的域,修改application.properties中的属性spring.jmx.default-domain = applicationname

您可以在application.properties中使用以下属性为每个应用程序配置JMX域(例如,使用应用程序名称作为域):

spring.jmx.default-domain=applicationname

这为Spring添加的所有JMX端点配置了域。

endpoints.jmx.domain=applicationname

这仅为执行器端点配置JMX域。

然后这将防止在不同的应用程序中使用相同名称的JMX bean冲突,因为该域将被添加到JMX名称中。

 

转载于:https://www.cnblogs.com/Nikola/p/8823915.html

### 如何实现两电脑同时运行同一 Spring Boot 项目 为了使两电脑能够同时运行同一个 Spring Boot 项目并协同工作,可以采用分布式部署的方式。以下是具体的实现方法: #### 1. **共享数据库** 通过配置共享数据库,可以让多服务器上的应用实例访问相同的业务数据存储层。通常情况下,推荐使用 MySQL 或 PostgreSQL 等支持高并发的数据库作为后端存储。 - 使用 `spring-boot-starter-data-jpa` 可以轻松完成与 MySQL 的集成[^1]。 - 需要确保每机器上的应用程序都连接到同一个数据库实例,并且数据库表结构一致。 ```yaml spring: datasource: url: jdbc:mysql://shared-database-host:3306/your_database?useSSL=false&serverTimezone=UTC username: your_username password: your_password ``` #### 2. **分布式任务调度** 当多个实例同时运行时,可能涉及到定时任务或异步任务的执行问题。为了避免重复触发任务,可以通过 Quartz 和 Redis 来管理任务分配。 - 利用 Quartz 提供的任务分发功能,在集群环境下协调各节点的工作负载。 - 如果需要进一步增强可靠性,则可以在 Redis 中维护一份全局状态记录,用于控制哪些任务应该由哪个服务负责处理[^2]。 #### 3. **缓存一致性保障** 在分布式环境中保持缓存的一致性非常重要。Spring Boot 支持多种缓存策略,其中基于 Redis 的方案较为常用。 - 整合 Spring Cache 后可自动优化读写操作性能[^3]。 - 对于频繁更新的数据项建议启用永不过期选项;而对于其他类型的对象则应考虑引入随机化的 TTL 值来减少集中失效的风险。 另外需要注意的是,针对某些特殊场景下的竞争条件(如缓存穿透),应当采取额外措施加以防范——比如借助互斥锁机制限制瞬时请求量级的增长速度。 #### 4. **依赖版本兼容性校验** 最后一点也是不容忽视的部分就是确认所使用的框架及其扩展库之间的匹配程度。不同组合可能会带来意想不到的行为差异甚至崩溃错误等问题因此务必仔细核对官方文档给出的相关指导信息以便做出合理的选择[^4]. ```java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring-cloud-alibaba --> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值