Skip to content

Commit

Permalink
Update go doc (#2952)
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj authored Apr 5, 2024
1 parent ee23846 commit 536b2ed
Show file tree
Hide file tree
Showing 31 changed files with 738 additions and 1,792 deletions.
46 changes: 25 additions & 21 deletions content/zh-cn/overview/mannual/golang-sdk/quickstart/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,28 @@ protoc --go_out=. --go_opt=paths=source_relative \
运行以上命令后,在目标目录中看到以下生成的文件:

```
proto
└── greet
└── v1
├── greet.pb.go
└── greetv1triple
└── greet.triple.go
proto
├── greet.pb.go
├── greet.proto
└── greet.triple.go
```
在 proto/greet/v1 包下有两部分内容:
- `greet.pb.go` 是由谷歌标准的 `protoc-gen-go`生成,它包含 `GreetRequest`、`GreetResponse` 结构体和响应的编解码规则。
- `greetv1triple` 包下的文件`reet.triple.go`是由 Dubbo 自定义的插件`protoc-gen-triple-go`成,其中关键的信息包括生成的接口 `GreeterClient`、构造器等。
- `greet.triple.go` 是由 Dubbo 自定义的插件`protoc-gen-go-triple`成,其中关键的信息包括生成的接口 `GreetService`、构造器等。
### 实现服务
接下来我们就需要添加业务逻辑了,实现 `greetv1triple.GreeterClient` 接口即可。
接下来我们就需要添加业务逻辑了,实现 `greet.GreetService` 接口即可。
```go
type GreeterServer struct {
greet.UnimplementedGreeterServer
type GreetTripleServer struct {
}
func (s *GreeterServer) SayHello(ctx context.Context, in *greet.HelloRequest) (*greet.User, error) {
return &greet.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, nil
func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
resp := &greet.GreetResponse{Greeting: req.Name}
return resp, nil
}
```

Expand All @@ -160,18 +158,20 @@ func (s *GreeterServer) SayHello(ctx context.Context, in *greet.HelloRequest) (*
func main() {
srv, err := server.NewServer(
server.WithServerProtocol(
protocol.WithTriple(),
protocol.WithPort(20000),
protocol.WithTriple(),
),
)
if err != nil {
panic(err)
}
if err := greetv1triple.RegisterGreetServiceHandler(srv, &api.GreetTripleServer{}); err != nil {

if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
panic(err)
}

if err := srv.Serve(); err != nil {
panic(err)
logger.Error(err)
}
}
```
Expand All @@ -183,26 +183,30 @@ func main() {
curl \
--header "Content-Type: application/json" \
--data '{"name": "Dubbo"}' \
http://localhost:50051/org.apache.dubbo.demo.DemoService/sayHello
http://localhost:20000/greet.GreetService/Greet
```

也可以使用 Dubbo client 请求服务,我们首先需要从生成代码即 `greetv1triple` 包中获取服务代理,为它指定 server 地址并初始化,之后就可以发起 RPC 调用了。
也可以使用 Dubbo client 请求服务,我们首先需要从生成代码即 `greet` 包中获取服务代理,为它指定 server 地址并初始化,之后就可以发起 RPC 调用了。

```go
func main() {
// for the most brief RPC case
cli, err := client.NewClient(
client.WithURL("tri://127.0.0.1:20000"),
client.WithClientURL("127.0.0.1:20000"),
)
if err != nil {
panic(err)
}
svc, err := greettriple.NewGreetService(cli)

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

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

Expand Down
147 changes: 147 additions & 0 deletions content/zh-cn/overview/mannual/golang-sdk/refer/nacos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
aliases:
- /zh/docs3-v2/golang-sdk/tutorial/develop/registry/nacos/
- /zh-cn/docs3-v2/golang-sdk/tutorial/develop/registry/nacos/
description: 使用 Nacos 作为注册中心
title: 使用 Nacos 作为注册中心
type: docs
weight: 10
---



## 1. 准备工作

- dubbo-go cli 工具和依赖工具已安装
- 创建一个新的 demo 应用

## 2. 使用 grpc_cli 工具进行 Dubbo 服务调试

### 2.1 开启服务端
示例:user.go:
```go
func (u *UserProvider) GetUser(ctx context.Context, userStruct *CallUserStruct) (*User, error) {
fmt.Printf("=======================\nreq:%#v\n", userStruct)
rsp := User{"A002", "Alex Stocks", 18, userStruct.SubInfo}
fmt.Printf("=======================\nrsp:%#v\n", rsp)
return &rsp, nil
}

```
服务端开启一个服务,名为GetUser,传入一个CallUserStruct的参数,返回一个User参数\
CallUserStruct参数定义:
```go
type CallUserStruct struct {
ID string
Male bool
SubInfo SubInfo // 嵌套子结构
}
func (cs CallUserStruct) JavaClassName() string {
return "com.ikurento.user.CallUserStruct"
}

type SubInfo struct {
SubID string
SubMale bool
SubAge int
}

func (s SubInfo) JavaClassName() string {
return "com.ikurento.user.SubInfo"
}

```
User结构定义:
```go
type User struct {
Id string
Name string
Age int32
SubInfo SubInfo // 嵌套上述子结构SubInfo
}

func (u *User) JavaClassName() string {
return "com.ikurento.user.User"
}
```

开启服务:

`cd server `\
`source builddev.sh`\
`go run .`

### 2.2 定义请求体(打解包协议)

请求体定义为json文件,约定键值均为string\
键对应go语言struct字段名例如"ID"、"Name" ,值对应"type@val"\
其中type支持string int bool time,val使用string 来初始化,如果只填写type则初始化为零值。
约定每个struct必须有JavaClassName字段,务必与server端严格对应

见userCall.json:
```json
{
"ID": "string@A000",
"Male": "bool@true",
"SubInfo": {
"SubID": "string@A001",
"SubMale": "bool@false",
"SubAge": "int@18",
"JavaClassName":"[email protected]"
},
"JavaClassName": "[email protected]"
}
```
userCall.json将参数CallUserStruct的结构及子结构SubInfo都定义了出来,并且给请求参数赋值。

user.json 同理,作为返回值不需要赋初始值,但JavaClassName字段一定与server端严格对应
```go
{
"ID": "string",
"Name": "string",
"Age": "int",
"JavaClassName": "[email protected]",
"SubInfo": {
"SubID": "string",
"SubMale": "bool",
"SubAge": "int",
"JavaClassName":"[email protected]"
}
}
```

### 2.3 执行请求
`dubbogo-cli call --h=localhost --p 20001 --proto=dubbo --i=com.ikurento.user.UserProvider --method=GetUser --sendObj="./userCall.json" --recvObj="./user.json"`

cli端打印结果:
```log
2020/10/26 20:47:45 Created pkg:
2020/10/26 20:47:45 &{ID:A000 Male:true SubInfo:0xc00006ea20 JavaClassName:com.ikurento.user.CallUserStruct}
2020/10/26 20:47:45 SubInfo:
2020/10/26 20:47:45 &{SubID:A001 SubMale:false SubAge:18 JavaClassName:com.ikurento.user.SubInfo}
2020/10/26 20:47:45 Created pkg:
2020/10/26 20:47:45 &{ID: Name: Age:0 JavaClassName:com.ikurento.user.User SubInfo:0xc00006ec90}
2020/10/26 20:47:45 SubInfo:
2020/10/26 20:47:45 &{SubID: SubMale:false SubAge:0 JavaClassName:com.ikurento.user.SubInfo}
2020/10/26 20:47:45 connected to localhost:20001!
2020/10/26 20:47:45 try calling interface:com.ikurento.user.UserProvider.GetUser
2020/10/26 20:47:45 with protocol:dubbo
2020/10/26 20:47:45 After 3ms , Got Rsp:
2020/10/26 20:47:45 &{ID:A002 Name:Alex Stocks Age:18 JavaClassName: SubInfo:0xc0001241b0}
2020/10/26 20:47:45 SubInfo:
2020/10/26 20:47:45 &{SubID:A001 SubMale:false SubAge:18 JavaClassName:}```
```
可看到详细的请求体赋值情况,以及返回结果和耗时。支持嵌套结构

server端打印结果
```
=======================
req:&main.CallUserStruct{ID:"A000", Male:true, SubInfo:main.SubInfo{SubID:"A001", SubMale:false, SubAge:18}}
=======================
```
可见接收到了来自cli的数据

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 536b2ed

Please sign in to comment.