Using Nacos as a Registry

Using Nacos as a Registry

1. Preparation

  • The dubbo-go CLI tool and dependencies are installed
  • Create a new demo application

2. Using grpc_cli Tool for Dubbo Service Debugging

2.1 Start the Server

Example: 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. }

The server exposes a service named GetUser, taking a CallUserStruct parameter and returning a User parameter.
Definition of CallUserStruct parameter:

  1. type CallUserStruct struct {
  2. ID string
  3. Male bool
  4. SubInfo SubInfo // Nested substructure
  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. }

Definition of User structure:

  1. type User struct {
  2. Id string
  3. Name string
  4. Age int32
  5. SubInfo SubInfo // Nesting the above substructure SubInfo
  6. }
  7. func (u *User) JavaClassName() string {
  8. return "com.ikurento.user.User"
  9. }

Start the service:

cd server
source builddev.sh
go run .

2.2 Define the Request Body (Packing/Unpacking Protocol)

The request body is defined as a JSON file, with the convention that all keys are strings.
Keys correspond to Go struct field names such as “ID”, “Name”, and values correspond to “type@val”.
The types supported are string, int, bool, time, with val initialized as a string. If only the type is provided, it is initialized to zero value.
It is required for each struct to have a JavaClassName field, which must strictly correspond with the server side.

See 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 defines the structure of CallUserStruct and its substructure SubInfo, and assigns values to the request parameters.

Similarly for user.json, no initial values are needed for return values, but the JavaClassName field must strictly correspond to the server side.

  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 Execute Request

dubbogo-cli call --h=localhost --p 20001 --proto=dubbo --i=com.ikurento.user.UserProvider --method=GetUser --sendObj="./userCall.json" --recvObj="./user.json"

CLI prints the result:

  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. You can see detailed request body assignment, return results, and elapsed time. Nested structures are supported.
  2. Server logs:

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

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

```

It shows that data from CLI has been received.

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)