在 SAP BTP Kyma Runtime 上使用 Redis 读取和存储数据

举报
Jerry Wang 发表于 2022/06/11 09:59:00 2022/06/11
【摘要】 https://developers.sap.com/tutorials/cp-kyma-redis-function.htmlThis sample provides a Redis deployment and two serverless functions that interact with it.这个例子提供了一个 Redis 应用部署和两个使用 Redis 的 serverle...

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 上:

  1. cache-order: This function will subscribe to an event published by the Commerce mock application. Once triggered, it will call back to the Commerce mock application to obtain the totalPriceWithTax and then cache the information into Redis.

该函数订阅 Commerce Mock Application 暴露的 order.created 事件。该事件发生后,cache-order 函数调用 Commerce mock Application,获得 totalPriceWithTax,然后存储到 Redis 去。

  1. get-order: This function will be exposed as an API allowing retrieval of data stored within the Redis cache.

Redis cache 里通过 cache-order 存储的订单信息,通过 get-order 暴露给消费者。

  1. redis-deployment: This deployment defines the Redis cache configuration and the associated Kubernetes service which exposes the Redis instance to the two Serverless functions.

这个 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

现在就能够正常工作了。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。