drws65968272 2019-02-21 07:11
浏览 194

如何为微服务应用程序配置docker的公开端口?

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.

  • 写回答

1条回答 默认 最新

  • douqulv6059 2019-02-21 13:38
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!