This is a test project about implementing Java backend microservices on SAP BTP Cloud Foundry Platform.
- Guide to developing cloud applications using SAP Cloud Platform and Cloud Foundry.
- Spring-Boot Crud operations
- Deployment on Cloud Foundry (SAP BTP)
- O-Auth 2.0 protocol (XSUAA service)
- SaaS multi-tenant application (Schema isolation), saas-registry service
Simple Java application by using cf CLI via Spring Boot.
- This simple app will be invoked through a web microservice (application router).
- This app will set authentication checks and an authorization role to properly access your web application.
cf create-service xsuaa application xsuaa-service -c xs-security.json
cf unbind-service 'app_name' xsuaa-service cf delete-service 'xsuaa-service'
Where:
- 'xsuaa-service' is name of xsuaa service in SAP BTP
- 'application' is an application name
- In 'web' run commands which will create package.json and add approuter service:
npm init
npm install @sap/approuter --save
- In the root run:
mvn clean install
- Set the Cloud Foundry API endpoint for your subaccount on SAP BTP:
cf api https://api.cf.eu20.hana.ondemand.com
cf login or cf login --sso
- Deploy java application on SAP BTP Cloud Foundry:
mvn clean install cf push
Read values from the environment variable of the application:
cf env app_name
-
open Postman
-
create Get request
-
http field: put link on approuter from BTP Cloud Foundry
-
authorisation tab:
-
- Grant Type: 'Password credential'
-
- Access Token URL = xsuaa[0].credentials.url + "/oauth/token"
-
- Client ID = xsuaa[0].credentials.clientid
-
- Client Secret = xsuaa[0].credentials.clientsecret
-
- Scope = xsuaa[0].credentials.xsappname + ".admin"
Useful commands:
cf logs cf logs --recent //show logs from SAP BTP
cf marketplace //show available services
cf marketplace -s postgresql //show info about one service
cf events //show events cf create-service postgresql v9.6-dev postgres cf bind-service //connect two services
cf services //show services in your dev spase
cf apps //show downloaded apps cf app //show statistics
cf marketplace
cf create-service application-logs lite 'myapplogs'
cf services
cf bind-service 'appname' 'myapplogs'
cf restage 'appname'
Deep Dive 6 with SAP Cloud SDK: Extend your Cloud Foundry Application with Tenant-Aware Persistency
cf create-service postgresql-db trial postgres-instance cf bind-service sbtp postgres-instance //if you write it in manifest then ut will be automatically cf restage sbtp
where:
- sbtp is APP_NAME in BTP CLoud Foundry
- postgresql-db <- name from: cf marketplace
- trial <- your plan in BTP Cockpit
- postgres-instance <- name of your new instance 'postgresql-db'
Expand
- Activate SSH for your app:
cf enable-ssh sbtp
- Create a service key for your service instance using the cf create-service-key command:
cf create-service-key <service_instance_name_in_BTP> <my_new_key_name>
- Retrieve your new service key using the cf service-key command:
cf service-key <service_instance_name_in_BTP> <my_new_key_name>
- Open SSH connection:
cf ssh -L <local_port>:<service_hostname>:<service_port> <app_name>
like:
cf ssh -L 63306:postgres-ade9561e-2985-4c20-b601-097eee32029a.cqryblsdrbcs.us-east-1.rds.amazonaws.com:4605 sbtp
- <local_port> is available local port for port forwarding. For example, 63306
- <service_hostname> is the address provided under hostname in the service key retrieved earlier.
- <service_port> is the port provided under port.
- <app_name> is the name of your app in SAP BTP.
- Add SaaS Provisioning service Create config.json is a configuration file for SaaS Provisioning service
{
"xsappname":"helloworld",
"appUrls": {
"onSubscription" : "https://helloworld-ap25.cfapps.eu10.hana.ondemand.com/callback/v1.0/tenants/{tenantId}"
},
"displayName" : "Helloworld MTA",
"description" : "Helloworld MTA sample application",
"category" : "Custom SaaS Applications"
}
Create the SaaS Provisioning service instance with the config.json file:
cf create-service saas-registry application saas-registry-instance -c config.json
- Create a route for a consumer subaccount:
cf map-route 'approuter_name' cfapps.us10-001.hana.ondemand.com --hostname 'subaccount_subdomain'-'first_part_of_approuter_rout'
Expand
- Configure application for debugging. Add setting to manifest.yaml:
JBP_CONFIG_JAVA_OPTS: "[java_opts: '-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n,onjcmd=y']"
env:
JBP_CONFIG_SAP_MACHINE_JDK: "[default_debug_agent_active: true]"
- Redeploy application:
cf push
- Enter in file system of a remote app:
cf ssh < app name >
- Move to 'bin' directory:
cd app/META-INF/.sap_java_buildpack/sap_machine_jdk/bin
- Run the following command to found $JAVA_PID of debug process:
'META-INF/.sap_java_buildpack/sap_machine_jdk/bin/java'
ps aux
exit
- Start the debug process. Replace $JAVA_PID on your number, and <app_name>:
cf ssh sbtp -c "export JAVA_PID=
ps java pid=
&& app/META-INF/.sap_java_buildpack/sap_machine_jdk/bin/jcmd $JAVA_PID VM.start_java_debugging"
- Open SSH connection to debug process:
cf ssh -N -T -L 8000:localhost:8000 <app_name>
Expand
- Prepare request like this [Using Postman for API Testing with XSUAA]
- Delete scope
- Change 'Grand Type' to 'Client credentials'
- Expand 'advanced' and create parameter x-zid, value is a subaccount tenant ID in CF:
- Get token for current tenant and use it on route of application (You can parse your token by JWT.io, and check "x-zid" section to make sure that you have correct tenantId):
https://sbtp-app-rout.cfapps.us10-001.hana.ondemand.com/books