📜 ⬆️ ⬇️

Cloud Application Development Guide with SAP Cloud Platform and Cloud Foundry

Cloud Foundry (CF) is a global standard for developing cloud-based applications that allows developers to pay more attention to their tasks and facilitates the process of deploying and managing applications.

In this article we will talk about several stages that will help work with Cloud Foundry and our cloud platform:


(there is a lot of text under the cut)

Let's take a conventional example - what to do if you need to quickly create a prototype site for e-commerce on the SAP Cloud Platform for the sales department.
')
Initial site requirements:


If you already have experience in projects that start with PoC (Proof of Concept) and then turn into a productive application, then you understand that you also need to provide functions for monitoring, managing the application. As soon as they see the prototype in the sales department, they will immediately want to release it as soon as possible. However, you need to prepare the application in advance for effective DevOps before launching.

In this article, we will explain in detail how to run a simple SpringBoot application locally, then move it to Cloud Foundry on the SAP Cloud Platform. After studying the application and checking the main logs (logs) and metrics, we will look at how to improve the application, using the capabilities supporting various functions — for example, integration with the Application Logging service, as well as scaling the application.

The article consists of 7 sections:

1. Preparing the development environment
2. Clone an application template from Github
3. Moving the application to the cloud
4. Getting information about an application running in the SAP Cloud Platform environment
5. Integration with the Application Logging service (application logging)
6. Application scaling

Development tools
To complete the steps that are described in this article, you will need to install the following components:


Used services and applications

Application "Product List"

This application is used as an example, containing a list of products. It will develop and improve in the course of the steps that we consider in this article. The application will work in the Cloud Foundry environment of the SAP cloud platform using various services, such as PostgreSQL for storage in the cloud, Application Logging (application logging), etc. This is a SpringBoot application with a simple user interface.

PostgreSQL

PostgreSQL is an open source object-relational database management system. This is an enterprise class ACID compliant database. It is available for use as a third-party service in the Cloud Foundry environment.

Application Logging service

You can create, view and analyze application logs using the Application Logging service. It is based on the open source logging platform Elasticsearch, Logstash, Kibana (Elastic Stack). The application requires some preparation before its logs are transferred to the Application Logging service.

Application Autoscaler service

The Application Autoscaler service is used to automatically scale an application and to scale related applications based on user policies. Dynamic scaling of the application instance ensures that it does not encounter performance problems with increasing load and does not fail. Dynamic scaling also ensures that your application uses optimal resources.

1. Preparing the development environment

In the first part, we will explain how to create a free trial account in the Cloud Foundry environment based on the SAP Cloud Platform and set up a local development environment. We will also look at the SAP Cloud Platform dashboard, which is a web application administration tool for the cloud platform.

1) Getting Started with Cloud Foundry

If you do not have a trial account in the SAP Cloud Platform, you will receive it by following this step-by-step instruction.

In the SAP Cloud Platform control panel (SAP Cloud Platform cockpit), click on the “Home” tab to go to the SAP Cloud Platform home page. In the opened page, click on "Cloud Foundry Trial".



In the window that appears, select a region for your trial account and click "OK". In our case, the region "Europe (Frankfurt)" was selected.



After that, you will receive a global account, subaccount, organization and space in the selected region. You can go to the created space by clicking “Go to Space”.



You now have your free trial account in Cloud Foundry.

Note: If you already have a trial account in the Cloud Foundry environment, simply click on the “Cloud Foundry Trial” on the SAP Cloud Platform home page, after which you will immediately enter your trial account space in the Cloud Foundry environment.

2) Control Panel

Below is a simplified domain model that is used in the Cloud Foundry environment. Get more information in the documentation .



Below is a screenshot from the SAP Cloud Platform “Control Panel”:



3) Account cleanup

In the Cloud Foundry Environment trial account, the quota is limited.
If you have already used a trial account quota, you need to clear the applications and service instances to complete the exercises. You can do this through the control panel:

If you have just created a trial account, then this is not necessary.

4) Setting up a local development environment

The following steps and working with Cloud Foundry will require the development tools described below. If any tools are not installed on your PC, install them.

