此内容没有您所选择的语言版本。
Chapter 4. Debugging your Spring Boot-based application
This sections contains information about debugging your Spring Boot–based application both in local and remote deployments.
4.1. Remote debugging 复制链接链接已复制到粘贴板!
To remotely debug an application, you must first configure it to start in a debugging mode, and then attach a debugger to it.
One of the ways of debugging a Maven-based project is manually launching the application while specifying a debugging port, and subsequently connecting a remote debugger to that port. This method is applicable at least when launching the application manually using the mvn spring-boot:run goal.
Prerequisites
- A Maven-based application
Procedure
- In a console, navigate to the directory with your application.
Launch your application and specify the necessary JVM arguments and the debug port using the following syntax:
mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$PORT_NUMBER"
$ mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$PORT_NUMBER"Copy to Clipboard Copied! Toggle word wrap Toggle overflow $PORT_NUMBERis an unused port number of your choice. Remember this number for the remote debugger configuration.If you want the JVM to pause and wait for remote debugger connection before it starts the application, change
suspendtoy.
4.1.2. Starting an uberjar in debugging mode 复制链接链接已复制到粘贴板!
If you chose to package your application as a Spring Boot uberjar, debug it by executing it with the following parameters.
Prerequisites
- An uberjar with your application
Procedure
- In a console, navigate to the directory with the uberjar.
Execute the uberjar with the following parameters. Ensure that all the parameters are specified before the name of the uberjar on the line.
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$PORT_NUMBER -jar $UBERJAR_FILENAME
$ java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$PORT_NUMBER -jar $UBERJAR_FILENAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow $PORT_NUMBERis an unused port number of your choice. Remember this number for the remote debugger configuration.If you want the JVM to pause and wait for remote debugger connection before it starts the application, change
suspendtoy.
To debug your Spring Boot-based application on OpenShift remotely, you must set the JAVA_DEBUG environment variable inside the container to true and configure port forwarding so that you can connect to your application from a remote debugger.
Prerequisites
- Your application running on OpenShift.
-
The
ocbinary installed. -
The ability to execute the
oc port-forwardcommand in your target OpenShift environment.
Procedure
Using the
occommand, list the available deployment configurations:oc get dc
$ oc get dcCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the
JAVA_DEBUGenvironment variable in the deployment configuration of your application totrue, which configures the JVM to open the port number5005for debugging. For example:oc set env dc/MY_APP_NAME JAVA_DEBUG=true
$ oc set env dc/MY_APP_NAME JAVA_DEBUG=trueCopy to Clipboard Copied! Toggle word wrap Toggle overflow Redeploy the application if it is not set to redeploy automatically on configuration change. For example:
oc rollout latest dc/MY_APP_NAME
$ oc rollout latest dc/MY_APP_NAMECopy to Clipboard Copied! Toggle word wrap Toggle overflow Configure port forwarding from your local machine to the application pod:
List the currently running pods and find one containing your application:
oc get pod
$ oc get pod NAME READY STATUS RESTARTS AGE MY_APP_NAME-3-1xrsp 0/1 Running 0 6s ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure port forwarding:
oc port-forward MY_APP_NAME-3-1xrsp $LOCAL_PORT_NUMBER:5005
$ oc port-forward MY_APP_NAME-3-1xrsp $LOCAL_PORT_NUMBER:5005Copy to Clipboard Copied! Toggle word wrap Toggle overflow Here,
$LOCAL_PORT_NUMBERis an unused port number of your choice on your local machine. Remember this number for the remote debugger configuration.
When you are done debugging, unset the
JAVA_DEBUGenvironment variable in your application pod. For example:oc set env dc/MY_APP_NAME JAVA_DEBUG-
$ oc set env dc/MY_APP_NAME JAVA_DEBUG-Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
You can also set the JAVA_DEBUG_PORT environment variable if you want to change the debug port from the default, which is 5005.
4.1.4. Attaching a remote debugger to the application 复制链接链接已复制到粘贴板!
When your application is configured for debugging, attach a remote debugger of your choice to it. In this guide, Red Hat CodeReady Studio is covered, but the procedure is similar when using other programs.
Prerequisites
- The application running either locally or on OpenShift, and configured for debugging.
- The port number that your application is listening on for debugging.
- Red Hat CodeReady Studio installed on your machine. You can download it from the Red Hat CodeReady Studio download page.
Procedure
- Start Red Hat CodeReady Studio.
Create a new debug configuration for your application:
- Click Run→Debug Configurations.
- In the list of configurations, double-click Remote Java application. This creates a new remote debugging configuration.
- Enter a suitable name for the configuration in the Name field.
- Enter the path to the directory with your application into the Project field. You can use the Browse… button for convenience.
- Set the Connection Type field to Standard (Socket Attach) if it is not already.
- Set the Port field to the port number that your application is listening on for debugging.
- Click Apply.
Start debugging by clicking the Debug button in the Debug Configurations window.
To quickly launch your debug configuration after the first time, click Run→Debug History and select the configuration from the list.
Additional resources
Debug an OpenShift Java Application with JBoss Developer Studio on Red Hat Knowledgebase.
Red Hat CodeReady Studio was previously called JBoss Developer Studio.
- A Debugging Java Applications On OpenShift and Kubernetes article on OpenShift Blog.
4.2. Debug logging 复制链接链接已复制到粘贴板!
4.2.1. Add Spring Boot debug logging 复制链接链接已复制到粘贴板!
Add debug logging to your application.
Prerequisites
- An application that you want to debug.
Procedure
Declare a
org.apache.commons.logging.Logobject using theorg.apache.commons.logging.LogFactoryfor the class you want to add logging.import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; ... private static Log logger = LogFactory.getLog(TheClass.class);
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; ... private static Log logger = LogFactory.getLog(TheClass.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add debugging statements using
logger.debug("my logging message").Example logging statement
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a
logging.level.fully.qualified.name.of.TheClass=DEBUGinsrc/main/resources/application.properties.For example, if you added a logging statement to
dev.snowdrop.example.service.GreetingEndpointyou would use:logging.level.dev.snowdrop.example.service.GreetingEndpoint=DEBUG
logging.level.dev.snowdrop.example.service.GreetingEndpoint=DEBUGCopy to Clipboard Copied! Toggle word wrap Toggle overflow This enables log messages at the
DEBUGlevel and above to be shown in the logs for your class.
4.2.2. Accessing Spring Boot debug logs on localhost 复制链接链接已复制到粘贴板!
Start your application and interact with it to see the debugging statements.
Prerequisites
- An application with debug logging enabled.
Procedure
Start your application.
mvn spring-boot:run
$ mvn spring-boot:runCopy to Clipboard Copied! Toggle word wrap Toggle overflow Test your application to invoke debug logging.
curl http://localhost:8080/api/greeting?name=Sarah
$ curl http://localhost:8080/api/greeting?name=SarahCopy to Clipboard Copied! Toggle word wrap Toggle overflow View your application logs to see your debug messages.
dev.snowdrop.example.service.GreetingEndpoint : Message: Hello, Sarah!
dev.snowdrop.example.service.GreetingEndpoint : Message: Hello, Sarah!Copy to Clipboard Copied! Toggle word wrap Toggle overflow
To disable debug logging, remove logging.level.fully.qualified.name.of.TheClass=DEBUG from src/main/resources/application.properties and restart your application.
4.2.3. Accessing debug logs on OpenShift 复制链接链接已复制到粘贴板!
Start your application and interact with it to see the debugging statements in OpenShift.
Prerequisites
-
The
ocCLI client installed and authenticated. - A Maven-based application with debug logging enabled.
Procedure
Deploy your application to OpenShift:
mvn clean package -Popenshift -Ddekorate.deploy=true
$ mvn clean package -Popenshift -Ddekorate.deploy=trueCopy to Clipboard Copied! Toggle word wrap Toggle overflow View the logs:
Get the name of the pod with your application:
oc get pods
$ oc get podsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start watching the log output:
oc logs -f pod/MY_APP_NAME-2-aaaaa
$ oc logs -f pod/MY_APP_NAME-2-aaaaaCopy to Clipboard Copied! Toggle word wrap Toggle overflow Keep the terminal window displaying the log output open so that you can watch the log output.
Interact with your application:
Get the route of your application:
oc get routes
$ oc get routesCopy to Clipboard Copied! Toggle word wrap Toggle overflow Make an HTTP request on the
/api/greetingendpoint of your application:curl $APPLICATION_ROUTE/api/greeting?name=Sarah
$ curl $APPLICATION_ROUTE/api/greeting?name=SarahCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Return to the window with your pod logs and inspect debug logging messages in the logs.
dev.snowdrop.example.service.GreetingEndpoint : Message: Hello, Sarah!
dev.snowdrop.example.service.GreetingEndpoint : Message: Hello, Sarah!Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
To disable debug logging, remove
logging.level.fully.qualified.name.of.TheClass=DEBUGfromsrc/main/resources/application.propertiesand redeploy your application.