【API 进阶之路】老板给我涨薪 30%!如何通过 SDK 接口搞定千万级流量直播

发布于: 2020 年 07 月 17 日

摘要:看我如何通过API Explorer 的SDK接口搞定千万级流量直播。



最近几个月,我的变化其实还蛮大的,从一个被实习生“无视”的“前浪”,转变成了不仅能够解决技术问题还能解决业务问题(顺手还能帮实习生解决恋爱问题)的“前辈”。前面几期故事记录了我的高光时刻,有兴趣可以点击前文查看。



公司的短视频项目上线之后一直不温不火,老板挺着急,运营部提出要在6月底组织一次千人直播带货活动,邀请1千个主播同时在短视频平台上开直播,拉用户和流量。



千人同时在线直播,短视频平台的目标访问量在百万级以上,需要技术部门保证高并发流量下服务器的稳定。这个好办,华为云弹性云服务器是可以随时扩容的,经过部门研究,我们给出的技术方案是临时创建100台云虚拟机来支撑这次活动。



这个任务又光荣地落到了我的身上,谁让我是公司公认的云服务器专家呢…可离直播活动落地只有一个星期的时间了,还要留出时间进行压测。“通过API可以批量处理弹性云服务器,但我现在一个个接口封装也来不及啊!而且根据我的经验,还得提前做好随时扩容的准备,能够支持比预估目标更高的流量,得赶紧想想除了API还有什么可以用……”我心里有点着急了。



习惯性的打开华为云官网,在首页发呆了1分钟,一道灵光闪过:操作简单,可以快速控制批量资源,这可不就是SDK的特性吗!说来就来,我很快就有了思路:先快速创建100台按需计费的弹性云服务器作为直播支撑,在直播完成后,再对这些弹性云服务器进行释放。



