全部產品
Search
文件中心

Container Service for Kubernetes:在 Knative 中部署 Agent2Agent (A2A) 協議伺服器

更新時間:Mar 24, 2026

Agent2Agent (A2A) 協議是一種開放標準,旨在實現 AI 智能體(Agent)之間的無縫通訊與協作。通過在 Knative 中部署 A2A 伺服器,能夠利用其自動擴縮容(包括縮容至0)等特性,實現資源按需使用和版本的快速迭代。

工作原理

AI 智能體擁有推理、規劃和記憶能力,能夠自主學習並代表使用者完成任務。類比於 MCP 為 LLM 提供資料訪問標準,Agent2Agent (A2A) 協議為智能體之間的互通性提供了標準化定義。

在 Knative 中部署 A2A 伺服器,主要涉及以下核心互動:

  • 發現(Discovery):服務部署後,通過標準的智能體卡片暴露介面,允許其他智能體查詢其技能 (AgentSkill)和功能(AgentCapabilities)。

  • 通訊(Communication):利用 Knative 的網關和服務路由能力,處理基於 HTTP/gRPC 的標準訊息交換。

  • 協作(Collaboration):智能體通過 API 委派任務並協調行動。

可參見 A2A 規範瞭解 Agent 通訊的協議架構和核心概念

準備工作

  • 已在叢集中部署Knative,請參見部署與管理Knative組件

  • 已擷取訪問網關地址。

    可在Knative的組件管理服務管理頁面擷取。樣本如下。

    image

步驟一:部署 A2A 伺服器

本樣本部署一個名為 helloworld-agent-server 的基礎智能體服務。

  1. 建立a2a-service.yaml

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-agent-server
      # 根據實際情況修改命名空間
      namespace: default 
      annotations:
        # 使用通配網域名稱便於快速驗證
        knative.aliyun.com/serving-ingress: / 
    spec:
      template:
        spec:
          containers:
          # 將 {region} 替換為實際使用的地區,如 cn-hangzhou
          - image: registry-{region}-vpc.ack.aliyuncs.com/acs/knative-samples-a2a:v1.0-952c112
            name: user-container
            env:
            # INVOKE 定義了 Agent Card 中返回的調用 URL,請替換為服務的訪問網關地址
            - name: INVOKE
              value: http://<YOUR_GATEWAY_ADDRESS>/invoke
            ports:
            - containerPort: 9001
              name: http1
              protocol: TCP
  2. 部署服務。

    kubectl apply -f a2a-service.yaml
  3. 查看 Knative Service 狀態。

    kubectl get ksvc helloworld-agent-server

    預期輸出:

    NAME                      URL                                                  LATESTCREATED                   LATESTREADY                     READY   REASON
    helloworld-agent-server   http://helloworld-agent-server.default.example.com   helloworld-agent-server-00001   helloworld-agent-server-00001   True    

步驟二:驗證服務與擷取 Agent Card

部署完成後,需要驗證服務是否能夠正確返回符合 A2A 協議的智能體卡片(Agent Card)。

  1. 擷取訪問網關與服務預設網域名稱。

    1. ACK叢集列表頁面,單擊目的地組群名稱,在叢集詳情頁左側導覽列,選擇應用 > Knative

    2. 服務管理頁面,擷取服務預設網域名稱

      下圖展示了訪問網關預設網域名稱

      image

  2. 訪問服務的中繼資料端點。

    # 將 <GATEWAY_ADDRESS> 替換為實際擷取的網關地址
    curl http://<GATEWAY_ADDRESS>/.well-known/agent-card.json | jq .

    查看預期輸出,
    輸出應包含智能體的能力(capabilities)、描述(description)和技能列表(skills)。

    {
      "capabilities": {
        "streaming": true
      },
      "defaultInputModes": [
        "text"
      ],
      "defaultOutputModes": [
        "text"
      ],
      "description": "Just a hello world agent",
      "name": "Hello World Agent",
      "preferredTransport": "JSONRPC",
      "protocolVersion": "",
      "skills": [
        {
          "description": "Returns a 'Hello, world!'",
          "examples": [
            "hi",
            "hello"
          ],
          "id": "hello_world",
          "name": "Hello, world!",
          "tags": [
            "hello world"
          ]
        }
      ],
      "url": "http://XXX/invoke",
      "version": ""
    }

步驟三:通過 A2A Client 調用服務

使用 Golang 編寫用戶端代碼,類比另一個智能體與部署的 A2A 伺服器進行通訊。

  1. 準備開發環境,安裝 Go 語言環境

  2. 建立 main.go 檔案,寫入以下代碼。

    package main
    
    import (
    	"context"
    	"flag"
    	"log"
    
    	// 引入 A2A 協議相關的核心庫
    	"github.com/a2aproject/a2a-go/a2a"
    	"github.com/a2aproject/a2a-go/a2aclient"
    	"github.com/a2aproject/a2a-go/a2aclient/agentcard"
    
    	// 引入 gRPC 相關的庫
    	"google.golang.org/grpc"
    	"google.golang.org/grpc/credentials/insecure"
    )
    
    // 將 <GATEWAY_ADDRESS> 替換為實際擷取的網關地址
    var cardURL = flag.String("card-url", "http://<GATEWAY_ADDRESS>", "Base URL of AgentCard client.")
    
    func main() {
    	flag.Parse()
    	ctx := context.Background()
    
    	// 服務發現
    	card, err := agentcard.DefaultResolver.Resolve(ctx, *cardURL)
    	if err != nil {
    		log.Fatalf("Failed to resolve an AgentCard: %v", err)
    	}
    
    	// 配置傳輸層
    	withInsecureGRPC := a2aclient.WithGRPCTransport(grpc.WithTransportCredentials(insecure.NewCredentials()))
    
    	// 建立用戶端
    	client, err := a2aclient.NewFromCard(ctx, card, withInsecureGRPC)
    	if err != nil {
    		log.Fatalf("Failed to create a client: %v", err)
    	}
    
    	// 構建訊息
    	msg := a2a.NewMessage(a2a.MessageRoleUser, a2a.TextPart{Text: "Hello, world"})
    	resp, err := client.SendMessage(ctx, &a2a.MessageSendParams{Message: msg})
    	if err != nil {
    		log.Fatalf("Failed to send a message: %v", err)
    	}
    
    	log.Printf("Server responded with: %+v", resp)
    }
  3. 運行代碼進行測試。

    go mod init a2a-demo
    go mod tidy
    go run main.go

    預期輸出如下,表明用戶端已成功串連到Knative 服務,且服務端已經處理完畢並返回了響應。

    2025/11/27 17:24:21 Server responded with: &{ID:019ac4a0-c386-7cdc-9aad-d40fb8f98ae2 ContextID: Extensions:[] Metadata:map[] Parts:[{Text:Hello, world! Metadata:map[]}] ReferenceTasks:[] Role:agent TaskID:}

生產環境使用建議

  • 自訂網域名與 HTTPS:生產環境中請勿直接使用測試網域名稱。建議配置自訂網域名並開啟 HTTPS 認證,以確保 Agent 間通訊的安全性。

  • 冷啟動最佳化:如果智能體調用頻率較低,Knative 會將執行個體縮容至0。為避免首個請求的冷啟動延遲影響互動體驗,可配置最小執行個體數(MinScale)或配置保留執行個體

計費說明

Knative組件本身不產生額外費用。但在使用過程中產生的計算資源(如ECS)、網路資源(如ALB)等費用,由各雲產品收取。請參見雲產品資源費用