Cloud Foundry Command Line Interface (CF CLI)
Download and install the Cloud Foundry CLI as described here .

Eclipse
- Eclipse is required for development. This example uses the Neon version. Download the Eclipse IDE for Java EE Developers for the Neon version here .
- You also need to install the plug-in STS (Spring Tool Suite) for Eclipse.
How to install the STS plugin - described here .

Java
- Check the Java version by entering the java -version command at the command line. This example uses version 1.8.0_152.

Maven
- Download and install Apache Maven.
- If Maven is installed correctly, you can check its version by entering the command mvn -version on the command line. This example uses Apache Maven version 3.5.0.

Git
- Download Git here and install it.
- If Git is installed correctly, you can check its version by entering the git --version command at the command prompt. This example uses version 2.15.1.windows.2.

2. Clone an application template from Github
In this section, we will explain how to clone the target version of the Product List application template, import the project template into Eclipse, and then build it.

Navigate to the project source in github .
Click on the “Clone or download” button and select “Download ZIP”.



After the file is downloaded, unzip it in a convenient place for you. In this place a new folder will appear with the name
cloud-cf-product-list-sample 
.

1) Import project template into Eclipse
Go to Eclipse. To import a project template as a Maven project into the Eclipse workspace, on the Eclipse menu, select File -> Import ...



In the “Import” window that opens, select Maven -> Existing Maven Projects and click “Next”.



In the “Import Maven Projects” window, click “Browse” to set the path to the unzipped folder with the “cloud-cf-product-list-sample-master” project that you saved earlier, then click “Finish”.



Now the project is imported into Eclipse, and you see your project in the Project Explorer window, as shown in the screenshot below.



2) Build the project in Eclipse using Maven

Before building a project with Maven, check if your Eclipse JRK is used. If your Eclipse uses JRE instead of JRK, you may get an error about the absence of a compiler during the build process: "[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?".

How to configure Eclipse for the correct assembly of the project - described here .

Now you can build the project in Eclipse. To do this, click on the project with the right mouse button in the “Project Explorer” window and in the opened context menu select Run As -> Maven build ...



In the Edit Configuration window, enter clean install in the Goals field and click Run. After that, the assembly of the project will begin.



The build should end without errors. In case of successful assembly in the console, you will see a message about this:



3. Moving the application to the cloud

1) Account Login through CF CLI
In this exercise, you will learn how to download the application to your trial account in the Cloud Foundry environment of the SAP Cloud Platform. This can be done using the Cloud Foundry command line interface (CF CLI).

Note for Windows users: Open a terminal (command prompt) on your computer. To do this, simultaneously press the "Windows" key and the "R" key, and then type cmd in the input field and click "OK".

First, you need to “tell” your CF CLI what kind of region for the Cloud Foundry environment you are going to use. To do this, you need to set up an API endpoint for a cloud controller (Cloud Controller) located in the region in which your trial account was created in the Cloud Foundry environment. You can do this with the command

cf api CLOUD_FOUNDRY_API_ENDPOINT .

Since this example uses an account that has an API endpoint in the EU10 region, enter cf api api.cf.eu10.hana.ondemand.com . If your account is created in the same region, use the same API endpoint.



Note: You can find all API endpoints for different regions of the Cloud Foundry environment in the official SAP Cloud Platform documentation .

Now log into your account using the following command:

cf login

After this, you will need to enter the email and password that you provided when registering with the SAP Cloud Platform.

Email> enter your e-mail
Password> password for your user

Next, you must select the organization and space in Cloud Foundry that you will use to host the application. If only one organization and space is used in your account, the system will automatically select them for use after logging in to the account, and they will be displayed in the terminal under the word “OK”.



Note: If several organizations and spaces are created in your Cloud Foundry account, you can select the ones you are going to use. To do this, in the terminal you will need to enter the following command:

cf target -o ORGANIZATION -s SPACE


You are now ready to start working with your Cloud Foundry account.

2) Application Manifest

Before downloading the application to the Cloud Foundry environment, you can either set the application parameters in the command line for the push command, which allows the application to run in the cloud, or use the application’s application descriptor file (manifest.yml), setting the parameters for the movement in it, which will allow not to enter them restart each time you use the push command. You can find more information about the application manifest file in the Cloud Foundry documentation .