打开API Explorer(https://apiexplorer.developer.huaweicloud.com/apiexplorer/overview?utm_source=apiwz&utm_medium=read )快速浏览,确认我需要用到的接口是CreatePostPaidServers 、 ListServerDetails 和DeleteServers。



万万没想到,这个设计还真起到了救急的作用!



直播开始前1个小时,运营部门突然反馈,这次活动的推广效果大大超出预期,峰值流量可能是预期目标的10倍!这将给网站访问带来很大的压力,老大给我打电话问我有没有办法处理。



这当然难不住我了,因为提前准备了技术方案,在直播过程中随着访问量的变化,随时批量调整服务器的配置,完美保障了直播的进行,最后在10倍于目标值的访问量下,依然没有出现任何卡顿/延迟的情况。



活动结束后,我又开始了疯狂输出,将SDK的配置方法写在了文档里:



一、前置条件:获取必填参数



1. 华为云SDK的认证方式为AK/SK认证,可以在华为云控制台”我的凭证-访问密钥”页面上创建和查看AK/SK。更多信息请查看访问密钥(https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html)。



2. 准备接口的必填参数



PS:在华为云控制台-镜像服务IMS中可快速获取公共镜像相应的ID



服务器配置详情:



  • 区域:华北-北京一 - cn-north-1

  • 可用区:可用区1 - cn-north-1a

  • 规格:通用计算增强型 - c3.large.2

  • 镜像:Windows Server 2019数据中心版 64 位简体中文 - fb48d5c7-8718-489a-9273-d3e0e09c84d7

  • 服务器名:任意指定 - 如 "live-stream”

  • 系统盘类型:普通 IO 磁盘 – "SATA"

  • 虚拟私有云 ID(VPC ID):可获取当前账号在北京一区域中默认的 VPC ID,若没有默认 VPC 则新建

  • 网卡信息:可获取当前账号在北京一区域中默认 PC / 新建 VPC 下的子网 ID

  • 允许重名:当批量创建弹性云服务器时,云服务器名称是否允许重名,当count大于1的时候该参数生效 - false



二、实战演练



PS:指南:https://github.com/huaweicloud/huaweicloud-sdk-java-v3/blob/master/README_CN.md



1. 新建maven项目,导入SDK的maven依赖



<span style="font-size: 14px;"><!-- add dependencies inpom.xml --><br style=""><dependency><br style=""> <groupId>com.huaweicloud.sdk</groupId><br style=""> <artifactId>huaweicloud-sdk-core</artifactId><br style=""> <version>[3.0.1-beta, 3.1.0-beta)</version><br style=""></dependency><br style=""><dependency><br style=""> <groupId>com.huaweicloud.sdk</groupId><br style=""> <artifactId>huaweicloud-sdk-ecs</artifactId><br style=""> <version>[3.0.1-beta, 3.1.0-beta)</version><br style=""></dependency><br style=""></span>



2. 批量创建弹性云服务器Demo



<span style="font-size: 14px;">import com.huaweicloud.sdk.core.auth.BasicCredentials;<br style="">import com.huaweicloud.sdk.core.http.HttpConfig;<br style="">import com.huaweicloud.sdk.ecs.v2.EcsClient;<br style="">import com.huaweicloud.sdk.ecs.v2.model.*;<br style=""> <br style="">import java.util.LinkedList;<br style="">import java.util.List;<br style=""> <br style="">public class TestCreateEcs {<br style=""> public static void main(String[] args) {<br style=""> String ak = "{your ak string}";<br style=""> String sk = "{your sk string}";<br style=""> String projectId = "{your project id}";<br style=""> String endpoint = "https://ecs.cn-north-1.myhuaweicloud.com";<br style=""> <br style=""> HttpConfig config = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true);<br style=""> <br style=""> BasicCredentials credentials = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);<br style=""> <br style=""> EcsClient ecsClient = EcsClient.newBuilder().withCredential(credentials).withEndpoint(endpoint).withHttpConfig(config).build();<br style=""> <br style=""> // 确认创建虚拟机的必填参数<br style=""> String az = "cn-north-1a";<br style=""> String flavorRef = "s3.medium.2";<br style=""> String imageRef = "fb48d5c7-8718-489a-9273-d3e0e09c84d7";<br style=""> String name = "live-stream";<br style=""> String vpcId = "{you vpc id}";<br style=""> // 网卡信息<br style=""> PostPaidServerNic nic = new PostPaidServerNic().withSubnetId("{your subnet id}");<br style=""> List<PostPaidServerNic> list = new LinkedList<>();<br style=""> list.add(nic);<br style=""> // 系统盘信息<br style=""> PostPaidServerRootVolume root = new PostPaidServerRootVolume().withVolumetype(PostPaidServerRootVolume.VolumetypeEnum.SATA);<br style=""> <br style=""> PostPaidServer servers = new PostPaidServer().withAvailabilityZone(az)<br style=""> .withFlavorRef(flavorRef)<br style=""> .withImageRef(imageRef)<br style=""> .withName(name)<br style=""> .withNics(list)<br style=""> .withRootVolume(root)<br style=""> .withVpcid(vpcId)<br style=""> .withCount(100)<br style=""> .withIsAutoRename(false);<br style=""> <br style=""> CreatePostPaidServersRequestBody body = new CreatePostPaidServersRequestBody().withServer(servers);<br style=""> CreatePostPaidServersRequest request = new CreatePostPaidServersRequest().withBody(body);<br style=""> CreatePostPaidServersResponse response = ecsClient.createPostPaidServers(request);<br style=""> <br style=""> System.out.println(response.toString());<br style=""> }<br style="">}<br style=""></span>



3. 批量删除弹性云服务器Demo



<span style="font-size: 14px;">// Step1: 查询当前以"live-stream"为开头的虚拟机列表<br style="">ListServersDetailsRequest listServersDetailsRequest = new ListServersDetailsRequest().withName("live-stream");<br style="">ListServersDetailsResponse listServersDetailsResponse =<br style=""> ecsClient.listServersDetails(listServersDetailsRequest);<br style=""> <br style="">// Step2: 构造删除请求中的ServerId列表<br style="">List<ServerDetail> serversList = listServersDetailsResponse.getServers();<br style="">List<ServerId> serverIdList = new ArrayList<>();<br style="">for(ServerDetail server : serversList) {<br style=""> ServerId id = new ServerId().withId(server.getId());<br style=""> serverIdList.add(id);<br style="">}<br style=""> <br style="">// Step3: 传入serverId列表,删除虚拟机<br style="">DeleteServersRequestBody deleteServersRequestBody = new DeleteServersRequestBody().withServers(serverIdList);<br style="">DeleteServersRequest deleteServersRequest = new DeleteServersRequest().withBody(deleteServersRequestBody);<br style="">DeleteServersResponse deleteServersResponse = ecsClient.deleteServers(deleteServersRequest);<br style=""></span>



活动取得了非常好的成绩,实现了短视频平台流量和用户增长的目标,但是这次老板居然没有表扬我,我还有些纳闷:难道老板已经习惯了我的优秀?过了几天,HR小姐姐递过来一张表让我签字,表上写着:“调薪申请表,调薪幅度30%”,老板已经签好了名字。



目前API Explorer平台已开放EI企业智能、计算、应用服务、网络、软件开发平台、视频等70+云服务,共上线2000+个API、6000+个错误码。在前期试运行期间,华为云API Explorer平台上的API接口也已被多家企业成功接入。



华为云API Explorer平台在未来几个月会实现更多功能,比如支持SDK示例代码、CLI等特性,同时也会开放更多的云服务API接口,连接更多开发者实现创新、拓宽创新边界。



点击关注,第一时间了解华为云新鲜技术~



发布于: 2020 年 07 月 17 日 阅读数: 64
用户头像

传送门:https://bbs.huaweicloud.com/ 2020.07.14 加入

华为云开发者社区,提供全面深入的云计算前景分析、丰富的技术干货、程序样例,分享华为云前沿资讯动态,方便开发者快速成长与发展,欢迎提问、互动,多方位了解云计算!

评论

发布
暂无评论
【API进阶之路】老板给我涨薪30%!如何通过SDK接口搞定千万级流量直播