Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize event sources in Knative and receive weather change information through event drivers

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to customize the event source in Knative and receive weather change information through event driving, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

Next, we introduce how to implement Knative docking table storage event sources through the channel service provided by table storage, subscribe and send weather alert notifications through nails.

overall architecture

Consider the overall structure:

Through CronJob event source, timing events are sent every 3 hours, and the weather information of domestic cities in the next 3 days is stored and updated to table storage.

Provide RESTful API to query weather information

Implement TableStore event sources through channel services provided by tabular storage

Subscribe to weather information via Borker/Trigger event-driven models

Pinned message notifications based on weather information received by subscription. If it rains tomorrow, bring an umbrella.

Weather Services Based on Knative

First let's introduce the channel services provided by table storage. Tunnel Service is a fully incremental integrated service based on tabular storage data interfaces. Channel service provides you with three types of distributed data real-time consumption channels: incremental, full, and incremental plus full. By creating data channels for a data table, you can simply consume historical inventory and new data in the table. Data synchronization, event-driven, streaming data processing and data migration can be performed through the data channel. The event drivers here fit right into our scenario.

Let's look at the process flow chart:

Defines TableStore event sources for receiving channel service data

Subscribe to weather information via Borker/Trigger event-driven models

Subscribe to the weather information received and send it to the weather alert service for nail message notification

Let's talk about it in detail.

Custom TableStore event sources

Customizing event sources in Knative is actually very easy. You can refer to the official custom event source example: https://github.com/knative/docs/tree/master/docs/eventing/samples/writing-a-source.

We define the data source here as AliTablestoreSource. The code implementation is divided into two main parts:

Resource Controller-Controller: Receives AliTablestoreSource resource, creates Tunnel in Channel Service

Event Receiver: Listen for events through Tunnel Client and send the received events to the target service ( Broker)

For custom TableStore event source implementation see GitHub source code: github.com/knative-sample/tablestore-source

Deploy custom event source services as follows:

You can get the event source deployment file from https://github.com/knative-sample/tablestore-source/tree/master/config and do the following:

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

After deployment is complete, we can see that the resource controller is already running:

[root@iZ8vb5wa3qv1gwrgb3lxqpZ config]# kubectl -n knative-sources get podsNAME READY STATUS RESTARTS AGEalitablestore-controller-manager-0 1/1 Running 0 4h22m Create Event Source

Because we process weather events through the Broker/Trigger event-driven model in Knative Eventing. First we create a Broker service for data reception.

BrokerapiVersion: eventing.knative.dev/v1alpha1kind: Brokermetadata: name: weatherspec: channelTemplateSpec: apiVersion: messaging.knative.dev/v1alpha1 kind: InMemoryChannel creates an event source instance

It should be noted here that creating an event source instance is actually creating a channel service in the table storage, so you need to configure the address, accessKeyId and accessKeySecret of the access channel service. Here, refer to the format: { "url":"https://xxx.cn-beijing.ots.aliyuncs.com/", "accessKeyId":"xxxx","accessKeySecret":"xxxx" } and perform base64 encoding. Set the result to the alitablestore property of the Secret profile as follows:

apiVersion: v1kind: Secretmetadata: name: alitablestore-secrettype: Opaquedata: # { "url":"https://xxx.cn-beijing.ots.aliyuncs.com/", "accessKeyId":"xxxx","accessKeySecret":"xxxx" } alitablestore: ""

Create RBAC permissions:

apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: eventing-sources-alitablestoresubjects:- kind: ServiceAccount name: alitablestore-sa namespace: defaultroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: eventing-sources-alitablestore-controller---apiVersion: v1kind: ServiceAccountmetadata: name: alitablestore-sasecrets:- name: alitablestore-secret

Create an AliTablestoreSource instance, where we set the sink that receives events to be the Broker service created above.

---apiVersion: sources.eventing.knative.dev/v1alpha1kind: AliTablestoreSourcemetadata: labels: controller-tools.k8s.io: "1.0" name: alitablestoresourcespec: # 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

Once created, we can see the event source in action:

[root@iZ8vb5wa3qv1gwrgb3lxqpZ config]# kubectl get podsNAME READY STATUS RESTARTS AGEtablestore-alitablestoresource-9sjqx-656c5bf84b-pbhvw 1/1 Running 0 4h9m Subscribe to events and notification alerts Create weather alert services

How to make a nail notification, we can create a nail group (you can group your family into a nail group, when the weather is abnormal, give your family a reminder), add a group robot:

Get webhook :

Here we assume Beijing (110000), date: 2019-10-13, if the weather is rainy, send a notification reminder through nails, then the service configuration is as follows:

apiVersion: serving.knative.dev/v1beta1kind: Servicemetadata: name: day-weatherspec: template: spec: containers: - args: - --dingtalkurl=https://oapi.dingtalk.com/robot/send? access_token=xxxxxx - --adcode=110000 - --date=2019-10-13 - dayweather= rain image: registry.cn-hangzhou.aliyuncs.com/knative-sample/dingtalk-weather-service:1.2

For the specific implementation of nail reminder service, see GitHub source code: github.com/knative-sample/dingtalk-weather-service

create a subscription

Finally we create Trigger to subscribe to weather events and trigger weather alert services:

apiVersion: eventing.knative.dev/v1alpha1kind: Triggermetadata: name: weather-triggerspec: broker: weather subscriber: ref: apiVersion: serving.knative.dev/v1alpha1 kind: Service name: day-weather

After subscription, if Beijing (110000), date: 2019-10-13, weather is rainy, you will receive the following nail reminder:

Here's what's left to be done:

Can I subscribe based on city (target city only)?

Is it possible to send a message alert at a specified time (8 p.m. on the same day to push the weather alert for the next day)?

How to customize event feeds in Knative, receive weather changes via event drivers, subscribe to and push notifications via nails. Thus, the overall implementation of weather service based on KnativeServerless technology is introduced. Interested students can continue to study the shortcomings mentioned above. Again, it's not easy to do good weather service, but fortunately I have Knative.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 292

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report