Skip to content

Commit

Permalink
Merge pull request #639 from DMwangnima/new-triple-samples-modify_hel…
Browse files Browse the repository at this point in the history
…loworld

feat: simplify helloworld sample
  • Loading branch information
chickenlj authored Oct 29, 2023
2 parents 48d8623 + 5ef4b25 commit 0ecee7b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 568 deletions.
132 changes: 9 additions & 123 deletions helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,10 @@ This is **Triple** helloworld example to help you finish a basic RPC invocation

## Prerequisites

### install protoc
### install protocol buffer compiler, protoc, [version3](https://protobuf.dev/programming-guides/proto3/)

#### 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/).
Please refer to [**Protocol Buffer Compiler Installation**](https://grpc.io/docs/protoc-installation/) and [**Download Protocol Buffers**](https://protobuf.dev/downloads/).
After installation, please run ```protoc --verion``` to ensure that the version of protoc is 3+.

### install protoc-gen-go

Expand Down Expand Up @@ -93,107 +64,22 @@ protoc --go_out=. --go_opt=paths=source_relative --triple_out=. --triple_opt=pat

### 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)
}
```
Finish **client.go**. For specific code, please refer to [**this**](https://github.com/apache/dubbo-go-samples/blob/new-triple-samples/helloworld/client.go)

### 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)
}
}
```
Implement **GreetService** Interface and finish **server.go**. For specific code, please refer to [**this**](https://github.com/apache/dubbo-go-samples/blob/new-triple-samples/helloworld/server.go).

## Build and run

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

```shell
cd ~/triple_helloworld/go-client/cmd
go build -o client .
cd ~/triple_helloworld
go build -o client ./client.go
./client
```
131 changes: 9 additions & 122 deletions helloworld/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,10 @@

## 开始

### 安装protoc
### 安装版本为[3+](https://protobuf.dev/programming-guides/proto3/)的protoc

#### 使用包管理器

```shell
# 对于Linux用户, 使用apt或者apt-get
apt install -y protobuf-compiler
protoc --version # 确保版本是 3+

# 对于Macos用户, 使用homebrew
brew install protobuf
protoc --version # 确保版本是 3+
```

#### 使用预编译的二进制文件

```shell
# 根据您的操作系统和架构下载预先编译好的protoc文件
# protoc-<version>-<os>-<arch>.zip
# 或者您可以从 github.com/protocolbuffers/protobuf/releases 手动下载
PB_REL="https://github.com/protocolbuffers/protobuf/releases"
curl -LO $PB_REL/download/v24.4/protoc-24.4-linux-x86_64.zip

# 在一个目录下解压下载好的protoc文件,这里使用$HOME/.local作为示例
unzip protoc-24.4-linux-x86_64.zip -d $HOME/.local

# 更新环境变量
export PATH="$PATH:$HOME/.local/bin"
```

#### 使用源码进行编译

详情请参考[**下载Protocol Buffers**](https://protobuf.dev/downloads/).
详情请参考[**安装Protocol Buffer Compiler**](https://grpc.io/docs/protoc-installation/)[**下载Protocol Buffers**](https://protobuf.dev/downloads/)
安装完成后,请运行```protoc --version``来确保protoc的版本为3+。

### 安装 protoc-gen-go

Expand Down Expand Up @@ -93,106 +64,22 @@ protoc --go_out=. --go_opt=paths=source_relative --triple_out=. --triple_opt=pat

### 客户端

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

编写 **client.go** 并把它放置在 **go-client/cmd** 目录。

```go
package main

import (
"context"
"dubbo.apache.org/dubbo-go/v3/client"
// 重要,使用这个import声明引用dubbo-go扩展
_ "dubbo.apache.org/dubbo-go/v3/imports"
greet "triple_helloworld/proto"
"triple_helloworld/proto/greettriple"
"github.com/dubbogo/gost/log/logger"
)

func main() {
// 初始化一个用于调用特定服务的Client,如果你想要调用其它服务,请创建新的Client
cli, err := client.NewClient(
// 指定该服务的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)
}
```
编写 **client.go**。具体代码请参考[**这里**](https://github.com/apache/dubbo-go-samples/blob/new-triple-samples/helloworld/client.go)

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

实现 **GreetService** 接口并把它放置在 **go-server/handler** 目录。
请参考[**具体实现**](https://github.com/apache/dubbo-go-samples/tree/new-triple-samples/helloworld/go-server/handler).

完成 **server.go** 并把它放置在 **go-server/cmd** 目录。

```go
package main

import (
// 重要,使用这个import声明引用dubbo-go扩展
_ "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() {
// 初始化一个Server用于承载多个服务
srv, err := server.NewServer(
// 默认使用Triple协议
server.WithServerProtocol(
// 指定监听的端口
protocol.WithPort(20000),
),
)
if err != nil {
panic(err)
}
// 注册一个特定服务
if err := greettriple.RegisterGreetServiceHandler(srv, &handler.GreetTripleServer{}); err != nil {
panic(err)
}
if err := srv.Serve(); err != nil {
logger.Error(err)
}
}
```
实现 **GreetService** 接口并完成 **server.go**。具体代码请参考[**这里**](https://github.com/apache/dubbo-go-samples/blob/new-triple-samples/helloworld/server.go).

## 编译并运行

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

```shell
cd ~/triple_helloworld/go-client/cmd
go build -o client .
cd ~/triple_helloworld
go build -o client ./client.go
./client
```
File renamed without changes.
31 changes: 0 additions & 31 deletions helloworld/go-server/handler/handler.go

This file was deleted.

13 changes: 11 additions & 2 deletions helloworld/go-server/cmd/server.go → helloworld/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@
package main

import (
"context"
_ "dubbo.apache.org/dubbo-go/v3/imports"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/server"
"github.com/apache/dubbo-go-samples/helloworld/go-server/handler"
greet "github.com/apache/dubbo-go-samples/helloworld/proto"
"github.com/apache/dubbo-go-samples/helloworld/proto/greettriple"
"github.com/dubbogo/gost/log/logger"
)

type GreetTripleServer struct {
}

func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
resp := &greet.GreetResponse{Greeting: req.Name}
return resp, nil
}

func main() {
srv, err := server.NewServer(
server.WithServerProtocol(
Expand All @@ -35,7 +44,7 @@ func main() {
if err != nil {
panic(err)
}
if err := greettriple.RegisterGreetServiceHandler(srv, &handler.GreetTripleServer{}); err != nil {
if err := greettriple.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
panic(err)
}
if err := srv.Serve(); err != nil {
Expand Down
Loading

0 comments on commit 0ecee7b

Please sign in to comment.