国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

grpc-go通過context傳遞額外數(shù)據(jù)

這篇具有很好參考價值的文章主要介紹了grpc-go通過context傳遞額外數(shù)據(jù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

  • 使用 ctx.Value 從 context 讀取數(shù)據(jù)
// ValueFromIncomingContext returns the metadata value corresponding to the metadata
// key from the incoming metadata if it exists. Key must be lower-case.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ValueFromIncomingContext(ctx context.Context, key string) []string {
	md, ok := ctx.Value(mdIncomingKey{}).(MD)
	if !ok {
		return nil
	}

	if v, ok := md[key]; ok {
		return copyOf(v)
	}
	for k, v := range md {
		// We need to manually convert all keys to lower case, because MD is a
		// map, and there's no guarantee that the MD attached to the context is
		// created using our helper functions.
		if strings.ToLower(k) == key {
			return copyOf(v)
		}
	}
	return nil
}
  • 使用 ctx.Value 往 context 寫入數(shù)據(jù)
// AppendToOutgoingContext returns a new context with the provided kv merged
// with any existing metadata in the context. Please refer to the documentation
// of Pairs for a description of kv.
func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context {
	if len(kv)%2 == 1 {
		panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
	}
	md, _ := ctx.Value(mdOutgoingKey{}).(rawMD)
	added := make([][]string, len(md.added)+1)
	copy(added, md.added)
	kvCopy := make([]string, 0, len(kv))
	for i := 0; i < len(kv); i += 2 {
		kvCopy = append(kvCopy, strings.ToLower(kv[i]), kv[i+1])
	}
	added[len(added)-1] = kvCopy
	return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md.md, added: added})
}

metadata 是 grpc 內(nèi)置的,用來往 RPC 服務(wù)傳遞 http 頭數(shù)據(jù),分 in 和 out 兩種,對應(yīng)的 key 都為一個空 struct,分別為:mdIncomingKey 和 mdOutgoingKey 。

服務(wù)端的 ctx 和 md 直接打印出來,如下樣子:

fmt.Println(ctx)
fmt.Println(md)

context.Background.WithValue(type transport.connectionKey, val <not Stringer>).WithValue(type peer.peerKey, val <not Stringer>).WithDeadline(2024-02-19 10:02:43.212614653 +0800 CST m=+41018.106555206 [1.999790196s]).WithValue(type metadata.mdIncomingKey, val <not Stringer>).WithValue(type grpc.streamKey, val <not Stringer>).WithValue(type baggage.baggageContextKeyType, val <not Stringer>).WithValue(type trace.traceContextKeyType, val <not Stringer>).WithValue(type trace.traceContextKeyType, val <not Stringer>).WithCancel

map[:authority:[add.rpc] append:[append-value] content-type:[application/grpc] extra:[extra-value] grpc-accept-encoding:[gzip] noncestr:[abc] signature:[0123456789] timestamp:[2021-07-01 00:00:00] traceparent:[00-89415f99d44e6f8f6e14e3fe8f13ad20-bf33b29c4362ca6a-00] user-agent:[grpc-go/1.59.0]]
signature: [0123456789]

注意 md 中的值會被加上中括號“[]”。文章來源地址http://www.zghlxwxcb.cn/news/detail-831814.html

