https://www.youtube.com/watch?v=MYmPY1E17ZM&t=7s https://pjchender.dev/golang/grpc-getting-started/#grpc-%E7%9A%84%E5%9B%9B%E7%A8%AE%E9%A1%9E%E5%9E%8B https://github.com/jincheng9/go-tutorial/tree/main/workspace/rpc/02 https://zhuanlan.zhihu.com/p/436041011 ## interceptor https://pandaychen.github.io/2020/02/25/GRPC-INTERCEPTOR-ALLINONE/ ## server 如何用遮罩跟 https://github.com/lubanproj/grpc-read/blob/master/8-grpc%20%E8%AE%A4%E8%AF%81%E9%89%B4%E6%9D%83%E2%80%94%E2%80%94oauth2%E8%AE%A4%E8%AF%81.md ## fieldmask https://loesspie.com/2021/12/24/protobuf-fieldmask/ ## grpc balance 对于高流量、信任客户端的场景,推荐使用客户端侧负载均衡,可以结合ZooKeeper、Etcd、Consul或Eureka使用。 对于需要服务器与客户端之间有信任边界的传统设置,推荐使用代理负载均衡,可以使用GCLB、haproxy或Envoy等。 对于微服务架构,特别是性能要求极高的场景(低延迟、高流量),且客户端可能不被信任,推荐使用旁路负载均衡。 https://colobu.com/2017/03/25/grpc-naming-and-load-balance/ https://www.lixueduan.com/posts/grpc/12-client-side-loadbalance/ https://pandaychen.github.io/2019/11/22/GRPC-BALANCER-DEEP-ANALYSIS/ https://pandaychen.github.io/2019/07/11/GRPC-SERVICE-DISCOVERY/ https://jerryzhou343.github.io/post/grpc_client_balancer/ 長連線處裡 https://pandaychen.github.io/2020/09/01/GRPC-CLIENT-CONN-LASTING/ grpc + k8s https://william-yeh.net/post/2020/03/grpc-load-balancing/ https://www.cnblogs.com/sunsky303/p/11119300.html ## picker https://pandaychen.github.io/2020/01/03/GRPC-PICKER-DEEP-ANALYSIS/ ## Prometheus https://pandaychen.github.io/2020/05/01/GRPC-PROMETHEUS-APP1/ ## go-grpc-middleware https://pandaychen.github.io/2020/02/25/GRPC-INTERCEPTOR-ALLINONE/ ## Metadata https://pandaychen.github.io/2020/02/04/GRPC-IMPROVEMENT/ ## Resolver https://pandaychen.github.io/2020/01/03/GRPC-RESOLVER-DEEP-ANALYSIS2-IN-KRATOS/ ## keepalive https://pandaychen.github.io/2020/09/01/GRPC-CLIENT-CONN-LASTING/ GRPC 断线重连可以通过以下方法实现: 设置通道选项:使用 "grpc.WithBackoffMaxDelay" 和 "grpc.WithBackoffMaxTimeout" 通道选项可以控制重试间隔和超时时间。 客户端重试:在客户端代码中,对调用失败的请求进行重试。 服务端心跳:客户端定期发送心跳请求,服务端检测到心跳请求的丢失,可以关闭连接。 ![image](https://hackmd.io/_uploads/Bk3Idbro6.png) ## grpc pool https://pandaychen.github.io/2020/10/03/DO-WE-NEED-GRPC-CLIENT-POOL/ ### k8s https://william-yeh.net/post/2020/03/grpc-load-balancing/ ## 健康检查 https://github.com/grpc/grpc-go/blob/master/examples/features/health/README.md health應該是客戶端要確認而已 server這邊起不來就掛了 不用確認 他只要set到對應的etcd就好 client那邊如果連不到就會跑backoff 去re connection 那如果單一個rpc (steam)有問題才會跑retrypolicy ## OpenTracing https://pandaychen.github.io/2020/06/01/GOLANG-TRACING-WITH-ZIPKIN/ ## pool https://pandaychen.github.io/2020/10/03/DO-WE-NEED-GRPC-CLIENT-POOL/ ## grpc-go https://juejin.cn/post/7230242762951983159 https://juejin.cn/post/7232483262979719205 https://zhuanlan.zhihu.com/p/623998314 ## retry ![image](https://hackmd.io/_uploads/BJ41M1Uca.png) 通道狀態主要關於客戶端與服務器之間的連接狀態。 RPC錯誤碼則是關於單個RPC調用的成功或失敗的具體原因 ![image](https://hackmd.io/_uploads/H1HDfy85a.png) ![image](https://hackmd.io/_uploads/rkJJSyUc6.png) 客户端角色:在gRPC架构中,客户端负责发起RPC调用,处理可能的网络问题,以及实现复杂的重试逻辑。WaitForReady和WithBackoff正是为了增强客户端在处理这些问题时的能力和灵活性。 服务端角色:而服务端的角色通常集中在处理接收到的RPC请求,执行业务逻辑,以及返回响应。服务端通常不直接涉及调用的重试逻辑,这部分由客户端负责。 服務跟server都要對應的timeout ![image](https://hackmd.io/_uploads/S1zRIy856.png) ### timeout ![image](https://hackmd.io/_uploads/r1eG9J89a.png) ![image](https://hackmd.io/_uploads/H1pXq185a.png) ## error https://medium.com/onedegree-tech-blog/%E8%AE%93%E9%8C%AF%E8%AA%A4%E6%88%90%E7%82%BA%E8%B3%87%E6%BA%90-grpc-%E7%9A%84%E9%8C%AF%E8%AA%A4%E8%99%95%E7%90%86%E6%A8%A1%E5%9E%8B-52e743e258bc ## proto特殊寫法 customer option 欄位可以空, 字不要太長 限制1~10之類的 比較算是自定義 ![](https://hackmd.io/_uploads/ryVGwqNih.png) import descriptor 這個套件 ![](https://hackmd.io/_uploads/SJ8Uv54jh.png) 為什麼是五萬 是因為官方說 5萬到九萬九千九百九是給各公司用的 所以自定義的最好放這區間 ![](https://hackmd.io/_uploads/SJyuic4j3.png) 另外自己常用的最好1~15原因好像是byte 的長度問題 二進位 ## enum ![](https://hackmd.io/_uploads/rkfC6o4in.png) ## grpc vs websocket https://www.infoq.com/articles/websocket-and-http2-coexist/ ## Protocol Buffers & gRPC gRPC 擴充 Protocol Buffers 的語法,因此使用 gRPC 時必須先安裝 Protocol Buffers, 可以先至 protobuf 的 Github 下載 執行檔( e.g. protoc-3.11.3-osx-x86_64.zip ) 進行安裝,安裝方式詳見該檔案夾內的 readme.txt ,在此不多贅述。 記住grpc沒有router 因為她是call自己的function ## protocol buffers ## 工具 vs安裝vscode-proto3 安裝cli 打api的 用BloomRPC(buffers 裡面的posman) 每個欄位都要給123456, 因為她會編譯成二進檔案 所以要唯一 ## 四種方式 ![](https://i.imgur.com/uvXSDmR.png) ## grpc vs api ![](https://i.imgur.com/ovCijbz.png) ## compiler ![](https://i.imgur.com/ejuIOm5.png) 編譯 可看官網 ``` protoc --proto_path=proto --go_out=./pb --go_opt=paths=source_relative \ --go-grpc_out=./pb --go-grpc_opt=paths=source_relative \ proto/*.proto ``` ## 編譯 Protocol Buffer 檔案 `$ protoc *.proto --go_out=plugins=grpc:. --go_opt=paths=source_relative` 在成功編譯好後,應該會看到同樣的資料夾位置出現 *.pb.go 的檔案,這就是編譯好後可以在 Golang 中使用 Protocol Buffer 和 gRPC 的檔案。. ![](https://i.imgur.com/SEwTi16.png) ## protoc flow https://kennychen-blog.herokuapp.com/2022/07/09/Protobuf-%E5%A6%82%E4%BD%95%E9%96%8B%E7%99%BC-codegen-plugin/ ## import 就是import package 但是像是時間 就要 go get github.com/protocolbuffers/protobuf 另外extion 那邊也要加入倒 setting josn那邊 ![](https://i.imgur.com/8Od1M4j.png) 不然他不會知道去哪找 ## gateway ![](https://i.imgur.com/yn9n7Mn.png) 他是算差件 記住他的grpc只能用一元grpc ### 自訂輸出欄位名稱 預設是fullName 改snake的話 ![](https://i.imgur.com/8PlC7Jn.png) ### matedata 獲取matedata get Ip agent之類的 ![](https://i.imgur.com/5EZ1mYk.png) ![](https://i.imgur.com/IbGDSMw.png) 獲取metadata 但是grpc 有些 ex ip 不再metadata裡面 所以要從ctx裡面拉出來 ![](https://i.imgur.com/gegSjKD.png) test的部分 ![](https://i.imgur.com/Q3v9ogX.png) ### swagger ![](https://i.imgur.com/WzmwKai.png) 先搜尋找到這個 然後看關鍵字v2 ... 去搜尋會發現它是一開始安裝的放在bin裡面 可執行黨 所以就help去找他看他能不能匯出一個檔案 會找到 ![](https://i.imgur.com/lQRkibc.png) ![](https://i.imgur.com/SzBeg8a.png) 這兩個 ### 安裝swaager UI 把swagger UI 安裝近來 只要dist資料夾 因為我只要UI部分 init.js 的部分放入對應json 不知道為什麼他不能往上層資料夾找 fileServer用好 ![](https://i.imgur.com/H7pEanJ.png) ## 採坑 go_out= 這最新的會有問題 改用二進制下載就好 import錯誤 https://mileslin.github.io/2020/04/Golang/Import-%E5%85%B6%E4%BB%96-proto/ go_package 是別人導入的位置 所以要加上package name ![](https://i.imgur.com/q7uyTF3.png) ### 安裝步驟 第一安裝 Compile from source 開一個tool資料夾讓他install 第二輸出 ![](https://i.imgur.com/CmEu6Km.png) 一樣指定位置跟source_relation 安裝google.api.http 到import 我使用protoc 所以要把檔案clone下來 ### customer 一些東西 ![](https://i.imgur.com/C45iBCc.png) ### 時間轉換 ![](https://i.imgur.com/U9hugwa.png) new轉換 ## server ![](https://i.imgur.com/rhepuyE.png) server要embed 下面那個struct 這感覺是extend的特別用法 ## validation ![](https://i.imgur.com/tJra8nj.png) ![](https://i.imgur.com/ngdtIQp.png) 先將feild放好 然後建立badrequest 把field放進去 然後建立status 平常也是用這個 接著status放入badrequest ## fieldmask ## 欄位對應 oneof 可以擇一 ![](https://i.imgur.com/2RwYe6g.png) ## stream https://www.cnblogs.com/FireworksEasyCool/p/12693749.html ## health https://www.twle.cn/t/19358 ## grpc-middware https://www.cnblogs.com/FireworksEasyCool/p/12750339.html 看 https://www.cnblogs.com/rickiyang/p/14975552.html ## SSL / TLS https://www.cnblogs.com/rickiyang/p/14981374.html ``` 对于加密传输的部分 gRPC 将它抽出来作为一个组件,可以由用户自由选择。gRPC 内默认提供了两种 内置的认证方式: 基于 CA 证书的 SSL/TLS 认证方式; 基于 Token 的认证方式。 同时也提供了可扩展的用户自定义认证方式。 gRPC 中的连接类型一共有以下 3 种: insecure connection:不使用 TLS 加密; server-side TLS:仅服务端 TLS 加密; mutual TLS:客户端、服务端都使用 TLS 加密 ``` ## convert 時間轉換 ![](https://i.imgur.com/m4Gs7pi.png) ## oneof ![](https://i.imgur.com/xrXArL6.png) 可以讓相似的共用一些東西 例如都是message很多可以共用 ###### tags: `Go`