Exercise: Debugging a Spring-boot application in Cloud Foundry

Objectives

After completing this lesson, you will be able to:

  • Create an Spring-boot hallo application, deploy it to Cloud Foundry, execute it step-by-step attaching a remote debugger

Debugging a Spring-boot application in Cloud Foundry

Create a simpleHello, World application using Java and the Spring-boot framework. Deploy the application to Cloud Foundry. Debug it remotely from Business Application Studio.

Prerequisites

For the complete execution of current exercise, you must execute the following activities first, using SAP Business Application Studio:

  1. Execute the previous exercise Creating your free trial account in SAP BTP.
  2. Open SAP Business Application Studio, open the training workspace you created in the previous exercise.
  3. Choose FileOpen Folder and open the /home/user/projects folder.

Steps

  1. In the Web browser, access the https://start.spring.io website, then generate and download a Spring Boot application based on the following information:

    SettingValue
    ProjectMaven
    LanguageJava
    Spring Bootleave the default (currently 3.2.2)
    Project Metadataleave the defaults
    PackagingJar
    Java17
    DependenciesSpring Web
    1. In your Web browser, open https://start.spring.io.

    2. Choose the settings as described in the provided table.

    3. Choose Generate. A file named demo.zip is downloaded.

  2. Upload and open the generated project to SAP Business Application Studio

    1. In SAP Business Application Studio, , open the ~/projects folder.

    2. Upload the demo.zip archive to the ~/projects folder (just drag and drop it).

    3. Unzip the archive by running the following commands in the terminal:

      Code snippet
      cd ~/projects
      unzip demo.zip
      Expand
    4. Open the demo project folder by choosing FileOpen Folder

  3. Complete the project with a basic controller class.

    1. Create a new file demo/src/main/java/com/example/demo/DemoController.java.

      Enter the following code content:

      Code snippet
      package com.example.demo;
      
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class DemoController {
      
          @GetMapping("/")
          public String hello() {
              
              String page = "Hello Spring!";    
              return String.format(page);
          }
      }
      Expand

  4. Build the project by running mvn clean install in the terminal.

  5. Run the program in debug mode.

    1. Open the demo/main/java/com/example/demo/DemoApplication.java file.

    2. Choose RunStart Debugging

    3. When the popup message appears A service is listening to port 8080, choose Open in a New Tab.

    4. A new tab is opened, displaying Hello Spring!.

  6. Set a breakpoint at DemoController.java line 12 and make the debugger stop there.

    1. In the file DemoController.java, click aside line 12, a red dot switches on.

    2. Refresh the Hello Spring! tab. Program execution pauses at line 12 in the debugger.

    3. Choose Continue (F5). The execution continues.

    4. Remove the breakpoint.

  7. Stop the program execution.

  8. Login to Cloud Foundry

    1. Make sure you have your Cloud Foundry API endpoint, user (email) and password, created in the previous exercise.

    2. Open a terminal and run the following commands:

      Code snippet
      cf api YOUR_API_ENDPOINT_GOES_HERE
      cf login
      Expand
      When requested, enter your email and your password.

  9. Deploy the application to Cloud Foundry.

    1. In the project root folder, create a new deployment descriptor file with name manifest.yml with the following content:

      Code snippet
      ---
      applications:
      
      - name: my-spring-demo
        memory: 1024M
        timeout: 300
        random-route: true
        buildpacks:
          - sap_java_buildpack
        env:
          JBP_CONFIG_COMPONENTS: '{ "jres": ["com.sap.xs.java.buildpack.jre.SAPMachineJRE"] }'
          JBP_CONFIG_SAP_MACHINE_JRE: '{ "version": "17.+" }'
        path: target/demo-0.0.1-SNAPSHOT.jar
      
      Expand
      Being a yaml file, pay attention that proper vertical alignment is preserved. For example, words name, memory, timeout, random-route, need to start at the same horizontal character.

    2. Deploy the application to cloud foundry by tunning the following command:

      Code snippet
      cf push
      Expand
      At the end of the deployment process, you will get a text, such as the following:
      Code snippet
           state     since                  cpu     memory         disk           details
      #0   running   2020-04-24T17:25:03Z   66.4%   167.5M of 1G   152.4M of 1G
      Expand

  10. Look for the running application in the SAP BTP Cockpit and open it.

    1. In a web browser, open your SAP BTP Cockpit based on the url you received at subscription time (for example: https://cockpit.hanatrial.ondemand.com. Login if required..

    2. Navigate to the training Subaccount.

    3. Navigate to the dev space.

    4. Choose the my-spring-demo application name.

    5. Choose the Application Route. The application opens in the Hello Spring! page.

  11. In SAP Business Application Studio, create a debug configuration to attach the remote Java application in Cloud Foundry.

    1. Choose RunAdd Configuration..Java. The launch.json file is opened.

    2. Replace the full content of the file with the following:

      Code snippet
      {
          "version": "0.2.0",
          "configurations": [
              {
                  "type": "java",
                  "name": "Launch Application",
                  "request": "launch",
                  "mainClass": "com.example.demo.DemoApplication",
                  "projectName": "demo"
              },
              {
                  "type": "java",
                  "name": "Attach Application",
                  "request": "attach", 
                  "hostName": "localhost",
                  "port": 5005,
                  "sourcePaths": [ "${workspaceFolder}" ] 
              } 
          ]
      }
      Expand
  12. Attach the debugger of SAP Business Application Studio to the remote application running in Cloud Foundry.

    1. To enable the remote application for debug, execute the following commands:

      Code snippet
      cf set-env my-spring-demo JBP_CONFIG_JAVA_OPTS "[java_opts: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000']"
      cf enable-ssh my-spring-demo
      cf restage my-spring-demo
      cf ssh -N -T -L 5005:localhost:8000 my-spring-demo
      Expand
      Ignore eventual messages proposing you to expose port 5005.

    2. In SAP Business Application Studio, in the Run and Debug tab, run the Attach Application configuration. The debugger attaches to the remote application.

  13. Set a breakpoint in the DemoController.java file at line 12. Open the application in the browser and verify that the execution stops at the breakpoint. Continue the execution to the end. Remove the breakpoint. Stop the application.

    1. Open the src/main/java/com/example/demo/DemoController.java file. Create a breakpoint by selecting aside line number 12. A red dot appears beside the number.

    2. Re-open or refresh the the Hello Spring! web page in Cloud Foundry. The program execution pauses in the debugger.

    3. Choose Continue (F5). The execution continues.

    4. Remove the breakpoint.

    5. Disconnect the debugger.

    6. In the Terminal, chooseCtrl+C to interrupt the cf ssh command.

Log in to track your progress & complete quizzes