此内容没有您所选择的语言版本。
Chapter 4. Debugging Eclipse Vert.x based application
This sections contains information about debugging your Eclipse Vert.x–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 to the following deployments of the application:
-
When launching the application manually using the
mvn vertx:debuggoal. This starts the application with debugging enabled.
Prerequisites
- A Maven-based application
Procedure
- In a console, navigate to the directory with your application.
Launch your application and specify the debug port using the
-Ddebug.portargument:mvn vertx:debug -Ddebug.port=$PORT_NUMBER
$ mvn vertx:debug -Ddebug.port=$PORT_NUMBERCopy to Clipboard Copied! Toggle word wrap Toggle overflow Here,
$PORT_NUMBERis an unused port number of your choice. Remember this number for the remote debugger configuration.Use the
-Ddebug.suspend=trueargument to make the application wait until a debugger is attached to start.
To debug your Eclipse Vert.x-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.3. 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 复制链接链接已复制到粘贴板!
Eclipse Vert.x provides a built-in logging API. The default logging implementation for Eclipse Vert.x uses the java.util.logging library that is provided with the Java JDK. Alternatively, Eclipse Vert.x allows you to use a different logging framework, for example, Log4J (Eclipse Vert.x supports Log4J v1 and v2) or SLF4J.
To configure debug logging for your Eclipse Vert.x application using java.util.logging:
-
Set the
java.util.logging.config.filesystem property in theapplication.propertiesfile. The value of this variable must correspond to the name of yourjava.util.loggingconfiguration file. This ensures thatLogManagerinitializesjava.util.loggingat application startup. -
Alternatively, add a
java.util.loggingconfiguration file with thevertx-default-jul-logging.propertiesname to the classpath of your Maven project. Eclipse Vert.x will use that file to configurejava.util.loggingon application startup.
Eclipse Vert.x allows you to specify a custom logging backend using the LogDelegateFactory that provides pre-built implementations for the Log4J, Log4J2 and SLF4J libraries. Unlike java.util.logging, which is included with Java by default, the other backends require that you specify their respective libraries as dependencies for your application.
To add logging to your application, create a
io.vertx.core.logging.Logger:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantLogging backends use different formats to represent replaceable tokens in parameterized messages. If you rely on parameterized logging methods, you will not be able to switch logging backends without changing your code.
If you do not want Eclipse Vert.x to use java.util.logging, configure io.vertx.core.logging.Logger to use a different logging framework, for example, Log4J or SLF4J:
Set the value of the
vertx.logger-delegate-factory-class-namesystem property to the name of the class that implements theLogDelegateFactoryinterface. Eclipse Vert.x provides the pre-built implementations for the following libraries with their corresponding pre-defined classnames listed below:Expand Library Class name Log4Jv1io.vertx.core.logging.Log4jLogDelegateFactoryLog4Jv2io.vertx.core.logging.Log4j2LogDelegateFactorySLF4Jio.vertx.core.logging.SLF4JLogDelegateFactoryWhen implementing logging using a custom library, ensure that the relevant
Log4JorSLF4Jjars are included among the dependencies for your application.ImportantThe Log4J v1 delegate provided with Eclipse Vert.x does not support parameterized messages. The delegates for Log4J v2 and SLF4J both use the
{}syntax. Thejava.util.loggingdelegate relies onjava.text.MessageFormatthat uses the{n}syntax.
Netty is a library used by VertX to manage asynchronous network communication in applications.
Netty:
- Allows quick and easy development of network applications, such as protocol servers and clients.
- Simplifies and streamlines network programming, such as TCP and UDP socket server development.
- Provides a unified API for managing blocking and non-blocking connections.
Netty does not rely on an external logging configuration using system properties. Instead, it implements a logging configuration based on logging libraries visible to Netty classes in your project. Netty tries to use the libraries in the following order:
-
SLF4J -
Log4J -
java.util.loggingas a fallback option
You can set io.netty.util.internal.logging.InternalLoggerFactory directly to a particular logger by adding the following code at the beginning of the main method of your application:
// Force logging to Log4j InternalLoggerFactory.setDefaultFactory(Log4JLoggerFactory.INSTANCE);
// Force logging to Log4j
InternalLoggerFactory.setDefaultFactory(Log4JLoggerFactory.INSTANCE);
4.2.5. 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 oc:deploy -Popenshift
$ mvn clean oc:deploy -PopenshiftCopy 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:
For example, the following command is based on an example REST API level 0 application where debug logging is set to log the
messagevariable in the/api/greetingmethod: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.
... Feb 11, 2017 10:23:42 AM io.openshift.MY_APP_NAME INFO: Greeting: Hello, Sarah ...
... Feb 11, 2017 10:23:42 AM io.openshift.MY_APP_NAME INFO: Greeting: Hello, Sarah ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
To disable debug logging, update your logging configuration file, for example
src/main/resources/vertx-default-jul-logging.properties, remove the logging configuration for your class and redeploy your application.