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

Knative practice: implementation of Weather Service based on Knative Serverless Technology-part two

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

In the last issue, we introduced how to implement weather services based on Knative Serverless technology. In the first part, let's review the contents of the previous article:

Through Gaode Weather API interface, regular events are sent every 3 hours to store and update the weather information of domestic cities in the next 3 days to table storage and provide RESTful API query weather information.

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

Overall architecture

Review the overall architecture:

Cdn.com/b4bb37ed5338e0a138c76a2e72a4a9583dbf7236.png ">

Through the CronJob event source, the scheduled events are sent regularly every 3 hours, and the weather information of domestic cities in the next 3 days is stored and updated to the table storage to provide RESTful API query weather information through the channel service provided by the table storage, so that the TableStore event source uses the Borker/Trigger event-driven model, and the subscription weather information notifies the nailing message according to the weather information received by the subscription. If it rains tomorrow, you will be prompted to bring an umbrella and other weather services based on Knative.

First of all, let's introduce the channel services provided by table storage. Channel Service (Tunnel Service) is a fully incremental integrated service based on table storage data interface. Channel service provides you with three types of distributed real-time data consumption channels: incremental, full, incremental plus full. By establishing a data channel for the data table, you can simply realize the consumption of the historical stock and new data in the table. Data synchronization, event-driven, streaming data processing and data relocation can be carried out through the data channel. The event-driven here fits our scene.

Take a look at the processing flow chart first:

Define the TableStore event source, which is used to receive channel service data through the Borker/Trigger event-driven model, subscribe to weather information, subscribe to the received weather information and send it to the weather reminder service for pinning message notification.

Let's introduce it in detail.

Customize the TableStore event source

It's actually easy to customize event sources in Knative. You can refer to the official example of custom event sources: https://github.com/knative/docs/tree/master/docs/eventing/samples/writing-a-source.

Here we define the data source as AliTablestoreSource. The code implementation is mainly divided into two parts:

Resource controller-Controller: receives AliTablestoreSource resources and creates a Tunnel event sink in the channel service-Receiver: listens for events through Tunnel Client and sends received events to the target service (Broker)

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

Deploy the custom event source service 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 the 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 deal with weather events through the Broker/Trigger event-driven model in Knative Eventing. First, we create a Broker service for data reception.

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

It needs to be explained here that to create an event source instance is to create a channel service in the table store, so you need to configure the address, accessKeyId and accessKeySecret to access the channel service. Here, refer to the format: {"url": "https://xxx.cn-beijing.ots.aliyuncs.com/"," accessKeyId ":" xxxx "," accessKeySecret ":" xxxx "} and base64 encoding. Set the result to the following Secret profile alitablestore property:

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 the event to 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/v1alpha1kind: Broker name: weather

After the creation is complete, we can see the running event source:

[root@iZ8vb5wa3qv1gwrgb3lxqpZ config] # kubectl get podsNAME READY STATUS RESTARTS AGEtablestore-alitablestoresource-9sjqx-656c5bf84b-pbhvw 1 Running 1 Running 0 4h9m subscription event and Notification reminders create Weather Alert Service

How to notify the nailing? we can create a nailing group (we can group the family into a nailing group and give the family a reminder when the weather is abnormal) and add a group of robots:

Get webhook:

Here we assume that 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 more information about the implementation of nail reminder service, please see GitHub source code: https://github.com/knative-sample/dingtalk-weather-service

Create a subscription

Finally, we create a Trigger to subscribe to weather events and trigger the weather reminder service:

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, the weather is rainy, you will receive the following nail reminder:

In fact, there is still room for improvement:

Can I subscribe based on the city (subscribe to the target city only)? Can I send a message reminder at a specified time (the weather reminder for day 2 will be pushed at 8: 00 p. M. that day)?

Those who are interested can continue to improve the current weather service function.

Summary

This article introduces how to customize the event source in Knative, receive weather change information through event driver, subscribe and send notification reminders through nails. In this way, the overall implementation of weather service based on Knative Serverless technology is introduced. Interested students can continue to study the shortcomings mentioned above. Again, it's not easy to do a good weather service, but luckily I have Knative.

"Alibaba Yun × × icloudnative × × erverless, containers, Service Mesh and other technical fields, focusing on cloud native popular technology trends, cloud native large-scale landing practice, to do the best understanding of cloud native development × ×

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

*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