Go to the Project Explorer window in Eclipse, where the template for your Product List application is located, and open the file manifest.yml .

Note: If this file is not in your project, create it. To do this, right-click on the “Product List” application and select New -> File from the menu that appears, and then create a file called manifest.yml.

After you open this file, paste the following fragment into it:

applications:
# Application
- name: product-list
instances: 1
memory: 896M
host: product-list-YOUR-BIRTHDATE-DAY-MONTH-YEAR
path: target / my-product-list-0.0.1-SNAPSHOT.jar
buildpack: github.com/cloudfoundry/java-buildpack.git#v4.3

Edit the inserted fragment as follows:




Save the manifest.yml file after editing.

3) Move

On the command line, write the path to the location of your SpringBoot application, where you just created the file manifest.yml, and then enter the following command:
cf push

In this example, the application is stored in the cloud-cf-product-list-sample-master folder, so in the terminal this command will look like this:



This command starts the process of deploying an application using the newly created manifest.yml file. The first step is to download the application binaries to the cloud. The next stage is the so-called staging. The next phase is the release phase. She tells Cloud Foundry how to run the application. After successful completion of the command, you can open the application in the browser: to do this, copy and paste the URL displayed on the console after completion.

Note: If the command fails, read the error message. You may have incorrectly specified the parameters in manifest.yml. Or the quota is exceeded in your trial account. If you have already used your trial account, you need to remove the applications and service instances before continuing the exercises. You can do this through the SAP Cloud Platform control panel:


3) Application Call

Now your application is running in your account in the Cloud Foundry environment of the SAP Cloud Platform.




After that you will be taken to your global account. Now click on your sub account (usually it has the name “trial”).



Click on the “Spaces” tab in the menu on the left and select the space in which your application is located (usually “dev”)



After that, you will find yourself in the space of your account, where you can find running applications - in our case this should be the “Product List” application that you transferred to the cloud. Click on the application name to see more detailed information about it:



In the opened window with information about the application, you will find the path to the application (Application Routes). Click on the URL to launch your application in a browser.



4) Services

So far, we have used product information in the memory of the Java virtual machine, which is good for local testing, fast change cycles. However, this method has some drawbacks - if the application is restarted, all changes will be lost, if you have more than one application process running, the data will be different, etc. Let's add the database as a third-party service in the cloud to avoid these problems. This can be done without changing the code. To do this, you need to create an instance of the service, associate it with the application, and restart the application so that it starts using the database. Do the following:

In the terminal, enter the cf marketplace command to display all the services available to your user. You will see a list of services, a brief description and information about service plans.



In this example, we will use PostgreSQL DBMS. To see information about a specific service, use the cf marketplace -s SERVICE command:

cf marketplace -s postgresql

Before an application starts using a service, you must create an instance of the service. Create an instance of PostgreSQL using the cf create-service SERVICE PLAN SERVICE_INSTANCE command. To do this, enter in the terminal:

cf create-service postgresql v9.6-dev postgres


The newly created postgresql service will appear in the list of services used in your space. To verify this, enter the following command:

cf services

The final step is to associate the service instance with your application. To do this, enter the cf bind-service APP_NAME SERVICE_INSTANCE command in CF CLI:

cf bind-service product-list postgres




Now you need to update the application so that the connection to PostgreSQL for it will be saved and this service will be used later. To do this, use the cf restage APP_NAME command:

cf restage product-list

After that, enter the cf services command, and you will see that the PostgreSQL service instance is now attached to the “Product List” application.



Note: An alternative way to attach services to an application is through an application manifest. If you use manifest.yml to bind services, then after editing this file, you will need to reload the application to the cloud using the push command so that the environment can be updated and the application can use this service. So for this example, you need to add the following snippet to the manifest file, and then download the app again to the cloud:

services:
- SERVICE_INSTANCE_NAME

Usually at this stage you need to define an instance of the service and adapt the application code. Information on how to access the service instance is now available in your application environment as the VCAP_SERVICES environment variable. In our case, the SpringBoot environment provides a mechanism for detecting PostgreSQL and starting to work with it, so there is no need to change the application code.

