上一期咱们介绍了如何基于 Knative Serverless 技术实现天气服务-上篇,首先咱们先来回顾一下上篇介绍的内容:git
接下来咱们介绍如何经过表格存储提供的通道服务,实现 Knative 对接表格存储事件源,订阅并经过钉钉发送天气提醒通知。github
回顾一下总体架构:web
首先咱们介绍一下表格存储提供的通道服务。通道服务(Tunnel Service)是基于表格存储数据接口之上的全增量一体化服务。通道服务为您提供了增量、全量、增量加全量三种类型的分布式数据实时消费通道。经过为数据表创建数据通道,您能够简单地实现对表中历史存量和新增数据的消费处理。经过数据通道能够进行数据同步、事件驱动、流式数据处理以及数据搬迁。这里事件驱动正好契合咱们的场景。api
先看一下处理流程图:微信
下面咱们来详细介绍一下。架构
在 Knative 中自定义事件源其实很容易,能够参考官方提供的自定义事件源的实例:https://github.com/knative/do...。app
咱们这里定义数据源为 AliTablestoreSource。代码实现主要分为两部分:less
关于自定义 TableStore 事件源实现参见 GitHub 源代码:https://github.com/knative-sample/tablestore-source分布式
部署自定义事件源服务以下:微服务
从 https://github.com/knative-sample/tablestore-source/tree/master/config 中能够获取事件源部署文件,执行下面的操做:
kubectl apply -f 200-serviceaccount.yaml -f 201-clusterrole.yaml -f 202-clusterrolebinding.yaml -f 300-alitablestoresource.yaml -f 400-controller-service.yaml -f 500-controller.yaml -f 600-istioegress.yaml
部署完成以后,咱们能够看到资源控制器已经开始运行:
[root@iZ8vb5wa3qv1gwrgb3lxqpZ config]# kubectl -n knative-sources get pods NAME READY STATUS RESTARTS AGE alitablestore-controller-manager-0 1/1 Running 0 4h12m
因为咱们是经过 Knative Eventing 中 Broker/Trigger 事件驱动模型对天气事件进行处理。首先咱们建立用于数据接收的 Broker 服务。
apiVersion: eventing.knative.dev/v1alpha1 kind: Broker metadata: name: weather spec: channelTemplateSpec: apiVersion: messaging.knative.dev/v1alpha1 kind: InMemoryChannel
这里须要说明一下,建立事件源实例其实就是在表格存储中建立通道服务,那么就须要配置访问通道服务的地址、accessKeyId 和 accessKeySecret,这里参照格式:{ "url":"https://xxx.cn-beijing.ots.aliyuncs.com/", "accessKeyId":"xxxx","accessKeySecret":"xxxx" }
设置并进行 base64 编码。将结果设置到以下 Secret 配置文件 alitablestore
属性中:
apiVersion: v1 kind: Secret metadata: name: alitablestore-secret type: Opaque data: # { "url":"https://xxx.cn-beijing.ots.aliyuncs.com/", "accessKeyId":"xxxx","accessKeySecret":"xxxx" } alitablestore: "<base64>"
建立 RBAC 权限:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: eventing-sources-alitablestore subjects: - kind: ServiceAccount name: alitablestore-sa namespace: default roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: eventing-sources-alitablestore-controller --- apiVersion: v1 kind: ServiceAccount metadata: name: alitablestore-sa secrets: - name: alitablestore-secret
建立 AliTablestoreSource 实例,这里咱们设置接收事件的 sink
为上面建立的 Broker 服务。
--- apiVersion: sources.eventing.knative.dev/v1alpha1 kind: AliTablestoreSource metadata: labels: controller-tools.k8s.io: "1.0" name: alitablestoresource spec: # Add fields here serviceAccountName: alitablestore-sa accessToken: secretKeyRef: name: alitablestore-secret key: alitablestore tableName: weather instance: knative-weather sink: apiVersion: eventing.knative.dev/v1alpha1 kind: Broker name: weather
建立完成以后,咱们能够看到运行中的事件源:
[root@iZ8vb5wa3qv1gwrgb3lxqpZ config]# kubectl get pods NAME READY STATUS RESTARTS AGE tablestore-alitablestoresource-9sjqx-656c5bf84b-pbhvw 1/1 Running 0 4h9m
如何进行钉钉通知呢,咱们能够建立一个钉钉的群组(能够把家里人组成一个钉钉群,天气异常时,给家人一个提醒),添加群机器人:
获取 webhook :
这里咱们假设北京 (110000),日期:2019-10-13, 若是天气有雨,就经过钉钉发送通知提醒,则服务配置以下:
apiVersion: serving.knative.dev/v1beta1 kind: Service metadata: name: day-weather spec: template: spec: containers: - args: - --dingtalkurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxxx - --adcode=110000 - --date=2019-10-13 - --dayweather=雨 image: registry.cn-hangzhou.aliyuncs.com/knative-sample/dingtalk-weather-service:1.2
关于钉钉提醒服务具体实现参见 GitHub 源代码:https://github.com/knative-sample/dingtalk-weather-service
最后咱们建立 Trigger订阅天气事件,而且触发天气提醒服务:
apiVersion: eventing.knative.dev/v1alpha1 kind: Trigger metadata: name: weather-trigger spec: broker: weather subscriber: ref: apiVersion: serving.knative.dev/v1alpha1 kind: Service name: day-weather
订阅以后,若是北京 (110000),日期:2019-10-13, 天气有雨,会收到以下的钉钉提醒:
这里其实还有待完善的地方:
有兴趣的能够继续完善当前的天气服务功能。
本文介绍了如何在 Knative 中自定义事件源,并经过事件驱动接收天气变化信息,订阅并经过钉钉推送通知提醒。这样基于 Knative Serverless 技术实现天气服务总体实现就介绍完了。有兴趣的同窗能够针对上面提到的不足继续研究。仍是那句话,作好天气服务不容易,但还好我有 Knative。
“ 阿里巴巴云原生微信公众号(ID:Alicloudnative)关注微服务、Serverless、容器、Service Mesh等技术领域、聚焦云原生流行技术趋势、云原生大规模的落地实践,作最懂云原生开发者的技术公众号。”
搜索「阿里巴巴云原生公众号」获取更多K8s容器技术内容