浣跨敤 Serverless + 椋炰功鎵撻€犱綘鐨勪釜鎬у寲娑堟伅鎻愰啋绯荤粺

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浣跨敤 Serverless + 椋炰功鎵撻€犱綘鐨勪釜鎬у寲娑堟伅鎻愰啋绯荤粺相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%8e%a8%e9%80%81' title='鎺ㄩ€?>鎺ㄩ€?/a>   puts   more   href   鏌ョ湅   info   NPU   鏈湴   

涓€銆佸墠瑷€

鍦ㄦ棩甯稿伐浣滃涔犵敓娲讳腑锛屾垜浠彲鑳戒細閬囧埌浠ヤ笅鎯呭舰锛?/p>

  • 鑷繁绠$悊鐨勬煇鍙版湇鍔″櫒瀹曟満浜嗭紝浣嗘槸娌℃湁寰楀埌鍙婃椂鐨勬彁閱掞紝瀵艰嚧涓氬姟鍙楀埌鎹熷け
  • 鏌愪簺鑷繁寰堟兂娉ㄥ唽鐨勭綉绔欐倓鎮勫紑鏀炬敞鍐岋紝浣嗘槸鑷繁骞舵病鏈夊強鏃跺緱鐭ワ紝浜庢槸鍙兘缁х画婕棤鐩殑鐨勭瓑寰?/li>
  • 鈥︹€?/li>

濡傛灉姣忎欢浜嬮兘鑺辨椂闂村幓鍏虫敞锛岄偅鎴戜滑鐨勬椂闂村繀鐒朵細涓嶅鐢紝閭f湁娌℃湁浠€涔堝姙娉曞彲浠ヨ杩欎簺娑堟伅闆嗕腑璧锋潵骞朵笖鍙婃椂鎺ㄩ€佸憿锛熷湪杩欓噷鎴戞兂鍚戝ぇ瀹舵帹鑽愪竴涓В鍐虫柟妗堬紝閭e氨鏄?strong>浣跨敤 Serverless + 椋炰功鎵撻€犲睘浜庤嚜宸辩殑涓€у寲娑堟伅鎻愰啋绯荤粺銆?/p>

