>[success] # 客戶端編寫案例
實現一個簡單 `web-grpc` 案例,使用 `webpack ` 構建項目
~~~
.
|-- dist
| |-- bundle.js
| |-- bundle.js.map
| `-- index.html
|-- package.json
|-- pnpm-lock.yaml
|-- protos
| `-- greet.proto
|-- src
| |-- index.js
| `-- proto
| |-- greet_grpc_web_pb.js
| `-- greet_pb.js
|-- tsconfig.json
`-- webpack.config.js
~~~
>[danger] ##### 準備 `proto` 文件
~~~
syntax = "proto3"; // 指定使用proto3,如果不指定的話,編譯器會使用proto2去編譯
package greet; // 編譯后的包名
message HelloRequest { // 定義請求對象類型
string name = 1; // string 類型name 字段1為 該成員編碼時用1編號代替名字
}
message HelloReply { // 定義響應對象類型
string message = 1; // string 類型name 字段1為 該成員編碼時用1編號代替名字
}
service Greeter { // 定義服務接口
//定義一個方法,SayHello 請求參數HelloRequest類型響應是HelloReply類型
rpc SayHello (HelloRequest) returns (HelloReply){};
}
~~~
>[danger] ##### 轉換 `proto` 文件
* 進入`protos `文件夾 執行

~~~
protoc *.proto --js_out=import_style=commonjs:../src/proto --grpc-web_out=import_style=commonjs,mode=grpcwebtext:../src/proto
~~~
>[danger] ##### 安裝 grpc-web
~~~
npm install google-protobuf @types/google-protobuf @improbable-eng/grpc-web --save
~~~
>[danger] ##### index.js 文件編寫grpc 請求
~~~
import { HelloRequest } from './proto/greet_pb.js'
import { GreeterClient } from './proto/greet_grpc_web_pb.js'
const url = ''
const client = new GreeterClient(url)
const helloRequest = new HelloRequest()
console.log(helloRequest)
helloRequest.setName('北京')
const metadata = { 'Content-Type': 'application/grpc-web+proto' } // header頭數據
client.sayHello(helloRequest, metadata, (err, response) => {
console.log(err, response)
})
~~~
>[success] # 服務的搭建
~~~
.
|-- index.js
|-- package.json
`-- pnpm-lock.yaml
~~~
使用 `node ` 作為客戶端安裝
~~~
npm i @grpc/grpc-js @grpc/proto-loader
~~~
>[danger] ##### 客戶端代碼
~~~
var PROTO_PATH = __dirname + '../../protos/greet.proto'
var grpc = require('@grpc/grpc-js')
var protoLoader = require('@grpc/proto-loader')
var packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
})
var hello_proto = grpc.loadPackageDefinition(packageDefinition).greet
/**
* Implements the SayHello RPC method.
*/
function sayHello(call, callback) {
console.log(123)
callback(null, { message: 'Hello ' + call.request.name })
}
/**
* Starts an RPC server that receives requests for the Greeter service at the
* sample server port
*/
function main() {
var server = new grpc.Server()
server.addService(hello_proto.Greeter.service, { sayHello: sayHello })
server.bindAsync(
'0.0.0.0:50051',
grpc.ServerCredentials.createInsecure(),
() => {
server.start()
}
)
}
main()
~~~
>[success] # 配置ENYO 代理
* `enyoygrpcweb.yaml`
~~~
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 0.0.0.0, port_value: 9901 }
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 10000 }
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
'@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ['*']
routes:
- match: { prefix: '/' }
route:
cluster: greeter_service
max_stream_duration:
grpc_timeout_header_max: 0s
cors:
allow_origin_string_match:
- prefix: '*'
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: '1728000'
expose_headers: custom-header-1,grpc-status,grpc-message
http_filters:
- name: envoy.filters.http.grpc_web
typed_config:
'@type': type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb
- name: envoy.filters.http.cors
typed_config:
'@type': type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors
- name: envoy.filters.http.router
typed_config:
'@type': type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: greeter_service
connect_timeout: 0.25s
type: static
http2_protocol_options: {}
lb_policy: round_robin
# win/mac hosts: Use address: host.docker.internal instead of address: localhost in the line below
load_assignment:
cluster_name: cluster_0
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: xxxxx
port_value: 50051
~~~
node 服務啟動的 `address`: 本機`ip` 和`port_value`: 端口
>[success] # 在docker 安裝 envoy
~~~
docker pull envoyproxy/envoy-dev
~~~
運行
~~~
docker run --rm -it -p 9901:9901 -p 10000:10000 -v C:/xx/envoy/:/etc/envoy/ envoyproxy/envoy-dev -c /etc/envoy/enyoygrpcweb.yaml
~~~
>[info] ## 參考
[# gRPC四種模式、認證和授權實戰演示,必贊](https://juejin.cn/post/6984210268871983118#heading-5)
[gRPC Web 示例
](https://www.vinsguru.com/grpc-web-example/)
[前端 Web gRPC 實踐和優化 文件較老1. google-protobuf + grpc-web-client 已經更改為了@improbable-eng/grpc-web](https://juejin.cn/post/6844903698351521805#heading-5)
- 工程化 -- Node
- vscode -- 插件
- vscode -- 代碼片段
- 前端學會調試
- 谷歌瀏覽器調試技巧
- 權限驗證
- 包管理工具 -- npm
- 常見的 npm ci 指令
- npm -- npm install安裝包
- npm -- package.json
- npm -- 查看包版本信息
- npm - package-lock.json
- npm -- node_modules 層級
- npm -- 依賴包規則
- npm -- install 安裝流程
- npx
- npm -- 發布自己的包
- 包管理工具 -- pnpm
- 模擬數據 -- Mock
- 頁面渲染
- 渲染分析
- core.js && babel
- core.js -- 到底是什么
- 編譯器那些術語
- 詞法解析 -- tokenize
- 語法解析 -- ast
- 遍歷節點 -- traverser
- 轉換階段、生成階段略
- babel
- babel -- 初步上手之了解
- babel -- 初步上手之各種配置(preset-env)
- babel -- 初步上手之各種配置@babel/helpers
- babel -- 初步上手之各種配置@babel/runtime
- babel -- 初步上手之各種配置@babel/plugin-transform-runtime
- babel -- 初步上手之各種配置(babel-polyfills )(未來)
- babel -- 初步上手之各種配置 polyfill-service
- babel -- 初步上手之各種配置(@babel/polyfill )(過去式)
- babel -- 總結
- 各種工具
- 前端 -- 工程化
- 了解 -- Yeoman
- 使用 -- Yeoman
- 了解 -- Plop
- node cli -- 開發自己的腳手架工具
- 自動化構建工具
- Gulp
- 模塊化打包工具為什么出現
- 模塊化打包工具(新) -- webpack
- 簡單使用 -- webpack
- 了解配置 -- webpack.config.js
- webpack -- loader 淺解
- loader -- 配置css模塊解析
- loader -- 圖片和字體(4.x)
- loader -- 圖片和字體(5.x)
- loader -- 圖片優化loader
- loader -- 配置解析js/ts
- webpack -- plugins 淺解
- eslit
- plugins -- CleanWebpackPlugin(4.x)
- plugins -- CleanWebpackPlugin(5.x)
- plugin -- HtmlWebpackPlugin
- plugin -- DefinePlugin 注入全局成員
- webapck -- 模塊解析配置
- webpack -- 文件指紋了解
- webpack -- 開發環境運行構建
- webpack -- 項目環境劃分
- 模塊化打包工具 -- webpack
- webpack -- 打包文件是個啥
- webpack -- 基礎配置項用法
- webpack4.x系列學習
- webpack -- 常見loader加載器
- webpack -- 移動端px轉rem處理
- 開發一個自己loader
- webpack -- plugin插件
- webpack -- 文件指紋
- webpack -- 壓縮css和html構建
- webpack -- 清里構建包
- webpack -- 復制靜態文件
- webpack -- 自定義插件
- wepack -- 關于靜態資源內聯
- webpack -- source map 對照包
- webpack -- 環境劃分構建
- webpack -- 項目構建控制臺輸出
- webpack -- 項目分析
- webpack -- 編譯提速優護體積
- 提速 -- 編譯階段
- webpack -- 項目優化
- webpack -- DefinePlugin 注入全局成員
- webpack -- 代碼分割
- webpack -- 頁面資源提取
- webpack -- import按需引入
- webpack -- 搖樹
- webpack -- 多頁面打包
- webpack -- eslint
- webpack -- srr打包后續看
- webpack -- 構建一個自己的配置后續看
- webpack -- 打包組件和基礎庫
- webpack -- 源碼
- webpack -- 啟動都做了什么
- webpack -- cli做了什么
- webpack - 5
- 模塊化打包工具 -- Rollup
- 工程化搭建代碼規范
- 規范化標準--Eslint
- eslint -- 擴展配置
- eslint -- 指令
- eslint -- vscode
- eslint -- 原理
- Prettier -- 格式化代碼工具
- EditorConfig -- 編輯器編碼風格
- 檢查提交代碼是否符合檢查配置
- 整體流程總結
- 微前端
- single-spa
- 簡單上手 -- single-spa
- 快速理解systemjs
- single-sap 不使用systemjs
- monorepo -- 工程
- Vue -- 響應式了解
- Vue2.x -- 源碼分析
- 發布訂閱和觀察者模式
- 簡單 -- 了解響應式模型(一)
- 簡單 -- 了解響應式模型(二)
- 簡單 --了解虛擬DOM(一)
- 簡單 --了解虛擬DOM(二)
- 簡單 --了解diff算法
- 簡單 --了解nextick
- Snabbdom -- 理解虛擬dom和diff算法
- Snabbdom -- h函數
- Snabbdom - Vnode 函數
- Snabbdom -- init 函數
- Snabbdom -- patch 函數
- 手寫 -- 虛擬dom渲染
- Vue -- minVue
- vue3.x -- 源碼分析
- 分析 -- reactivity
- 好文
- grpc -- 瀏覽器使用gRPC
- grcp-web -- 案例
- 待續