Use Redis in SAP BTP, Kyma Runtime to Store and Retrieve Data
- How to deploy a microservice using Kyma dashboard
- How to deploy Functions using Kyma dashboard
- How to create APIRules
- How APIs and Events are used.
Prerequisites
- Kyma provisioned
- Deploy Commerce Mock Application in SAP BTP, Kyma Runtime tutorial completed
This sample provides a Redis deployment and two Serverless Functions that interact with it. The cache-order
Function is set to subscribe to the order.created event provided by the commerce mock application. Once triggered, the Function performs an API call to the commerce mock to obtain additional details regarding the order and then cache the information into Redis. The get-order
Function, exposed as an API, is used to retrieve the order details from the Redis cache.
- Step 1
-
Go to the kyma-runtime-extension-samples repository. This repository contains a collection of Kyma sample applications which will be used during the tutorial.
-
Use the green Code button to choose one of the options to download the code locally, or simply run the following command using your CLI at your desired folder location:
Shell/BashCopygit clone https://github.com/SAP-samples/kyma-runtime-extension-samples
-
- Step 2
-
Open Kyma dashboard using the Console URL link in SAP BTP cockpit.
-
Choose Modify Modules, and select Add.
-
In the Add Modules section, check
eventing
,nats
, andserverless
, and select Add.
You can find more info in Add and Delete a Kyma Module.
-
- Step 3
When you add the Eventing module, you must configure a backened for it. Follow these steps:
-
In your Modules section, go to eventing, and choose Edit.
-
In Backend Type, choose NATS from the dropdown, and select Save.
You can find more info in Choose a Backend for Kyma Eventing.
-
- Step 4
In this step, you will deploy the Redis resources, obtained in Step 1, into SAP BTP, Kyma runtime. These resources include:
redis.yaml
: The deployment defining the Redis cache configuration.redis-service.yaml
: The Kubernetes Service which exposes the Redis instance to the two Serverless Functions.redis-secret.yaml
: The Kubernetes Secret which defines the Redis password.
-
In the
dev
namespace, choose Upload YAML and upload or copy the contents of theredis-function/k8s/redis.yaml
file, and click Upload. -
Perform the same procedure to upload the contents of the
redis-function/k8s/redis-service.yaml
file. -
Go to Configuration > Secrets, and choose Create.
-
Switch to the YAML tab, and copy the contents of the
redis-function/k8s/redis-secret.yaml
file over-writing the preexisting content found within the pane, and then choose Create. -
After completing the deployment of the resources, go to Cluster Details and see the Pods Overview and Deployments Overview section. If there are no Failing Pods or Failing Deployments, then everything is ready.
- Step 5
In this step, you will deploy two Functions into SAP BTP, Kyma runtime that were obtained in Step 1. These resources include:
cache-order-function.yaml
: This Function subscribes to an event published by the commerce mock application. Once triggered, it calls back to the commerce mock application to obtain thetotalPriceWithTax
and then cache the information into Redis.get-order-function.yaml
: This Function is exposed as an API allowing retrieval of data stored within the Redis cache.
-
In the
dev
namespace, choose Upload YAML, and upload or copy the contents of theredis-function/k8s/cache-order-function.yaml
file. -
Change the Node.js version to
nodejs20
by adjusting the value ofspec.runtime
, and click Upload. -
Repeat the steps to create the
get-order
Function using the fileredis-function/k8s/get-order-function.yaml
.
- Step 6
- To expose the
get-order
Function as an API, choose the menu option Discovery and Network > API Rules, and click Create. - Provide the value
get-order
for the Name, Service Name and Host, and choose Create.
- To expose the
- Step 7
In this step, you will configure the
cache-order
Function, deployed in the previous step, to run when the order.created event is fired from the commerce mock application.-
In the
dev
namespace, choose Configuration > Subscriptions. -
Choose Create, and use the following values:
- Name: order-created
- Types: order.created.v1
- Service: cache-order
- Source: mp-commerce-mock
What is the status of the function after it successfully builds?
-
- Step 8
In this step, you will copy the URL to the commerce web services API which will be used by the
cache-order
Function.-
Choose the menu option Back to Cluster Details to go back to the Kyma home workspace.
-
In the Kyma home workspace, choose Integration > Applications.
-
Choose the mp-commerce-mock application.
-
Choose SAP Commerce Cloud - Commerce Webservices.
-
Choose the Copy to Clipboard option to get the Central Gateway URL of the SAP Commerce Cloud - Commerce Webservices. This will be used in the
cache-order
Function.
-
- Step 9
In this step, the URL copied in the previous step will be assigned to an environment variable of the
cache-order
Function. This allows the Function to call the SAP Commerce Cloud - Commerce Webservices of the commerce mock application.-
Go to the
dev
namespace, and choose Workloads > Functions. -
Choose the cache-order Function to open it.
-
Choose Edit, scroll down to Environment Variables and find the Environment Variable with the Variable Name
CENTRAL_GATEWAY_URL
. -
Paste the value copied in the previous step into the Value field. Make sure that the value begins with
http://central-application...
and choose Save. -
Choosing Save will cause the Function to be rebuilt and deployed. The Status field will indicate that the Function is Deploying and will change to Running once this process completes.
-
- Step 10
With the configuration steps completed, you can now test the scenario to validate that it is working as intended.
-
Open the mock application in the browser by choosing Discovery and Network > API Rules from the menu.
-
Paste the Hosts entry for the commerce-mock APIRule to your browser and press Enter. This URL should be similar to:
https://commerce.*******.kyma.ondemand.com
-
Choose the Remote APIs tab.
-
Choose the SAP Commerce Cloud - Events option.
-
For the Event Topics, choose order.created.v1.
-
Modify the
orderCode
value as desired and choose Send Event. Make sure to remember theorderCode
value, it will be used in a later step.
What process exposes the Events and APIs of a connected system to a namespace?
-
- Step 11
In this step, we will view the logs outputted by the Function to verify that the scenario is working.
-
In the
dev
namespace, choose Workload > Functions. -
Choose the cache-order Function.
-
Scroll to the bottom of the pane to find the option Replicas of the Function and choose the value found in the table.
-
Under Containers, choose View Logs for the container function.
-
If necessary, search for the
orderCode
value. -
The output should be similar to:
-
- Step 12
In this step, we use the
get-order
Function to perform a read request of the data cached in the Redis database.-
Choose Discovery and Network > API Rules from the menu.
-
Paste the Hosts entry for the
get-order
APIRule to your browser and press Enter. When opened for the first time, you will receive the message:{"error":"No orderCode received!"}
-
Append the value
?orderCode={YOUR_ORDER_CODE}
to the URL where the value is the same as used when sending the event, for example:https://get-order.*********.kyma.ondemand.com/?orderCode=1231231
This should output the value saved when the event was submitted.
`{"orderCode":"1231231","Date":"Tue Nov 17 2020 19:28:42 GMT+0000 (Coordinated Universal Time)","Value":"100"}`
Congratulations! You have successfully completed the mission.
-
- Clone the Git repository
- Add the Serverless, Eventing and NATS modules in SAP BTP, Kyma runtime
- Choose a backend for Kyma Eventing
- Deploy Redis
- Deploy Functions
- Expose a Function
- Add event subscription to the Function
- Obtain the API URL for the Function
- Adjust Function variable
- Test event consumption
- Review output in the Function logs
- Get output from the Function APIRule