在 SAP BTP Kyma Runtime 上使用 Redis 读取和存储数据
https://developers.sap.com/tutorials/cp-kyma-redis-function.html
This sample provides a Redis deployment and two serverless functions that interact with it.
這個例子提供了一個 Redis 應用部署和兩個使用 Redis 的 serverless 函數(shù)。
The function cache-order will be set to subscribe to an order.created event provided by the Commerce mock application.
Commerce Mock 應用有一個 order.created 事件,cache-order 函數(shù)訂閱這個事件。
Once triggered, the function will perform an API call to the Commerce mock to obtain additional details regarding the order and then cache the information into Redis.
一旦 order.created 事件觸發(fā),cache-order 函數(shù)響應該事件,調(diào)用一個 Commerce Mock API,讀取創(chuàng)建訂單的更多明細,然后將數(shù)據(jù)存儲到 Redis 里。
The function get-order, exposed as an API, is used to then retrieve the order details from the Redis cache.
函數(shù) get-order, 以 API 的形式暴露,負責從 Redis cache 里讀取訂單明細。
從這個Github倉庫里下載 sample 代碼。
進入 Kyma 控制臺,點擊 Create Binding:
In this step, you will bind the Commerce Mock application to the dev namespace. This will allow the APIs and Events of the mock application to be used within the namespace.
這一步會將 Commerce Mock 應用同 dev 命名空間綁定在一起,這使得 Mock 應用的 API 和 Events 能夠在 dev 命名空間內(nèi)被使用。
點擊 Create:
Create the Events service instances - 創(chuàng)建事件服務實例
In this step, you will create service instances of the Events the Commerce Mock application exposes. This will allow the events to be consumed from resources within the namespaces such as functions or microservices.
創(chuàng)建 Commerce Mock 應用暴露的事件的服務實例。這樣,dev 命名空間下的資源,比如 functions 和 microservices 就能夠消費這些事件。
選擇 commerce-mock:
選擇 SAP Commerce Cloud - Events:
點擊 Add 按鈕,創(chuàng)建一個 Service 實例:
創(chuàng)建好的 service 實例:
Step 4: Create API service instances
In this step, you will create a service instance of the Commerce Webservices the Mock application exposes.
Mock Application 暴露的 Commerce Web Service, 可以基于其創(chuàng)建服務實例。該服務實例能夠?qū)?API 暴露給同一命名空間下的其他資源,比如函數(shù)和微服務。
查看兩個成功創(chuàng)建的服務實例:
下一步,部署三個資源到 Kyma 上:
該函數(shù)訂閱 Commerce Mock Application 暴露的 order.created 事件。該事件發(fā)生后,cache-order 函數(shù)調(diào)用 Commerce mock Application,獲得 totalPriceWithTax,然后存儲到 Redis 去。
Redis cache 里通過 cache-order 存儲的訂單信息,通過 get-order 暴露給消費者。
這個 deployment 定義了 Redis cache 配置信息,以及關聯(lián)的 Kubernetes Service, 后者將 Redis 實例暴露給(1)(2) 兩個 serverless 函數(shù)使用。
依次將 k8s 文件夾下的 yaml 文件進行部署:
確保 deployment 狀態(tài)全部為 Running:
Add event subscription to function
In this step, you will configure the function cache-order, deployed in the previous step, to run when the order.created event is fired from the Commerce Mock application.
這一步,將 cache-order 訂閱到 order.created 事件上。
從 functions 列表里找到 cache-order:
創(chuàng)建一個事件訂閱 Event subscription:
cache-order function 需要調(diào)用對應的 API,因此需要為其創(chuàng)建 service binding.
通過這個 Service Binding,就可以借助該 Service Instance,調(diào)用 Commerce Webservice了:
對 function 代碼進行微調(diào)
找到環(huán)境變量里包含 GATEWAY_URL 的變量:
點擊 commerce-mock 的 host 超鏈接,手動觸發(fā)一個 order.created 事件。
點擊 Remote API:
找到 SAP Commerce Cloud - Events:
點擊按鈕 Send Event,模擬發(fā)送一個 order.created 事件,payload 里包含的 orderCode 為 76270000:
事件成功發(fā)送:
在 cache-order 的 logs 面板里,果然看到了訂單 code 76270000 對應的處理日志:
下一步,再試試 get-order 函數(shù),是否能夠從 Redis cache 中讀取訂單信息。
點擊這個超鏈接:https://get-order.c-46d70f2.kyma.shoot.live.k8s-hana.ondemand.com/
會收到如下錯誤消息:“error”: “No orderCode received!”
這是因為我們尚未為該 serverless function 指定 orderCode.
試試在 url 后手動加上 orderCode:
https://get-order.c-46d70f2.kyma.shoot.live.k8s-hana.ondemand.com/?orderCode=76270000
現(xiàn)在就能夠正常工作了。
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的在 SAP BTP Kyma Runtime 上使用 Redis 读取和存储数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dockerfile命令RUN、CMD、
- 下一篇: prometheus监控主机