1

Excuse me.I've learned of go-micro and know micro service client has no need to know which ip and port the micro service is deployed at.When a service runs itself, the port is randomly set, and the service is located by its service name,like go.micro.api.user.

The service side runs like:

     userService := micro.NewService(micro.Name("go.micro.api.user"))
     userService.Init()
     user.RegisterUserHandler(userService.Server(), new(User), api.WithEndpoint(&api.Endpoint{
          Name: "User.GetToken",
          Path: []string{"/user/token/"},
          Method: []string{"POST"},
          Handler: http.Handler,
     }),api.WithEndpoint(&api.Endpoint{
          Name: "User.GetUserName",
          Path: []string{"/user/username/"},
          Method: []string{"GET"},
          Handler: http.Handler,
     }))

    if er := userService.Run(); er != nil {
        panic(er)
    }

the client side like:

    service := micro.NewService(micro.Name("go.micro.api.user"))
    service.Init()

    // Create new greeter client
    userService := user.NewUserService("go.micro.api.user", service.Client())

    // Call the greeter
    rsp, err := userService.GetToken(context.TODO(), &user.Request{Username: "John", Password: "123"})
    if err != nil {
        fmt.Println(err)
        return
    }

    // Print response
    fmt.Println(rsp)

To deploy micro services in docker, I don't know how to config port in a docker container.

fwhez
  • 561
  • 4
  • 10

1 Answers1

1

You can set the port for go-micro servers using the server_address flag (see their cli package) or the MICRO_SERVER_ADDRESS env var (see cmd doc).

Once you do that, I suggest you experiment connecting to your service without Docker first. Once it all works, you can run it with Docker, using the -p flag.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412