4. Getting information about an application running in the SAP Cloud Platform environment

How to get more information about applications and services running in a trial account in Cloud Foundry, and how to get basic metrics and logs.
This information can be found both through the CF CLI and through the SAP Cloud Platform control panel. Both options will be considered.

1) Connection service for viewing logs

Before viewing logs in Kibana, you need to connect the application-logs service to the application. Kibana is a web-based interface for listing Elasticsearch-indexed logs. We will use the CLI and the commands that we used in the previous exercise to create and bind a service instance to our application. Successively enter the following commands in the terminal:

cf marketplace
cf create-service application-logs lite myapplogs
cf services
cf bind-service product-list myapplogs
cf restage product-list

2) View applications uploaded to the cloud

We get a list of applications running in our account space, with information about them. This can be done through the CF CLI and through the control panel.

CF CLI

To display a list of running applications, enter the following command in the terminal:
cf apps


SAP Cloud Platform Control Panel

Navigate to the space created in the Cloud Foundry environment and select the “Applications” tab in the left menu. You will see a list of applications in this space. In this example, only the “Product List” application is in space.



3) View services

Get the list of service instances created in your space and their bindings.

CF CLI

To display a list of created instances of services and their bindings, enter the following command:

cf services


SAP Cloud Platform Control Panel

In the navigation menu, which is on the left, there is a Services tab. Select it, and then click on the “Service Instances” tab that appears to see all instances of the services created in your space, as well as basic information about them.

4) View application logs


CF CLI

You can view application logs using the cf logs APP_NAM E command . To do this, enter in the terminal:

cf logs product-list

Note: Call the application in the event that the logs are not displayed after entering this command.

To exit the live streaming mode in the console, press Ctrl + C.
To display the latest application logs, use the following command:

cf logs product-list --recent


SAP Cloud Platform Control Panel

To view the application logs in the control panel, go back to the list of applications in your space (“Applications” tab) and select the running application (click on the application name). Then in the menu on the left, select "Logs", after which you will see a table with the latest application logs. If your application has more than one instance of the application, you can choose for which instance you want to view the logs.

5) Display of metrics

We obtain a display of the health status and status of the application, including information on the consumption of the processor, memory and disk.

CF CLI
To display application metrics, enter the following command in the terminal:

cf app product-list



SAP Cloud Platform Control Panel

To view application metrics in the control panel, go to the list of applications in your space (“Applications” tab) and select the running application (click on the application name). Then in the menu on the left, select “Overview”. In the window that opens, you will see similar information about the running application.



6) Events

View the latest application events (such as creating, reinstalling, updating, etc.) and get information about when they occurred and who caused them.

CF CLI

You can see the latest application events through the terminal using the cf events APP_NAME command. Enter the command:

cf events product-list



SAP Cloud Platform Control Panel

To view application events in the control panel, go to the list of applications in your space (“Applications” tab) and select the running application (click on the application name). Then in the menu on the left, select “Events”.



7) Environment Variables

Access to environment variables
You can see the list of environment variables using CF CLI by entering the following command in the terminal:

cf env product-list


You can also get a list of environment variables through the SAP Cloud Platform control panel. To do this, go to the list of applications in your space (“Applications” tab) and select the running application (click on the application name). Then select “Environment Variables” in the menu on the left.



The following examples show how to access environment variables from an application programmatically: Java examples, NodeJS, and Ruby.

System.getenv ("VCAP_SERVICES");

process.env.VCAP_SERVICES

ENV ['VCAP_SERVICES']

Setting Environment Variables

Environment-specific application variables can be set by the application developer using the command in the CF CLI:

cf set-env APP_NAME VARIABLE_NAME VARIABLE_VALUE


Or by describing them in the application's manifest.yml file as follows: \

- ...
env:
VARIABLE_NAME_1: VARIABLE_VALUE_1
VARIABLE_NAME_2: VARIABLE_VALUE_2


