如何远程调试在同一容器中运行的具有相同代码库但配置文件不同的两个spring boot应用程序? [英] How to remote debug two spring boot applications running in the same container with the same codebase but with different profiles?

查看:36
本文介绍了如何远程调试在同一容器中运行的具有相同代码库但配置文件不同的两个spring boot应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个运行在相同容器中,具有相同代码库但配置文件不同的spring boot应用程序.

I have two spring boot applications running in the same container with the same codebase but with different profiles.

一个应用程序用于生产,另一个应用程序用于测试.每个应用程序都在不同的虚拟主机上运行.

One application is for production and the other is for test. Each application run under a different virtual host.

容器是tomcat,我已经在 setenv.sh 中添加了它:

The container is tomcat and I've added this in setenv.sh :

CATALINA_OPTS="-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n $CATALINA_OPTS"

使用eclipse,我可以为远程Java应用程序创建调试配置.我已经将主机设置为测试服务器的主机名.

With eclipse, I can create a debug configuration for Remote Java Application. I've set the Host to the test server host name.

但是当我设置一个断点时,生产应用程序 AND 会触发该断点.

But when I set a breakpoint, the production AND the test application trigger the breakpoint.

如何设置调试环境(例如仅测试应用程序触发断点)?

How to set the debugging environment such as only the test application triggers the breakpoints?

推荐答案

您需要做的是创建一种验证当前配置文件的方法.您可以将环境添加到要调试的对象中:

What you need to do is to create a way for verifying the current profile. You can add a environment to the object you want to debug:

public class SpringBean {
  @Autowired
  private Environment env;
  ...
}

您可以在其任何方法中设置一个断点,右键单击它并编辑其断点属性... ,如下所示:

You can set a breakpoint in any of its methods, right click it and edit its Breakpoint properties... like this:

注意 Conditional 在选择"true" 时挂起.仅当当前对象属于已激活 testProfile 的应用程序时,断点才会触发.

Note Conditional and Suspend when 'true' selected. The breakpoint will only trigger, if current object belongs to the app that has testProfile activated.

注意:

您还可以将单例bean投放到您的应用中,这将使您可以从应用中的任何对象静态访问 env .在这种情况下,您可以在断点属性中访问 EnvHolder.env 来检查当前配置文件.

You can also ad a singleton bean to your app that will make env statically accessible from any object in your app. In this case you can access EnvHolder.env in your breakpoint properties to check the current profile.

public class EnvHolder {
  public static Environment env;

  @Autowired
  public void setEnv(final Environment environment) {
      env = environment;
  }
}

但是,如果您在tomcat上没有两个具有单独库的不同应用程序,则类加载问题可能会干扰该方法.

However, if you do not have two distinct apps on tomcat with separate libraries, class loading issues could interfere with that approach.

如果您无法在应用程序中修改任何类,则可以尝试将有关当前配置文件的提示插入某个线程绑定的对象中.例如,在线程名称上添加一个特殊的后缀,但是您必须确保在线程在测试应用程序中结束工作时清除该名称.

If you cannot modify any classes in the app, you could try to insert hints about current profile into some thread bound object. For instance add a special suffix to thread name, but you would have to ensure that the name is cleaned when the thread ends its work in test app.

这篇关于如何远程调试在同一容器中运行的具有相同代码库但配置文件不同的两个spring boot应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