共计 5155 个字符,预计需要花费 13 分钟才能阅读完成。
[
](https://developer.aliyun.com/…)
一、Serverless 函数计算
什么是 Serverless?
在《Serverless Architectures》中对 Serverless 是这样子定义的:
Serverless was first used to describe applications that significantly or fully incorporate third-party, cloud-hosted applications and services, to manage server-side logic and state. These are typically“rich client”applications—think single-page web apps, or mobile apps—that use the vast ecosystem of cloud-accessible databases (e.g., Parse, Firebase), authentication services(e.g., Auth0, AWS Cognito), and so on. These types of services have been previously described as“(Mobile) Backend as a service”, and I use“BaaS”as shorthand in the rest of this article. Serverless can also mean applications where server-side logic is still written by the application developer, but, unlike traditional architectures, it’s run in stateless compute containers that are event-triggered, ephemeral (may only last for one invocation), and fully managed by a third party. One way to think of this is“Functions as a Service”or“FaaS”.(Note: The original source for this name—a tweet by @marak—isno longer publicly available.) AWS Lambda is one of the most popular implementations of a Functions-as-a-Service platform at present, but there are many others, too.
这样的形容我置信有很多小伙伴不明确,咱们能够这样子来了解 Serverless:
它的中文直译就是【无服务器】
目前对于 Serverless 有几种解读办法:
- 在某些场景能够解读为一种软件系统架构办法,通常称为 Serverless 架构
- 而在另一些状况下,又能够代表一种产品状态,称为 Serverless 产品
能够了解为 Severless=FAAS+BAAS 即函数即服务 (Function as a Service)+ 后端即服务 (Backend as a Service)
阿里云函数计算
阿里云函数计算是事件驱动的全托管计算服务。应用函数计算,您无需洽购与治理服务器等基础设施,只需编写并上传代码。函数计算为您筹备好计算资源,弹性地、牢靠地运行工作,并提供日志查问、性能监控和报警等性能。
借助函数计算,您能够疾速构建任何类型的利用和服务,并且只需为工作理论耗费的资源付费。
阿里云也为开发者敌人们提供了每月 收费额度!
二、成绩介绍
疫情数据统计推送基于 Python 和阿里云 Serverless 函数计算开发。实现了应用 Python 爬取取得疫情数据并进行整顿,应用函数计算配合定时触发器,每天定时推送全国疫情数据到企业微信。
三、背景意义
疫情防控常态化,在寰球疫情一直减速蔓延态势下在短期内齐全完结是不可能的,很有可能较长期间处于疫情防控的状态,这要求咱们时刻保持警惕,及时理解疫情状况。疫情数据统计推送我的项目,适应了此背景。企业员工每天关上手机微信就能够收到一条简洁的推送,理解当日的疫情状况。
四、劣势和有余
劣势:绝对各大媒体每日推送的疫情状况相比,此疫情数据统计推送更加简介,能够更快的获取到无效信息。应用了阿里云函数 FC 开发,保护不便,无需关注服务器等基础设施,能够依据企业微信推送的需求量主动扩缩容,而且老本极低。应用定时触发器,每天定时的触发程序,发送数据推送,无需人为干涉。
有余:文字枯燥,将在前期推出数据可视化版本。
五、作品展现
我的项目代码:
import requests,random,json
url = "https://c.m.163.com/ug/api/wuhan/app/data/list-total"
def UserAgent(): #随机获取申请头
user_agent_list = ['Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36',
'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36',
'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36']
UserAgent={'User-Agent': random.choice(user_agent_list)}
return UserAgent
def Get(arg1,arg2): #获取疫情
url_json = requests.get(url=url,headers=UserAgent()).json()
today_confirm = str(url_json['data']['chinaTotal']['today']['confirm'])# 全国累计确诊较昨日新增
today_input =str(url_json['data']['chinaTotal']['today']['input'])# 全国较昨日新增境外输出
today_storeConfirm = str(url_json['data']['chinaTotal']['today']['storeConfirm'])# 全国现有确诊较昨日
today_dead =str(url_json['data']['chinaTotal']['today']['dead'])# 累计死亡较昨日新增
today_heal = str(url_json['data']['chinaTotal']['today']['heal'])# 累计治愈较昨日新增
today_incrNoSymptom = str(url_json['data']['chinaTotal']['extData']['incrNoSymptom'])# 无症状感染者较昨日
total_confirm = str(url_json['data']['chinaTotal']['total']['confirm']) # 全国累计确诊
total_input = str(url_json['data']['chinaTotal']['total']['input']) # 境外输出
total_dead = str(url_json['data']['chinaTotal']['total']['dead']) # 累计死亡
total_heal = str(url_json['data']['chinaTotal']['total']['heal']) # 累计治愈
total_storeConfirm = str(url_json['data']['chinaTotal']['total']['confirm'] - url_json['data']['chinaTotal']['total']['dead'] - url_json['data']['chinaTotal']['total']['heal']) # 全国现有确诊
total_noSymptom = str(url_json['data']['chinaTotal']['extData']['noSymptom'])# 无症状感染者
lastUpdateTime = url_json['data']['lastUpdateTime']# 截止工夫
data ='-' * 6 +'全国疫情数据实时统计' + '-' * 5 + '\n 统计截至工夫:'+ lastUpdateTime +'\n' + '-' * 27 + '\n' + \
'累计确诊:' + total_confirm + ';' + '较昨日:' + today_confirm + \
'\n 现有确诊:' + total_storeConfirm + ';' + '较昨日:' + today_storeConfirm + \
'\n 累计死亡:' + total_dead + ';' + '较昨日:' + today_dead + \
'\n 累计治愈:' + total_heal + ';' + '较昨日:' + today_heal + \
'\n 境外输出:' + total_input + ';' + '较昨日:' + today_input + \
'\n 无症状感染者:' + total_noSymptom + ';' + '较昨日:' + today_incrNoSymptom
print(data)
HtmlPuch_server(data)
def HtmlPuch_server(data):
url_wx = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3b4bd7fa-4063-477f-bbc6-0fe767c52fdf"
headers = {"Content-Type": "text/plain"}
push_data ={
"msgtype": "text",
"text": {"content":data}
}
html = requests.post(url_wx,headers=headers,json=push_data)
print(html.text)
应用阿里云函数计算 FC 服务:
应用定时触发器:
最终成果:
六、总结
通过 Serverless 咱们不再须要关注务器等基础设施,只需编写并上传代码,只有为工作理论耗费的资源付费,每月的收费额度能够满足开发者的根本应用。当初函数计算 FC 为开发者提供一站式 Serverless 利用治理,从一键创立利用到疾速体验。
更多内容关注 Serverless 微信公众号(ID:serverlessdevs),会集 Serverless 技术最全内容,定期举办 Serverless 流动、直播,用户最佳实际。