到了這里,關(guān)于grpc-go通過context傳遞額外數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • go-zero數(shù)據(jù)庫連接池 database/sql 源碼學(xué)習(xí)

    database/sql 中接口的層級關(guān)系 https://draveness.me/golang/docs/part4-advanced/ch09-stdlib/golang-database-sql/ database/sql源碼地址 : https://github.com/golang/go/tree/release-branch.go1.17/src/database/sql go-zero數(shù)據(jù)庫連接池源碼地址 https://github.com/zeromicro/go-zero/blob/master/core/stores/sqlx/sqlmanager.go 滑動驗證頁面 go

    2024年02月06日
    瀏覽(18)
  • 【go-zero】go-zero分布式鎖實戰(zhàn) | apifox測試go-zero分布式鎖方式

    包地址:github.com/zeromicro/go-zero/core/stores/redis 使用場景: 為了防止并發(fā)的下載相同的excel 我們通過redis鎖來控制請求相同的excel下載 個人思路: req為API傳入的請求參數(shù) 然后加密成md5的字符串,這樣可以處理 相同的請求

    2024年02月15日
    瀏覽(18)
  • qt解決信號和槽連接時傳遞額外參數(shù)的問題

    QSignalMapper 是 Qt 框架中的一個類,用于解決信號和槽連接時傳遞額外參數(shù)的問題。當(dāng)一個信號被觸發(fā)時,QSignalMapper 可以將該信號與一個特定的參數(shù)關(guān)聯(lián)起來,并將信號與對應(yīng)的槽函數(shù)進(jìn)行連接。 下面是關(guān)于 QSignalMapper 的一些詳細(xì)解釋: 1.作用: QSignalMapper 類的主要作用是在一

    2024年02月07日
    瀏覽(23)
  • 【go-zero】docker鏡像直接部署go-zero的API與RPC服務(wù) 如何實現(xiàn)注冊發(fā)現(xiàn)?docker network 實現(xiàn) go-zero 注冊發(fā)現(xiàn)

    【go-zero】docker鏡像直接部署go-zero的API與RPC服務(wù) 如何實現(xiàn)注冊發(fā)現(xiàn)?docker network 實現(xiàn) go-zero 注冊發(fā)現(xiàn)

    使用docker直接部署go-zero微服務(wù)會發(fā)現(xiàn)API無法找到RPC服務(wù) 用docker直接部署 我們會發(fā)現(xiàn)API無法注冊發(fā)現(xiàn)RPC服務(wù) 原因是我們?nèi)鄙倭薲ocker的network網(wǎng)橋 RPC服務(wù)運(yùn)行正常 API服務(wù)啟動,通過docker logs 查看日志還是未發(fā)現(xiàn)RPC API的yaml配置 RPC服務(wù)的IP是 127.0.0.1 與對應(yīng)的端口 下圖為改成了定

    2024年02月13日
    瀏覽(28)
  • 【go-zero】go-zero阿里云oss 前端上傳文件到go-zero API服務(wù) 并在k8s pod中創(chuàng)建文件 并推送到阿里云oss 最佳實踐

    問題:在本地通過上傳文件,然后將文件推送到aliyun的oss中,是沒問題的 但是部署到了k8s中,則出現(xiàn)了問題,一直報錯沒有創(chuàng)建的權(quán)限 思路:開始認(rèn)為應(yīng)該將該文件掛載到configmap中,然后通過這種方式修改了deployment和dockerfile。最終發(fā)現(xiàn)應(yīng)該是go的創(chuàng)建文件路徑方式搞錯了,

    2024年02月13日
    瀏覽(29)
  • React 18 使用 Context 深層傳遞參數(shù)

    React 18 使用 Context 深層傳遞參數(shù)

    參考文章 通常來說,會通過 props 將信息從父組件傳遞到子組件。但是,如果必須通過許多中間組件向下傳遞 props,或是在應(yīng)用中的許多組件需要相同的信息,傳遞 props 會變的十分冗長和不便。 Context 允許父組件向其下層無論多深的任何組件提供信息,而無需通過 props 顯式傳

    2024年02月09日
    瀏覽(26)
  • go-zero系列:接入Prometheus

    go-zero系列:接入Prometheus

    參考文檔:https://zhuanlan.zhihu.com/p/463418864 https://prometheus.io/download/ 進(jìn)入下載文件夾,比如prometheus-2.44.0.windows-amd64。 然后雙擊Prometheus.exe啟動軟件。 啟動后,可以訪問 http://127.0.0.1:9090/。就能查看Prometheus后臺。 然后重啟go-zero項目,能看到輸出日志:Starting prometheus agent at 0.0.

    2024年02月16日
    瀏覽(19)
  • go-zero學(xué)習(xí) 第一章 基礎(chǔ)

    go-zero學(xué)習(xí) 第一章 基礎(chǔ)

    因官網(wǎng)重新改版,本文是基于官網(wǎng)最新版本的文檔并整合舊文檔重新進(jìn)行全面總結(jié)、歸納。 本文主要對官網(wǎng) 快速開始 進(jìn)行提煉總結(jié),未涉及部分將在后續(xù)章節(jié)陸續(xù)補(bǔ)充完善。 go-zero 的 goctl 工具下載 驗證 goctl 的安裝結(jié)果: goctl 一鍵安裝 protoc 、 protoc-gen-go 、 protoc-gen-go-grp

    2024年02月09日
    瀏覽(21)
  • go-zero的服務(wù)發(fā)現(xiàn)源碼閱讀

    go-zero的服務(wù)發(fā)現(xiàn)源碼閱讀

    服務(wù)發(fā)現(xiàn)原理與grpc源碼解析_wangxiaoangg的博客-CSDN博客 ? go-zero rpc demo官方文檔:rpc編寫與調(diào)用 | go-zero 目錄 一 服務(wù)注冊 1.?創(chuàng)建rpc服務(wù) 2. 啟動rpc服務(wù) 3.?registerEtcd做了什么 4.?discov.NewPublisher 服務(wù)發(fā)布者 二 服務(wù)發(fā)現(xiàn) 1.定義注冊resolver 2.解析etcd地址創(chuàng)建鏈接 3.update方法 在看rp

    2024年02月06日
    瀏覽(16)
  • go-zero 開發(fā)之安裝 etcd

    本文只涉及 Linux 上的安裝。 二進(jìn)制安裝 下載二進(jìn)制安裝包 下載地址示例: 解壓二進(jìn)制安裝包 刪除二進(jìn)制安裝包 版本檢查 啟動 etcd 往 etcd 寫讀數(shù)據(jù) Docker 安裝 etcd 主要使用 Google 容器注冊表(gcr.io)下的 gcr.io/etcd-development/etcd 倉庫來存儲其容器鏡像。作為次要選項,它還使

    2024年02月04日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包