You can also set variables through the SAP Cloud Platform control panel. To do this, go to the list of applications in your space (“Applications” tab) and select the running application (click on the application name). Then in the menu on the left, select "User-Provided Variables" and in the window that opens, click "Add Variable".



System environment variables

The full list of environment variables that Cloud Foundry provides to the application container is presented below:

o CF_INSTANCE_ADDR
o CF_INSTANCE_GUID
o CF_INSTANCE_INDEX
o CF_INSTANCE_IP
o CF_INSTANCE_INTERNAL_IP
o CF_INSTANCE_PORT
o CF_INSTANCE_PORTS
o HOME
o MEMORY_LIMIT
o PORT
o PWD
o TMPDIR
o USER
o VCAP_APP_HOST
o VCAP_APP_PORT
o VCAP_APPLICATION
o VCAP_SERVICES

The last variable (VCAP_SERVICES) is important if your application uses binding services, because Cloud Foundry adds binding information to it at the moment you download the application after linking the service to it. The example is based on an application that is associated with the postgresql service.

8) Application health check

Application health check is a monitoring process that constantly checks the status of an application running on Cloud Foundry. In Cloud Foundry, the default timeout is 60 seconds and the maximum custom timeout is 180 seconds. Three types of health checks are available in Cloud Foundry:

• http: - the http type health check is performed using a GET request to the configured http endpoint. When the health check receives an HTTP 200 response, the application is considered "healthy." The configured endpoint must respond within 1 second to be considered healthy.

• port: is the default check. This type of check makes a TCP connection to the port or ports configured for the application (8080 by default). A TCP connection must be established within 1 second in order for the application to be considered "healthy".

• process: Diego ensures that any process declared for the application will work. If the process ends, Diego stops and removes the application instance.

The type of health check can be specified when moving an application to the cloud using the push command:

cf push APP_NAME -u HEALTH-CHECK-TYPE -t HEALTH-CHECK-TIMEOUT

You can also specify the type of application health check in the manifest.yml file as follows:
- ...
health-check-type: http
health-check-http-endpoint: / health
timeout: 120

Using the CF CLI, you can assign a type of health check to an already running application using the following command:

cf set-health-check APP_NAME (process | port | http [--endpoint PATH])


Examples:

cf set-health-check APP_NAME http --endpoint PATH (default to /)
cf set-health-check APP_NAME port
cf set-health-check APP_NAME PROCESS_NAME

By entering the command cf set-health-check product-list http - endpoint / , the http test type was checked:


CF CLI cf get-health-check APP_NAME . , «Product List»:

cf get-health-check product-list



5. Application Logging ( )

, Application Logging ( ) SAP Cloud Platform, Kibana Cloud Foundry.

1)

. SAP – Cloud Foundry: github.com/SAP/cf-java-logging-support . pom.xml , (logback.xml) (ConfigLogging).

Eclipse Controller.java «product-list».
Logger:

private static final Logger logger = LoggerFactory.getLogger(Controller.class);


getProductByName :

logger.info("***First — Retrieving details for '{}'.", name);
logger.info("***Second — Retrieving details for '{}'.", name);


application-logs product-list ( ).

logback.xml.

• Eclipse logback.xml. <appender-ref ref=«STDOUT-JSON» /> <appender-ref ref=«STDOUT» />.




• . , Run As… -> Maven Build.

• Cloud Foundry.

SpringBoot,

cf push product-list.

2)

URL- SAP Cloud Platform.

, :

YOUR_APPLICATION_URL/productsByParam?name=Notebook Basic 15.

«Logs», , «product-list». «Open Log Analitic», :



URL-: logs.cf.eu10.hana.ondemand.com . , , SAP Cloud Platform.



, Kibana, «product-list».



«Requests and Logs».



.

msg . , , .. (Correlation ID). .



: Kibana, , 15 . — «Last 15 minutes» .





: Cloud Foundry SAP Cloud Platform : Application Logging .

6.

, .

, — , . , () . , . .

, , CF CLI SAP Cloud Platform, Application Autoscaler ( ) SAP Cloud Platform .

1)
, , , , . «Product List» , .

:
cf apps, , .



«Product List», , , . , :

cf stop APP_NAME

