Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: finish triple helloworld example #612

Merged
merged 15 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ jobs:

- name: Integration Test
run: |
./start_integrate_test.sh
./start_integrate_test.sh
14 changes: 0 additions & 14 deletions .run/dubbo-go-server.run.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .run/helloworld-go-client.run.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .run/helloworld-go-server.run.xml

This file was deleted.

14 changes: 0 additions & 14 deletions .run/rpc/dubbo/rpc-dubbo-go-client.run.xml

This file was deleted.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/apache/dubbo-go-samples

require (
dubbo.apache.org/dubbo-go/v3 v3.0.6-0.20230714083639-412bf7b2f36a
dubbo.apache.org/dubbo-go/v3 v3.1.1-0.20231026074600-9d62f726adba
github.com/SkyAPM/go2sky v1.5.0
github.com/SkyAPM/go2sky-plugins/dubbo-go v0.0.0-20220718123631-cb8f743b16cf
github.com/apache/dubbo-go-hessian2 v1.12.2
Expand All @@ -12,7 +12,7 @@ require (
github.com/golang/protobuf v1.5.3
github.com/opentracing/opentracing-go v1.2.0
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5
github.com/openzipkin/zipkin-go v0.2.2
github.com/openzipkin/zipkin-go v0.4.0
github.com/pkg/errors v0.9.1
github.com/seata/seata-go v0.1.0-rc1
github.com/stretchr/testify v1.8.4
Expand Down
166 changes: 157 additions & 9 deletions go.sum

Large diffs are not rendered by default.

222 changes: 199 additions & 23 deletions helloworld/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,199 @@
# Helloworld for Dubbo-go 3.0

For api definition and go client and server startup, please refer to [dubbo-go 3.0 quickstart](https://dubbogo.github.io/zh-cn/docs/user/quickstart/3.0/quickstart.html)

## Instructions
1. Start zookeeper

2. Start the server

Use goland to start helloworld-go-server

or

Execute `sh run.sh` in the java-server folder to start the java server

3. Start the client

Use goland to start helloworld-go-client

or

Execute `sh run.sh` under the java-client folder to start the java client

# Helloworld for dubbo-go Triple

This is **Triple** helloworld example to help you finish a basic RPC invocation done quickly.

## Prerequisites

### install protoc

#### install with package manager

```shell
# for Linux, use apt or apt-get
apt install -y protobuf-compiler
protoc --version # make sure that version is 3+

# for Macos, using homebrew
brew install protobuf
protoc --version # make sure that version is 3+
```

#### install pre-compiled binary

```shell
# fetch the pre-compiled protoc corresponding to your operating system and computer architecture
# protoc-<version>-<os>-<arch>.zip
# or you can download from github.com/protocolbuffers/protobuf/releases manually
PB_REL="https://github.com/protocolbuffers/protobuf/releases"
curl -LO $PB_REL/download/v24.4/protoc-24.4-linux-x86_64.zip

# unzip downloaded file under a directory
unzip protoc-24.4-linux-x86_64.zip -d $HOME/.local

# update path variable
export PATH="$PATH:$HOME/.local/bin"
```

#### build protoc from sources

Please see [**Download Protocol Buffers**](https://protobuf.dev/downloads/).

### install protoc-gen-go

```shell
# install the version of your choice of protoc-gen-go. here use the latest version as example
go install google.golang.org/protobuf/cmd/[email protected]
```

### install protoc-gen-triple
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename to protoc-gen-triple-go or protoc-gen-go-triple to follow the protoc plugin naming convention?


```shell
# install the latest version of protoc-gen-triple
git clone https://github.com/apache/dubbo-go.git && cd ./dubbo-go
git checkout feature-triple
go mod tidy
cd ./protocol/triple/triple-tool/protoc-gen-triple
go install .
```

## Generate Triple stub code

```shell
mkdir ~/triple_helloworld && cd ~/triple_helloworld
go mod init triple_helloworld
mkdir proto && cd ./proto

# replace this with your own proto IDL file
cat > greet.proto << EOF
syntax = "proto3";

package greet;

option go_package = "triple_helloworld/proto;greet";

message GreetRequest {
string name = 1;
}

message GreetResponse {
string greeting = 1;
}

service GreetService {
rpc Greet(GreetRequest) returns (GreetResponse) {}
}

EOF

# generate related stub code with protoc-gen-go and protoc-gen-triple
protoc --go_out=. --go_opt=paths=source_relative --triple_out=. --triple_opt=paths=source_relative ./greet.proto
```

## Finish client and server code

### client

```shell
cd ~/triple_helloworld
mkdir -p go-client/cmd && cd ./go-client/cmd
```

Finish **client.go** and put it in **go-client/cmd** directory.

```go
package main

import (
"context"
"dubbo.apache.org/dubbo-go/v3/client"
// important, must import this for dubbo-go extensions
_ "dubbo.apache.org/dubbo-go/v3/imports"
greet "triple_helloworld/proto"
"triple_helloworld/proto/greettriple"
"github.com/dubbogo/gost/log/logger"
)

func main() {
// initialize a Client which is responsible for invoking a certain service. it uses Triple protocol by default
// if you want to invoke another service, please initialize a new one
cli, err := client.NewClient(
// specify target server URL
client.WithURL("127.0.0.1:20000"),
)
if err != nil {
panic(err)
}

svc, err := greettriple.NewGreetService(cli)
if err != nil {
panic(err)
}

resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
if err != nil {
logger.Error(err)
}
logger.Infof("Greet response: %s", resp.Greeting)
}
```

### server
```shell
cd ~/triple_helloworld
mkdir -p go-server/cmd
mkdir -p go-server/handler
```

Implement **GreetService** Interface and put it in **go-server/handler** directory.
Please refer to [**concrete sample**](https://github.com/apache/dubbo-go-samples/tree/new-triple-samples/helloworld/go-server/handler).

Finish **server.go** and put it in **go-server/cmd** directory.

```go
package main

import (
// important, must import this for dubbo-go extensions
_ "dubbo.apache.org/dubbo-go/v3/imports"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/server"
"triple_helloworld/go-server/handler"
"triple_helloworld/proto/greettriple"
"github.com/dubbogo/gost/log/logger"
)

func main() {
// initialize a Server for serving multiple services
srv, err := server.NewServer(
// use Triple protocol by default
server.WithServerProtocol(
// specify port to listen on
protocol.WithPort(20000),
),
)
if err != nil {
panic(err)
}
// register a certain service
if err := greettriple.RegisterGreetServiceHandler(srv, &handler.GreetTripleServer{}); err != nil {
panic(err)
}
if err := srv.Serve(); err != nil {
logger.Error(err)
}
}
```

## Build and run

```shell
cd ~/triple_helloworld/go-server/cmd
go build -o server .
./server
```

```shell
cd ~/triple_helloworld/go-client/cmd
go build -o client .
./client
```
Loading