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 realize Weather Service based on Knative Serverless Technology

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to implement weather services based on Knative Serverless technology. The editor thinks it is very practical, so I hope you can get something after reading this article. Let's take a look at it with the editor.

When it comes to weather forecasting service, our first reaction is a very simple service. At present, there are a lot of weather forecast API on the Internet that can be used directly. Is it necessary to use Knative to set up a set? Break a butterfly on the wheel? Don't worry, let's take a look at the actual scenario requirements:

Scene demand 1: according to the local weather information over the years, predict the approximate time of high temperature next year.

Scene requirement 2: the weather is changeable recently. If it rains tomorrow, could you give me a reminder with an umbrella before going to work in the morning?

Scenario requirement 3: the leader said, "the recent economic downturn, the company's financial constraints, the server, you provide weather, road conditions and other services with the Mini Program, but to ensure the normal provision of services."

From the above demand, we actually find that in order to do a good weather forecasting service, it is not so simple to face internal worries (resource shortage) and external problems (increased demand). But don't worry now, we can use Knative to help you solve the above problems.

Keywords: weather query, table storage, channel service, event notification

Scene requirement

First of all, let's describe the weather service scenario we are going to do:

1. Provide external weather forecast RESTful API

Query the weather information of domestic cities according to city and date (support for the next 3 days)

No limit on the number of queries, supporting larger concurrent queries (1000)

two。 Weather warning

Subscribe to domestic city weather information, according to the actual subscription to the urban area, remind you to bring an umbrella when it rains tomorrow

Use nails to notify

Overall architecture

When there is a demand, then we begin to implement weather services based on Knative. Let's first take a look at the overall architecture:

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

Provide RESTful API to query weather information

Realize the TableStore event source through the channel service provided by table storage.

Subscribe the weather information of the target city through the Borker/Trigger event-driven model

The nailing message notification is made according to the weather information received by the subscription. If it rains tomorrow, remind you to bring an umbrella, etc.

Based on the fact that there are many contents, we will introduce them in the first and next two articles respectively:

In the first part, we will mainly introduce how to connect the third-party weather forecast API, synchronize and update the weather information regularly, and provide RESTful API.

In the next section, we will focus on how to implement TableStore event sources, subscribe to weather information, and send reminders through nails.

Implementation of Weather Service based on Knative-the first part connects Gaode Open platform Weather Forecast API

There are many API for weather query. Here we choose the weather query API provided by Gaode Open platform, which is easy to use and stable. The weather forecast API provides 100000 free calls every day to support weather information query in more than 3500 regions of China. In addition, Gaode open platform, in addition to weather forecast, can also provide IP positioning, search services, path planning, etc., interested can also study how to play.

Log in to Gaode Open platform: https://lbs.amap.com, create an application, and obtain Key:

After obtaining the Key, you can access it directly through url: https://restapi.amap.com/v3/weather/weatherInfo?city=110101&extensions=all&key=. The weather information is returned as follows:

{"status": "1", "count": "1", "info": "OK", "infocode": "10000", "forecasts": [{"city": "Hangzhou", "adcode": "330100", "province": "Zhejiang", "reporttime": "2019-09-24 20:49:27" "casts": [{"date": "2019-09-24", "week": "2", "dayweather": "Qing", "nightweather": "cloudy", "daytemp": "29" "nighttemp": "17", "daywind": "No wind direction", "nightwind": "no wind direction", "daypower": "≤ 3", "nightpower": "≤ 3"} Synchronize and update weather information regularly.

This function mainly realizes the docking of Gaode open platform weather forecast API to obtain weather forecast information, and the docking of Aliyun Table Storage Service (TableStore) for weather forecast data storage. The specific operations are as follows:

Receive CloudEvent timing events

Query the weather information of each region

Store or update weather information to table storage

In Knative, we can directly create services as follows:

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: weather-store namespace: defaultspec: template: metadata: labels: app: weather-store annotations: autoscaling.knative.dev/maxScale: "20" autoscaling.knative.dev/target: "100" spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/weather-store:1.2 Ports:-name: http1 containerPort: 8080 env:-name: OTS_TEST_ENDPOINT value: http://xxx.cn-hangzhou.ots.aliyuncs.com-name: TABLE_NAME value: weather-name: OTS_TEST_INSTANCENAME value: ${xxx}-name: OTS_TEST_KEYID Value: ${yyy}-name: OTS_TEST_SECRET value: ${Pxxx}-name: WEATHER_API_KEY value: xxx

For more information about the specific implementation of the service, see GitHub source code: https://github.com/knative-sample/weather-store

Create a scheduled event

There may be a question here: why not do a timed poll directly in the service, instead of making a timed event trigger execution call through Knative Eventing? So let's make it clear that this is how it should be played in the Serverless era-on demand. Never run these scheduled tasks in the traditional way in the service, dear, this is a continuous waste of computing resources.

To get to the point, let's use the timing task data source (CronJobSource) that comes with Knative Eventing to trigger timing synchronization events.

Create a CronJobSource resource to trigger synchronous Weather Service (weather-store) every 3 hours. The WeatherCronJob.yaml is as follows:

ApiVersion: sources.eventing.knative.dev/v1alpha1kind: CronJobSourcemetadata: name: weather-cronjobspec: schedule: "0 * / 3 *" data:'{"message": "sync"} 'sink: apiVersion: serving.knative.dev/v1alpha1 kind: Service name: weather-store

Execute the command:

Kubectl apply-f WeatherCronJob.yaml

Now when we log in to Aliyun's table storage service, we can see that the weather forecast data has been synchronized according to the format of city and date.

Provide weather forecast query RESTful API

With these weather data, we can provide our own weather forecasting service at will (it feels like we have contracted a piece of land and we will be the landlords). There is no difficulty here. Query the corresponding weather data from the table storage and encapsulate it according to the returned data format.

In Knative, we can deploy the RESTful API service as follows:

ApiVersion: serving.knative.dev/v1alpha1kind: Servicemetadata: name: weather-service namespace: defaultspec: template: metadata: labels: app: weather-service annotations: autoscaling.knative.dev/maxScale: "20" autoscaling.knative.dev/target: "100" spec: containers:-image: registry.cn-hangzhou.aliyuncs.com/knative-sample/weather-service:1.1 Ports:-name: http1 containerPort: 8080 env:-name: OTS_TEST_ENDPOINT value: http://xxx.cn-hangzhou.ots.aliyuncs.com-name: TABLE_NAME value: weather-name: OTS_TEST_INSTANCENAME value: ${xxx}-name: OTS_TEST_KEYID Value: ${yyy}-name: OTS_TEST_SECRET value: ${Pxxx}

Specific implementation source code GitHub address: https://github.com/knative-sample/weather-service

Check the weather RESTful API:

Request URL GET / api/weather/query

Parameter: cityCode: city area code. For example, Beijing area code: 110000date: query date. Such as format: 2019-09-26

Return the result

{"code": 200," message ":"," data ": {" adcode ":" 110000 "," city ":" Beijing "," date ":" 2019-09-26 "," daypower ":" ≤ 3 "," daytemp ":" 30 "," dayweather ":" Qing "," daywind ":" Southeast "," nightpower ":" ≤ 3 " "nighttemp": "15", "nightweather": "Qing", "nightwind": "Southeast", "province": "Beijing", "reporttime": "2019-09-25 14:50:46", "week": "4"}}

Query: Hangzhou, 2019-09-26 Weather Forecast example

Test address: http://weather-service.default.serverless.kuberun.com/api/weather/query?cityCode=330100&date=2019-09-26

The above is how to achieve weather services based on Knative Serverless technology. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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