: , , «Product List». , , Github CF CLI ( ).

2)
, Cloud Foundry . : CF CLI SAP Cloud Platform. .

CF CLI

cf scale APP_NAME -k DISK , . DISK – , M G .
cf scale APP_NAME -m MEMORY , . MEMORY — , M G .

, , 712 . :

cf scale APP_NAME -m 712M

CF CLI ( «Yes» ). , SAP Cloud Platform, «Product List», , 712 . :
cf app APP-NAME

SAP Cloud Platform
.
( «Applications») ( ). «Overview». , , . , «Change Quota».



«Change Quota» , , , 512 . «Save».



, , «Overview» .

, . «Restart».



3)
. , . . : CF CLI SAP Cloud Platform. .

CF CLI
cf scale APP_NAME -i INSTANCES . INSTANCES.
, , , cf scale APP_NAME -i 2.
, , , . cf app APP_NAME.



SAP Cloud Platform

. . , , «Overview» , , .. . «+ Instance» «- Instance». «- Instance», .



4)
, cf scale SAP Cloud Platform. , , , . , , . , .

, Application Autoscaler SAP Cloud Platform.



, . , «Product List».

Controller Eclipse API.

@GetMapping("/scaleup")
public String scaleUp() {
String str = "";
HashMap<String, Double> h = null;
for (int i = 0; i < 100000; i++) {
h = new HashMap<>();
String key = new String(«key»);
Double value = new Double(100.98);
h.put(key, value);
str = str + h.toString();
}
return «success»;
}

, .




import java.util.HashMap; .



API, .
index.html, src/main/resources/index.html Insert the code to increase the memory of the application :

/* Adding Toolbar to the UI */
var toolBar = new sap.m.Toolbar();
var memButton = new sap.m.Button({
title: «Scale Up»,
press: function (oEvent) {
jQuery.sap.require(«sap.m.MessageBox»);
sap.m.MessageBox.show(
«This will scale up the memory of the application», {
icon: sap.m.MessageBox.Icon.INFORMATION,
title: «ScaleUp Application»,
actions: [sap.m.MessageBox.Action.OK],
onClose: function(oAction) {
var oScaleModel = new sap.ui.model.json.JSONModel();
var scaleUpUrl = document.URL + «scaleup»;
oScaleModel.loadData(scaleUpUrl);
console.log(scaleUpUrl);
}
}
);
}
});
memButton.setText(«Scale Up»);
toolBar.addContent(memButton);
productList.setHeaderToolbar(toolBar);


. :






.



, , , Application Autoscaler , .

1)
CF CLI, SAP Cloud Platform. .

, , «Product List» ( «dev»). «Services» «Service Marketplace». Application Autoscaler. .



«Instances» «New Instance».



.
. «Lite», «Next».



, . «Next».



, «Next».



, , «myautoscaler». «Finish».



Application Autoscaler.

2)

«Application Autoscaler». , :



, «Bind Instance».



. «product-list» .



. JSON. JSON . , :

{
«instance_min_count»: 1,
«instance_max_count»: 3,
«scaling_rules»: [
{
«metric_type»: «memoryused»,
«stat_window_secs»: 60,
«breach_duration_secs»: 60,
«threshold»: 400,
«operator»: ">=",
«cool_down_secs»: 60,
«adjustment»: "+1"
}
]
}

1 [adjustment: +1], . . . , , .
«Specify Parameters», , . «Save».



, «Application Autoscaler» .
1) , ( , «Applications» «Product List», «Overview» «Restart»);
2) , URL- ;
3) cf app <app_name> CF CLI, SAP Cloud Platform ( «Overview»);



4) «Scale Up» , ;
5) cf app <app_name> CF CLI, SAP Cloud Platform ( «Overview»);

6) , , ( threshold). , 3 4.
7) (threshold), breach_duration_secs , , «Application Autoscaler» 1.
8) cf app <app_name> CF CLI, SAP Cloud Platform ( «Overview»).



Conclusion

, Cloud Foundry SAP Cloud Platform , , .

Cloud Foundry SAP Cloud Platform:

Source: https://habr.com/ru/post/350690/


All Articles