使用 Nacos 作为注册中心

使用 Nacos 作为注册中心

1. 准备工作

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

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

2.1 开启服务端

示例:user.go:

  1. func (u *UserProvider) GetUser(ctx context.Context, userStruct *CallUserStruct) (*User, error) {
  2. fmt.Printf("=======================\nreq:%#v\n", userStruct)
  3. rsp := User{"A002", "Alex Stocks", 18, userStruct.SubInfo}
  4. fmt.Printf("=======================\nrsp:%#v\n", rsp)
  5. return &rsp, nil
  6. }

服务端开启一个服务,名为GetUser,传入一个CallUserStruct的参数,返回一个User参数
CallUserStruct参数定义:

  1. type CallUserStruct struct {
  2. ID string
  3. Male bool
  4. SubInfo SubInfo // 嵌套子结构
  5. }
  6. func (cs CallUserStruct) JavaClassName() string {
  7. return "com.ikurento.user.CallUserStruct"
  8. }
  9. type SubInfo struct {
  10. SubID string
  11. SubMale bool
  12. SubAge int
  13. }
  14. func (s SubInfo) JavaClassName() string {
  15. return "com.ikurento.user.SubInfo"
  16. }

User结构定义:

  1. type User struct {
  2. Id string
  3. Name string
  4. Age int32
  5. SubInfo SubInfo // 嵌套上述子结构SubInfo
  6. }
  7. func (u *User) JavaClassName() string {
  8. return "com.ikurento.user.User"
  9. }

开启服务:

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:

  1. {
  2. "ID": "string@A000",
  3. "Male": "bool@true",
  4. "SubInfo": {
  5. "SubID": "string@A001",
  6. "SubMale": "bool@false",
  7. "SubAge": "int@18",
  8. "JavaClassName":"string@com.ikurento.user.SubInfo"
  9. },
  10. "JavaClassName": "string@com.ikurento.user.CallUserStruct"
  11. }

userCall.json将参数CallUserStruct的结构及子结构SubInfo都定义了出来,并且给请求参数赋值。

user.json 同理,作为返回值不需要赋初始值,但JavaClassName字段一定与server端严格对应

  1. {
  2. "ID": "string",
  3. "Name": "string",
  4. "Age": "int",
  5. "JavaClassName": "string@com.ikurento.user.User",
  6. "SubInfo": {
  7. "SubID": "string",
  8. "SubMale": "bool",
  9. "SubAge": "int",
  10. "JavaClassName":"string@com.ikurento.user.SubInfo"
  11. }
  12. }

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端打印结果:

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

=======================

req:&main.CallUserStruct{ID:”A000”, Male:true, SubInfo:main.SubInfo{SubID:”A001”, SubMale:false, SubAge:18}}

```

可见接收到了来自cli的数据

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)