diff --git a/ghz.md b/doc/ghz.md similarity index 100% rename from ghz.md rename to doc/ghz.md diff --git a/grpcurl.md b/doc/grpcurl.md similarity index 100% rename from grpcurl.md rename to doc/grpcurl.md diff --git a/doc/protoc.md b/doc/protoc.md index cef4fae..c32c49f 100644 --- a/doc/protoc.md +++ b/doc/protoc.md @@ -1,13 +1,25 @@ -Protobuf language bindings have two components. + +# protoc -The generated code (typically produced from `protoc`) and +## install -the runtime libraries that must be included when using the generated code. +# +```sh +unzip -j /d/zoo/protoc-28.2-win64.zip bin/protoc.exe -d /d/zoo/bin/ +``` + +## Usage + +Protobuf language bindings have two components. +The generated code (typically produced from `protoc`) and + +the runtime libraries that must be included when using the generated code. ```sh -PS C:\Users\han\coding> protoc -h +$protoc -h + Usage: C:\Program Files\protoc-3.19.1-win64\bin\protoc.exe [OPTION] PROTO_FILES Parse PROTO_FILES and generate output based on the options given: @@ -29,4 +41,3 @@ Parse PROTO_FILES and generate output based on the options given: --python_out=OUT_DIR Generate Python source file. --ruby_out=OUT_DIR Generate Ruby source file. ``` - diff --git a/docker/README.md b/docker/README.md index 969ff19..04e604f 100644 --- a/docker/README.md +++ b/docker/README.md @@ -1,40 +1,40 @@ -# build docker images - -## build all images +# Docker Images + +https://hub.docker.com/repositories/feuyeux + +| No. | Lang | Base Image | Server | Client | +|:----|:-----------------------------|:---------------------------------------------------------------|:---------------------------------|:---------------------------------| +| 1 | [C++](hello-grpc-cpp) | | feuyeux/grpc_server_cpp:1.0.0 | feuyeux/grpc_client_cpp:1.0.0 | +| 2 | [Rust](hello-grpc-rust) | | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 | +| 3 | [Java](hello-grpc-java) | | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 | +| 4 | [Go](hello-grpc-go) | [golang:1.23-alpine](go_grpc.dockerfile) | feuyeux/grpc_server_go:1.0.0 | feuyeux/grpc_client_go:1.0.0 | +| 5 | [C#](hello-grpc-csharp) | [mcr.microsoft.com/dotnet/runtime:8.0](csharp_grpc.dockerfile) | feuyeux/grpc_server_csharp:1.0.0 | feuyeux/grpc_client_csharp:1.0.0 | +| 6 | [Python](hello-grpc-python) | | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 | +| 7 | [Node.js](hello-grpc-nodejs) | | feuyeux/grpc_server_nodejs:1.0.0 | feuyeux/grpc_client_nodejs:1.0.0 | +| 8 | [Dart](hello-grpc-dart) | | feuyeux/grpc_server_dart:1.0.0 | feuyeux/grpc_client_dart:1.0.0 | +| 9 | [Kotlin](hello-grpc-kotlin) | | feuyeux/grpc_server_kotlin:1.0.0 | feuyeux/grpc_client_kotlin:1.0.0 | +| 10 | [Swift](hello-grpc-swift) | | feuyeux/grpc_server_swift:1.0.0 | feuyeux/grpc_client_swift:1.0.0 | +| 11 | [PHP](hello-grpc-php) | | feuyeux/grpc_server_php:1.0.0 | feuyeux/grpc_client_php:1.0.0 | +| 12 | [TypeScript](hello-grpc-ts) | | feuyeux/grpc_server_ts:1.0.0 | feuyeux/grpc_client_ts:1.0.0 | + +## Build ```sh -sh build.sh - -langs=(cpp rust java go csharp python nodejs dart kotlin swift php ts) -lang=$langs[2] -sh build_$lang.sh +sh ${lang}_build.sh ``` -## push all images +## Run ```sh -sh push.sh +sh ${lang}_run_server.sh ``` -## verify +```sh +sh ${lang}_run_client.sh +``` -### clean all containers +## Push ```sh -sh tools/clean_world.sh +sh ${lang}_push.sh ``` - -| No. | Lang | Server | Client | -|:----|:-----------------------------|:---------------------------------|:---------------------------------| -| 1 | [C++](hello-grpc-cpp) | feuyeux/grpc_server_cpp:1.0.0 | feuyeux/grpc_client_cpp:1.0.0 | -| 2 | [Rust](hello-grpc-rust) | feuyeux/grpc_server_rust:1.0.0 | feuyeux/grpc_client_rust:1.0.0 | -| 3 | [Java](hello-grpc-java) | feuyeux/grpc_server_java:1.0.0 | feuyeux/grpc_client_java:1.0.0 | -| 4 | [Go](hello-grpc-go) | feuyeux/grpc_server_go:1.0.0 | feuyeux/grpc_client_go:1.0.0 | -| 5 | [C#](hello-grpc-csharp) | feuyeux/grpc_server_csharp:1.0.0 | feuyeux/grpc_client_csharp:1.0.0 | -| 6 | [Python](hello-grpc-python) | feuyeux/grpc_server_python:1.0.0 | feuyeux/grpc_client_python:1.0.0 | -| 7 | [Node.js](hello-grpc-nodejs) | feuyeux/grpc_server_nodejs:1.0.0 | feuyeux/grpc_client_nodejs:1.0.0 | -| 8 | [Dart](hello-grpc-dart) | feuyeux/grpc_server_dart:1.0.0 | feuyeux/grpc_client_dart:1.0.0 | -| 9 | [Kotlin](hello-grpc-kotlin) | feuyeux/grpc_server_kotlin:1.0.0 | feuyeux/grpc_client_kotlin:1.0.0 | -| 10 | [Swift](hello-grpc-swift) | feuyeux/grpc_server_swift:1.0.0 | feuyeux/grpc_client_swift:1.0.0 | -| 11 | [PHP](hello-grpc-php) | feuyeux/grpc_server_php:1.0.0 | feuyeux/grpc_client_php:1.0.0 | -| 12 | [TypeScript](hello-grpc-ts) | feuyeux/grpc_server_ts:1.0.0 | feuyeux/grpc_client_ts:1.0.0 | diff --git a/docker/csharp_build.sh b/docker/csharp_build.sh index 9ab9b7a..601fc21 100644 --- a/docker/csharp_build.sh +++ b/docker/csharp_build.sh @@ -16,7 +16,7 @@ elif [[ "${1}" == "s" ]]; then docker build -f csharp_grpc.dockerfile --target server -t feuyeux/grpc_server_csharp:1.0.0 . else echo "build csharp" - docker build -f csharp_grpc.dockerfile --target build -t feuyeux/grpc_csharp:1.0.0 . + docker build -f csharp_grpc.dockerfile --target build -t feuyeux/grpc_build_csharp:1.0.0 . docker build -f csharp_grpc.dockerfile --target server -t feuyeux/grpc_server_csharp:1.0.0 . docker build -f csharp_grpc.dockerfile --target client -t feuyeux/grpc_client_csharp:1.0.0 . fi diff --git a/docker/build_go.sh b/docker/go_build.sh old mode 100755 new mode 100644 similarity index 76% rename from docker/build_go.sh rename to docker/go_build.sh index 7a454f8..a6432a6 --- a/docker/build_go.sh +++ b/docker/go_build.sh @@ -13,7 +13,7 @@ echo "~~~ build grpc server golang ~~~" env GOOS=linux GOARCH=amd64 go build -o proto_server server/proto_server.go mv proto_server ../docker/ cd ../docker -docker build -f grpc-server-go.dockerfile -t feuyeux/grpc_server_go:1.0.0 . +docker build -f go_grpc.dockerfile --target server -t feuyeux/grpc_server_go:1.0.0 . rm -rf proto_server echo @@ -23,6 +23,6 @@ export GO111MODULE="on" env GOOS=linux GOARCH=amd64 go build -o proto_client client/proto_client.go mv proto_client ../docker/ cd ../docker -docker build -f grpc-client-go.dockerfile -t feuyeux/grpc_client_go:1.0.0 . +docker build -f go_grpc.dockerfile --target client -t feuyeux/grpc_client_go:1.0.0 . rm -rf proto_client echo diff --git a/docker/go_grpc.dockerfile b/docker/go_grpc.dockerfile new file mode 100644 index 0000000..e712d0b --- /dev/null +++ b/docker/go_grpc.dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.23-alpine AS server +# https://hub.docker.com/_/golang +COPY proto_server grpc-server +COPY tls/server_certs /var/hello_grpc/server_certs +COPY tls/client_certs /var/hello_grpc/client_certs +ENTRYPOINT ["./grpc-server"] + +FROM golang:1.23-alpine AS client +COPY proto_client grpc-client +COPY tls/client_certs /var/hello_grpc/client_certs +ENTRYPOINT ["./grpc-client"] diff --git a/docker/grpc-client-go.dockerfile b/docker/go_grpc_client.dockerfile similarity index 82% rename from docker/grpc-client-go.dockerfile rename to docker/go_grpc_client.dockerfile index 34233eb..5b84661 100644 --- a/docker/grpc-client-go.dockerfile +++ b/docker/go_grpc_client.dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.22-alpine +FROM golang:1.23-alpine COPY proto_client grpc-client COPY tls/client_certs /var/hello_grpc/client_certs ENTRYPOINT ["./grpc-client"] diff --git a/docker/go_push.sh b/docker/go_push.sh new file mode 100644 index 0000000..c199728 --- /dev/null +++ b/docker/go_push.sh @@ -0,0 +1,3 @@ +#!/bin/bash +docker push feuyeux/grpc_server_go:1.0.0 +docker push feuyeux/grpc_client_go:1.0.0 diff --git a/docker/go_run_client.sh b/docker/go_run_client.sh new file mode 100644 index 0000000..1c1f32d --- /dev/null +++ b/docker/go_run_client.sh @@ -0,0 +1,13 @@ +#!/bin/bash +cd "$( + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P +)/" || exit +export CLIENT_NAME=grpc_client_go +export CLIENT_IMG=feuyeux/$CLIENT_NAME:1.0.0 +# if there's first argument, it's secure, otherwise insecure +if [ "$1" = "secure" ]; then + sh run_tls_client.sh +else + sh run_insecure_client.sh +fi diff --git a/docker/go_run_server.sh b/docker/go_run_server.sh new file mode 100644 index 0000000..03ced30 --- /dev/null +++ b/docker/go_run_server.sh @@ -0,0 +1,13 @@ +#!/bin/bash +cd "$( + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P +)/" || exit +export SERVER_NAME=grpc_server_go +export SERVER_IMG=feuyeux/$SERVER_NAME:1.0.0 +# if there's first argument, it's secure, otherwise insecure +if [ "$1" = "secure" ]; then + sh run_tls_server.sh +else + sh run_insecure_server.sh +fi diff --git a/docker/grpc-server-go.dockerfile b/docker/grpc-server-go.dockerfile deleted file mode 100644 index b286c4c..0000000 --- a/docker/grpc-server-go.dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM golang:1.22-alpine -COPY proto_server grpc-server -COPY tls/server_certs /var/hello_grpc/server_certs -COPY tls/client_certs /var/hello_grpc/client_certs -ENTRYPOINT ["./grpc-server"] diff --git a/docker/tools/fix_fmt.sh b/docker/tools/fix_fmt.sh deleted file mode 100644 index 7579791..0000000 --- a/docker/tools/fix_fmt.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -cd "$( - cd "$(dirname "$0")" >/dev/null 2>&1 - pwd -P -)/" || exit -chmod +x ./*.sh -dos2unix ./*.sh -chmod +x ../*.sh -dos2unix ../*.sh diff --git a/hello-grpc-go/README.md b/hello-grpc-go/README.md index d799d1e..9090182 100644 --- a/hello-grpc-go/README.md +++ b/hello-grpc-go/README.md @@ -15,12 +15,7 @@ sh proto2go.sh ## 3 Build ```bash -go mod tidy -go fmt hello-grpc/... -go fmt server/proto_server.go -go fmt client/proto_client.go -go install server/proto_server.go -go install client/proto_client.go +sh build.sh ``` ## 4 Run diff --git a/hello-grpc-go/client_start.sh b/hello-grpc-go/client_start.sh index 5ac6579..0ece3a2 100755 --- a/hello-grpc-go/client_start.sh +++ b/hello-grpc-go/client_start.sh @@ -1,7 +1,8 @@ #!/bin/bash +# shellcheck disable=SC2046 cd "$( - cd "$(dirname "$0")" >/dev/null 2>&1 - pwd -P + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P )/" || exit export GO111MODULE="on" @@ -16,8 +17,9 @@ elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then export GOPATH=$GOPATH:${PWD} GRPC_SERVER=$(hostname -I | cut -d' ' -f1) elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ] || [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then - # Do something under Windows NT platform - export GOPATH=$GOPATH;$PWD + windows_path=$GOPATH + linux_path=$(echo "$windows_path" | sed 's/^\([a-zA-Z]\):/\/\1/' | sed 's/\\/\//g') + export GOPATH=$linux_path:${PWD} GRPC_SERVER=$(ipconfig | grep -A 3 'Ethernet adapter Ethernet' | grep 'IPv4 Address' | cut -d: -f2 | sed 's/ //g') fi diff --git a/hello-grpc-go/conn/connection.go b/hello-grpc-go/conn/connection.go index cda38e7..45cc9bb 100644 --- a/hello-grpc-go/conn/connection.go +++ b/hello-grpc-go/conn/connection.go @@ -74,7 +74,7 @@ func buildConnByDisc() *grpc.ClientConn { Certificates: []tls.Certificate{cert}, RootCAs: GetCertPool(rootCert), } - conn, err := grpc.Dial("etcd:///", + conn, err := grpc.NewClient("etcd:///", grpc.WithStatsHandler(&StatsHandler{}), grpc.WithTransportCredentials(credentials.NewTLS(c)), grpc.WithDefaultServiceConfig(grpcServiceConfig)) @@ -84,7 +84,7 @@ func buildConnByDisc() *grpc.ClientConn { return conn } else { log.Infof("Connect With InSecure through discovery") - conn, err := grpc.Dial("etcd:///", + conn, err := grpc.NewClient("etcd:///", grpc.WithStatsHandler(&StatsHandler{}), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(grpcServiceConfig)) @@ -133,7 +133,7 @@ func transportInsecure(address string) (*grpc.ClientConn, error) { Timeout: time.Second, // wait 1 second for ping ack before considering the connection dead PermitWithoutStream: true, // send pings even without active streams }) - return grpc.Dial(address, + return grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()), keepaliveConfig, retryConfig, @@ -146,7 +146,7 @@ func transportCredentials(address string) (*grpc.ClientConn, error) { if err != nil { panic(err) } - return grpc.Dial(address, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ + return grpc.NewClient(address, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ ServerName: serverName, Certificates: []tls.Certificate{cert}, RootCAs: GetCertPool(rootCert), diff --git a/hello-grpc-go/go.mod b/hello-grpc-go/go.mod index 334599f..f576123 100644 --- a/hello-grpc-go/go.mod +++ b/hello-grpc-go/go.mod @@ -1,6 +1,6 @@ module hello-grpc -go 1.22 +go 1.23 require ( // https://github.com/google/uuid/tags @@ -22,7 +22,7 @@ require ( // https://github.com/grpc/grpc-go/tags google.golang.org/grpc v1.67.0 // https://github.com/protocolbuffers/protobuf-go/tags - google.golang.org/protobuf v1.34.2 // indirect + google.golang.org/protobuf v1.34.2 ) require ( diff --git a/hello-grpc-go/go.mod.bk b/hello-grpc-go/go.mod.bk deleted file mode 100644 index 6f45c04..0000000 --- a/hello-grpc-go/go.mod.bk +++ /dev/null @@ -1,26 +0,0 @@ -module hello-grpc - -go 1.22 - -require ( - // https://github.com/google/uuid/tags - github.com/google/uuid latest - // https://github.com/sirupsen/logrus/tags - github.com/sirupsen/logrus latest - // https://pkg.go.dev/golang.org/x/net - golang.org/x/net latest - // https://github.com/grpc/grpc-go/tags - google.golang.org/grpc latest - // https://github.com/protocolbuffers/protobuf-go/tags - google.golang.org/protobuf latest - // https://github.com/stretchr/testify/tags - github.com/stretchr/testify latest - // https://pkg.go.dev/go.etcd.io/etcd/client/v3?tab=versions - go.etcd.io/etcd/client/v3 latest - - // Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more. - // https://github.com/grpc-ecosystem/go-grpc-middleware/tags - github.com/grpc-ecosystem/go-grpc-middleware latest - // https://pkg.go.dev/go.uber.org/ratelimit?tab=versions - go.uber.org/ratelimit latest -) \ No newline at end of file diff --git a/hello-grpc-go/log/log.go b/hello-grpc-go/log/log.go index 3b1330d..8c3dc20 100644 --- a/hello-grpc-go/log/log.go +++ b/hello-grpc-go/log/log.go @@ -13,20 +13,20 @@ import ( // DInfo dev info func DInfo(msg string) string { id := uuid.New().String() - filename, line, funcname, funcname1, funcname2 := buildLogParams() - log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcname, funcname1, funcname2, msg) + filename, line, funcName, funcName1, funcName2 := buildLogParams() + log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcName, funcName1, funcName2, msg) return id } // TInfo dev tracing info func TInfo(id, msg string) { - filename, line, funcname, funcname1, funcname2 := buildLogParams() - log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcname, funcname1, funcname2, msg) + filename, line, funcName, funcName1, funcName2 := buildLogParams() + log.Infof("[%s] %s[%d]:%s<-%s<-%s: %s\n", id, filename, line, funcName, funcName1, funcName2, msg) } func DInfof(format string, args ...interface{}) { - filename, line, funcname, funcname1, funcname2 := buildLogParams() - log.Infof("%s[%d]:%s<-%s<-%s: %s\n", filename, line, funcname, funcname1, funcname2, fmt.Sprintf(format, args...)) + filename, line, funcName, funcName1, funcName2 := buildLogParams() + log.Infof("%s[%d]:%s<-%s<-%s: %s\n", filename, line, funcName, funcName1, funcName2, fmt.Sprintf(format, args...)) } func buildLogParams() (string, int, string, string, string) { @@ -34,7 +34,7 @@ func buildLogParams() (string, int, string, string, string) { } func buildLogParams0(skip int) (string, int, string, string, string) { - funcName, funcName1, funcname2, line, filename := "???", "???", "???", 0, "???" + funcName, funcName1, funcName2, line, filename := "???", "???", "???", 0, "???" pc, filename, line, ok := runtime.Caller(skip) if ok { funcName = runtime.FuncForPC(pc).Name() @@ -50,9 +50,9 @@ func buildLogParams0(skip int) (string, int, string, string, string) { } pc2, _, _, ok := runtime.Caller(skip + 2) if ok { - funcname2 = runtime.FuncForPC(pc2).Name() - funcname2 = filepath.Ext(funcname2) - funcname2 = strings.TrimPrefix(funcname2, ".") + funcName2 = runtime.FuncForPC(pc2).Name() + funcName2 = filepath.Ext(funcName2) + funcName2 = strings.TrimPrefix(funcName2, ".") } - return filename, line, funcName, funcName1, funcname2 + return filename, line, funcName, funcName1, funcName2 } diff --git a/hello-grpc-go/proto2go.sh b/hello-grpc-go/proto2go.sh index 940912d..ef629f1 100755 --- a/hello-grpc-go/proto2go.sh +++ b/hello-grpc-go/proto2go.sh @@ -5,15 +5,28 @@ cd "$( pwd -P )/" || exit export GOROOT=$(go env GOROOT) -echo "GOROOT=$GOROOT" -export GOPATH=$HOME/gopath +export GOPATH=$(go env GOPATH) +echo "GOROOT=$GOROOT GOPATH=$GOPATH" export PATH="$PATH:$GOPATH/bin" -# -export GOPROXY=https://mirrors.aliyun.com/goproxy/ -go install google.golang.org/protobuf/cmd/protoc-gen-go@latest -go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest -# proto2go.sh: 18: protoc: not found +PROTOC_VERSION=$(protoc --version) +if [ -z "$PROTOC_VERSION" ]; then + echo "install protoc firstly, see doc/proto.md" + exit 1 +else + echo "Protoc version: $PROTOC_VERSION" +fi + +if [ ! -f "$GOPATH/bin/protoc-gen-go.exe" ]; then + export GOPROXY=https://mirrors.aliyun.com/goproxy/ + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest + go install -v golang.org/x/tools/gopls@latest + echo "protoc-gen-go.exe has been installed successfully." +else + echo "protoc-gen-go.exe is already installed." +fi + echo "generate the messages" protoc --go_out=. ./proto/landing.proto echo "generate the services" diff --git a/hello-grpc-go/server_start.sh b/hello-grpc-go/server_start.sh index 3c1ab10..dec47ec 100755 --- a/hello-grpc-go/server_start.sh +++ b/hello-grpc-go/server_start.sh @@ -7,11 +7,13 @@ cd "$( export GO111MODULE="on" if [ "$(uname)" == "Darwin" ] || [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then - # Do something under Mac OS X or Linux platform - export GOPATH=$GOPATH:${PWD} + export GOPATH=$GOPATH:${PWD} + echo "[Mac OS X or Linux] GOPATH=$GOPATH" elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ] || [ "$(expr substr $(uname -s) 1 10)" == "MINGW64_NT" ]; then - # Do something under Windows NT platform - export GOPATH=$GOPATH;$PWD + windows_path=$GOPATH + linux_path=$(echo "$windows_path" | sed 's/^\([a-zA-Z]\):/\/\1/' | sed 's/\\/\//g') + export GOPATH=$linux_path:${PWD} + echo "[Windows] GOPATH=$GOPATH" fi -go run server/proto_server.go \ No newline at end of file +go run server/proto_server.go diff --git a/package.json b/package.json deleted file mode 100644 index 8591764..0000000 --- a/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "dependencies": { - "@grpc/grpc-js": "^1.9.2", - "google-protobuf": "^3.21.2" - }, - "devDependencies": { - "grpc-tools": "^1.12.4", - "grpc_tools_node_protoc_ts": "^5.3.3", - "typescript": "^5.2.2" - } -} diff --git a/sh_dos2unix.sh b/sh_dos2unix.sh index d3249b4..03e1b11 100755 --- a/sh_dos2unix.sh +++ b/sh_dos2unix.sh @@ -1 +1,7 @@ +#!/bin/bash +cd "$( + cd "$(dirname "$0")" >/dev/null 2>&1 + pwd -P +)/" || exit + find . -type f -name "*.sh" -print0 | xargs -0 dos2unix