椋炰功缃戦〉鐗?/a>鐧诲綍

  • 鎵撳紑椋炰功寮€鏀惧钩鍙?/a>锛岀偣鍑?strong>鍒涘缓浼佷笟鑷缓搴旂敤锛屽苟杈撳叆搴旂敤鍚嶇О鍜?strong>搴旂敤鍓爣棰?/strong>锛岀劧鍚庣偣鍑?strong>纭畾鍒涘缓

  • 鎶€鏈浘鐗? src=

    鎶€鏈浘鐗? src=

    1. 鍦ㄤ紒涓氳嚜寤哄簲鐢ㄥ垪琛ㄤ腑鐐瑰嚮鍒氬垰鍒涘缓鎴愬姛鐨勫簲鐢紝骞惰褰?App ID 鍜?App Secret

    鎶€鏈浘鐗? src=

    .env

    TENCENT_SECRET_ID=AKID********************************
    TENCENT_SECRET_KEY=********************************
    

    娉細杩欓噷鐨?TENCENT_SECRET_ID 鍜?TENCENT_SECRET_KEY 鍙湪鑵捐浜戞帶鍒跺彴鐨?a href="https://console.cloud.tencent.com/cam/capi">璁块棶瀵嗛挜涓幏鍙栵紝濡傛灉娌℃湁瀵嗛挜鍒欓渶瑕佽嚜宸辨柊寤轰竴涓?/p>

    serverless.yml

    myFunction:
      component: "@serverless/tencent-scf"
      inputs:
        name: feishu-notify-py
        codeUri: "./"
        handler: index.main_handler
        runtime: Python3.6
        region: ap-guangzhou
        description: My Serverless Function Used to Notify Myself
        memorySize: 128
        events:
        - apigw:
            name: serverless
            parameters:
              protocols:
              - https
              endpoints:
              - path: "/"
                method: POST
    

    娉細鍙互鐐瑰嚮杩欓噷鏌ョ湅serverless.yml涓墍鏈夊彲鐢ㄥ睘鎬х殑灞炴€у垪琛?/p>

    index.py

    def main_handler(event, context):
        import requests
        import json
        print(event)
        CONFIG = {
            "app_id": "********************",
            "app_secret": "********************************"
        }
        # my auth
        if 鈥榤yauth鈥?not in event[鈥榪ueryString鈥榏 or event[鈥榪ueryString鈥榏[鈥榤yauth鈥榏 != 鈥榝eishu1鈥?
            return 鈥榝orbidden鈥?    # Get content
        postContent = event[鈥榖ody鈥榏
        try:
            postContent = json.loads(postContent)
        except:
            return 鈥榚rror in json_loads(line: 19)鈥?    if 鈥榗ontent鈥?not in postContent:
            return 鈥榠nvalid params鈥?    content = postContent[鈥榗ontent鈥榏
        # Get tenant_access_token
        try:
            res = requests.post(鈥榟ttps://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/鈥? {
                "app_id": CONFIG[鈥榓pp_id鈥榏,
                "app_secret": CONFIG[鈥榓pp_secret鈥榏
            })
        except:
            return 鈥榚rror in get_tenant_access_token鈥?    data = json.loads(res.text)
        if data[鈥榗ode鈥榏 != 0:
            return data[鈥榤sg鈥榏
        token = data[鈥榯enant_access_token鈥榏
        # Get chat_id
        try:
            res = requests.get(鈥榟ttps://open.feishu.cn/open-apis/chat/v4/list鈥? headers={
                鈥楢uthorization鈥? 鈥楤earer %s鈥?% (token)
            })
        except:
            return 鈥榚rror in get_chat_id鈥?    data = json.loads(res.text)
        if data[鈥榗ode鈥榏 != 0:
            return data[鈥榤sg鈥榏
        groupList = data[鈥榙ata鈥榏[鈥榞roups鈥榏
        myGroupId = groupList[0][鈥榗hat_id鈥榏
        # Send message
        try:
            res = requests.post(鈥榟ttps://open.feishu.cn/open-apis/message/v4/send/鈥? json={
                "chat_id": myGroupId,
                "msg_type": "text",
                "content": {
                    "text": content
                }
            }, headers={
                鈥楢uthorization鈥? 鈥楤earer %s鈥?% (token),
                鈥楥ontent-Type鈥? 鈥榓pplication/json鈥?        })
        except:
            return 鈥榚rror in send message鈥?    data = json.loads(res.text)
        if data[鈥榗ode鈥榏 != 0:
            return data[鈥榤sg鈥榏
        return 鈥榮uccess鈥?

    鍏充簬 index.py锛岃繖閲屾湁鍑犵偣闇€瑕佷綔鍑鸿鏄庯細

    1. 浠g爜涓殑 app_id 鍜?app_secret 椤归渶濉啓鍦ㄥ噯澶囧伐浣滆褰曠殑 App ID 鍜?App Secret
    2. 鏈€缁堟垜浠娇鐢?POST 鏂规硶鍙戦€佹秷鎭?/li>
    3. 鍦ㄨ皟鐢ㄦ椂锛屾垜浠繕闇€瑕佸湪 query 澶勫姞涓??myauth=feishu1锛岀洰鐨勬槸浣滅畝鍗曢獙璇佷互闃叉浠栦汉鍙戦€侊紝渚嬪 https://service-********-**********.**.apigw.tencentcs.com/release/?myauth=feishu1

    涓夈€侀儴缃?Serverless 鏈嶅姟

    1. 閫氳繃 npm 瀹夎 Serverless
    $ npm install -g serverless
    
    1. 閫氳繃serverless鍛戒护杩涜閮ㄧ讲锛屽苟娣诲姞--debug鍙傛暟鏌ョ湅閮ㄧ讲杩囩▼涓殑淇℃伅
    $ serverless --debug
    
    1. 浠庣粓绔幏鍙?API 缃戝叧鐨?URL

    鎶€鏈浘鐗? src=

    鑾峰彇 API 缃戝叧鐨?URL

    椋炰功寮€鏀惧钩鍙?/a>锛屽湪浼佷笟鑷缓搴旂敤鍒楄〃涓偣鍑诲垰鍒氬垱寤烘垚鍔熺殑搴旂敤

  • 鐐瑰嚮搴旂敤鍔熻兘-鏈哄櫒浜?/strong>锛岀偣鍑?strong>鍚敤鏈哄櫒浜?/strong>

  • 鎶€鏈浘鐗? src=

    1. 鐐瑰嚮鐗堟湰绠$悊涓庡彂甯?/strong>-鍒涘缓鐗堟湰锛屽弬鑰冧笅鍥捐繘琛岄厤缃紙鍏堜笉瑕佺偣淇濆瓨锛?/li>

    鎶€鏈浘鐗? src=

    1. 鍦?strong>鍙敤鎬х姸鎬?/strong>澶勭偣鍑?strong>缂栬緫锛岄€夋嫨鎵€鏈夊憳宸ワ紝鐒跺悗鐐瑰嚮淇濆瓨

    鎶€鏈浘鐗? src=

    1. 鐐瑰嚮鐢宠鍙戝竷

    鎶€鏈浘鐗? src=

    1. 鐐瑰嚮椋炰功缃戦〉鐗堢殑澶村儚锛岃繘鍏ラ涔︾鐞嗗悗鍙?/li>

    鎶€鏈浘鐗? src=

    1. 鐐瑰嚮宸ヤ綔鍙?/strong>-搴旂敤瀹℃牳锛岀劧鍚庣偣鍑?strong>瀹℃牳

    鎶€鏈浘鐗? src=

    1. 鐐瑰嚮閫氳繃

    鍙傛暟 绫诲瀷 蹇呭~/閫夊~ 璇存槑 榛樿鍊?/th> 瀹炰緥 Content-Type string 蹇呭~ Content-Type application/json

    璇锋眰 Query锛?/strong>

    鍙傛暟 绫诲瀷 蹇呭~/閫夊~ 璇存槑 榛樿鍊?/th> 瀹炰緥
    myauth string 蹇呭~ 绠€鍗曢獙璇?/td> feishu1

    璇锋眰 Body锛?/strong>

    {
        "content": "杩欓噷濉叆浣犳兂瑕佸彂閫佺殑淇℃伅"
    }
    

    鍙互鐪嬪埌锛岄涔︾殑娑堟伅鎺ㄩ€佸緢鍙婃椂

    鎶€鏈浘鐗? src=

    One More Thing

    3 绉掍綘鑳藉仛浠€涔堬紵鍠濅竴鍙f按锛岀湅涓€灏侀偖浠讹紝杩樻槸 鈥斺€?閮ㄧ讲涓€涓畬鏁寸殑 Serverless 搴旂敤锛?/p>

    澶嶅埗閾炬帴鑷?PC 娴忚鍣ㄨ闂細https://serverless.cloud.tencent.com/deploy/express

    3 绉掓瀬閫熼儴缃诧紝绔嬪嵆浣撻獙鍙蹭笂鏈€蹇殑 Serverless HTTP 瀹炴垬寮€鍙戯紒

    浼犻€侀棬锛?/strong>

    娆㈣繋璁块棶锛?a href="https://serverlesscloud.cn/">Serverless 涓枃缃?/a>锛屾偍鍙互鍦?鏈€浣冲疄璺?/a> 閲屼綋楠屾洿澶氬叧浜?Serverless 搴旂敤鐨勫紑鍙戯紒


    鎺ㄨ崘闃呰锛?a href="https://union-click.jd.com/jdc?e=jdext-1235945425166544896-0&p=AyIGZRNeEAAbAF0aXyUFFg5VG10RAxUAUisfSlpMWGVCHlBDUAxLBQNQVk4YDk5ER1xOGVIfUhUCFANUHFwSHUtCCUZrEkdsezZSJhVgRn0cGhxzX2FdJRldZQ4eN1USWxcKEwZVHFglAhMGVR9eFgEUBmUrWxAyU2lSH1scAxU3VCtbEQsUAlEYWREBEgNXK1sdBiLR%2B4%2BOnb3Lt%2FDN8bvXn7eAkvDBvJQ3ZStYJVlHUxxeRxUBEANdHl8QARYHUhxfEAcbD1IHWiUCEwZVElMRAhMFOxo1FQoSBFAaWxMEFjdVK1slXVZaCCtZFAMQBQ%3D%3D">銆奡erverless 鏋舵瀯锛氫粠鍘熺悊銆佽璁″埌椤圭洰瀹炴垬銆?/a>

    以上是关于浣跨敤 Serverless + 椋炰功鎵撻€犱綘鐨勪釜鎬у寲娑堟伅鎻愰啋绯荤粺的主要内容,如果未能解决你的问题,请参考以下文章

    MySQL涔嬪垵浣跨敤

    濡備綍鎻愰珮绋嬪簭鍛樼殑閿洏浣跨敤鐜囷紵

    window10Cmd浣跨敤

    SpringCloudConfig浣跨敤

    vue axios浣跨敤

    mac浣跨敤

    (c)2006-2024 SYSTEM All Rights Reserved IT常识