How to remote debug two spring boot applications running in the same container with the same codebase but with different profiles?

那年仲夏 提交于 2019-12-25 07:59:34

问题


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.

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"

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

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?


回答1:


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:

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

Note:

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;
  }
}

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.



来源:https://stackoverflow.com/questions/39834699/how-to-remote-debug-two-spring-boot-applications-running-in-the-same-container-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!