在 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 函數。
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 函數訂閱這個事件。
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 事件觸發,cache-order 函數響應該事件,調用一個 Commerce Mock API,讀取創建訂單的更多明細,然后將數據存儲到 Redis 里。
The function get-order, exposed as an API, is used to then retrieve the order details from the Redis cache.
函數 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 命名空間內被使用。
點擊 Create:
Create the Events service instances - 創建事件服務實例
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.
創建 Commerce Mock 應用暴露的事件的服務實例。這樣,dev 命名空間下的資源,比如 functions 和 microservices 就能夠消費這些事件。
選擇 commerce-mock:
選擇 SAP Commerce Cloud - Events:
點擊 Add 按鈕,創建一個 Service 實例:
創建好的 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, 可以基于其創建服務實例。該服務實例能夠將 API 暴露給同一命名空間下的其他資源,比如函數和微服務。
查看兩個成功創建的服務實例:
下一步,部署三個資源到 Kyma 上:
該函數訂閱 Commerce Mock Application 暴露的 order.created 事件。該事件發生后,cache-order 函數調用 Commerce mock Application,獲得 totalPriceWithTax,然后存儲到 Redis 去。
Redis cache 里通過 cache-order 存儲的訂單信息,通過 get-order 暴露給消費者。
這個 deployment 定義了 Redis cache 配置信息,以及關聯的 Kubernetes Service, 后者將 Redis 實例暴露給(1)(2) 兩個 serverless 函數使用。
依次將 k8s 文件夾下的 yaml 文件進行部署:
確保 deployment 狀態全部為 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:
創建一個事件訂閱 Event subscription:
cache-order function 需要調用對應的 API,因此需要為其創建 service binding.
通過這個 Service Binding,就可以借助該 Service Instance,調用 Commerce Webservice了:
對 function 代碼進行微調
找到環境變量里包含 GATEWAY_URL 的變量:
點擊 commerce-mock 的 host 超鏈接,手動觸發一個 order.created 事件。
點擊 Remote API:
找到 SAP Commerce Cloud - Events:
點擊按鈕 Send Event,模擬發送一個 order.created 事件,payload 里包含的 orderCode 為 76270000:
事件成功發送:
在 cache-order 的 logs 面板里,果然看到了訂單 code 76270000 對應的處理日志:
下一步,再試試 get-order 函數,是否能夠從 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
現在就能夠正常工作了。
更多Jerry的原創文章,盡在:“汪子熙”:
總結
以上是生活随笔為你收集整理的在 SAP BTP Kyma Runtime 上使用 Redis 读取和存储数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dockerfile命令RUN、CMD、
- 下一篇: linux cmake编译源码,linu