摘要:看我如何通過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接口,連接更多開發者實現創新、拓寬創新邊界。