Get net/http: TLS handshake timeout when run golang app on docker container host

huangapple go评论88阅读模式
英文:

Get net/http: TLS handshake timeout when run golang app on docker container host

问题

这是我的代码:

func Login(w http.ResponseWriter, r *http.Request) {
    fmt.Println("Login prccessing")
    email := r.FormValue("email")
    password := r.FormValue("password")
    fmt.Println(email + password)
    var netTransport = &http.Transport{
        Dial: (&net.Dialer{
            Timeout: 50 * time.Second,
        }).Dial,
        TLSHandshakeTimeout: 50 * time.Second,
    }
    var netClient = &http.Client{
        Timeout:   time.Second * 50,
        Transport: netTransport,
    }
    res, err := netClient.Get("https://account.sloppy.zone/accounts/" + email)
    if err != nil {
        fmt.Println(err.Error())
        http.Redirect(w, r, "/", http.StatusSeeOther)
    }

    responeData, errs := ioutil.ReadAll(res.Body)
    fmt.Println(string(responeData))
    if errs != nil {
        log.Fatal(errs)
    }
    var info LoginInfo
    json.Unmarshal(responeData, &info)
    if email == info.Id && password == info.Password {
        http.Redirect(w, r, "/manager", http.StatusSeeOther)
    } else {
        http.Redirect(w, r, "/", http.StatusSeeOther)
    }
}

我正在尝试使用Docker容器化运行服务,并在Docker容器主机(sloppy.io)上部署它,该服务调用另一个服务(账户服务)从该服务获取账户和密码,并与用户输入进行验证。问题是当我登录到我的页面时,出现502 Bad Gateway错误。查看日志后,我发现错误是TLS握手超时。

你可以如何解决这个问题?

英文:

This is my code:

func Login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("Login prccessing")
	email := r.FormValue("email")
	password := r.FormValue("password")
	fmt.Println(email + password)
	var netTransport = &http.Transport{
		Dial: (&net.Dialer{
			Timeout: 50 * time.Second,
		}).Dial,
		TLSHandshakeTimeout: 50 * time.Second,
	}
	var netClient = &http.Client{
		Timeout:   time.Second * 50,
		Transport: netTransport,
	}
	res, err := netClient.Get("https://account.sloppy.zone/accounts/" + email)
	if err != nil {
		fmt.Println(err.Error())
		http.Redirect(w, r, "/", http.StatusSeeOther)
	}

	responeData, errs := ioutil.ReadAll(res.Body)
	fmt.Println(string(responeData))
	if errs != nil {
		log.Fatal(errs)
	}
	var info LoginInfo
	json.Unmarshal(responeData, &info)
	if email == info.Id && password == info.Password {
		http.Redirect(w, r, "/manager", http.StatusSeeOther)
	} else {
		http.Redirect(w, r, "/", http.StatusSeeOther)
	}
}

I'm trying to run service containerized with docker and deploy it on a docker container host (sloppy.io) and this service call another service (account service) to get account and password from that service and verify them with user input at the same host.The problem is when I login to my page then 502 Bad Gateway show up.Track to log I see error:TLS handshake timeout.

How can I solve this?

答案1

得分: 1

由于您的golang应用程序依赖于账户服务,您应该在设置中定义这个依赖关系,并使用Docker的网络功能来连接这两个服务。这样做还可以使您能够在Docker Swarm或其他可扩展的集群基础设施(如sloppy.io)中运行任意数量的实例。

这样做的好处还在于您不需要将后端(账户服务)暴露给公众。

您可以使用docker-compose或sloppy.io的命令行界面来构建和运行应用程序。Sloppy配置文件与docker-compose.yml文件非常相似。请参阅http://kb.sloppy.io/features/connecting-containers

以下是sloppy.yml的示例配置:

version: "v1"
project: "myproject"
services:
  frontend:
    golang_service:
      dependencies:
        - "../backend/account_service"
      domain: "//my_golang_service.sloppy.zone"
      env:
        - ACCOUNT_HOST: "account_service.backend.myproject"
      image: "dockerhub_username/golang_service:1.0.0"
      port: 80
  backend:
    account_service:
      image: "dockerhub_username/account_service:1.0.0"

这只涵盖了基本配置。您可能还想添加卷和环境变量。如果您已经有一个现有的docker-compose.yml文件,您可以使用sloppose(github.com/sloppyio/sloppose)将其转换为sloppy.yml。

英文:

As your golang application depends on the account service, you should define this dependency in your setup and use docker’s networking features to connect both services. Doing so will also give you the ability to run an arbitrary number of instances as a docker swarm or inside another scalable cluster infrastructure like sloppy.io.

This also has the advantage that you do not need to expose your backend (account service) to the public.

You can either use docker-compose or sloppy.io's command line interface to build and run your application. Sloppy configuration files are quite similar to docker-compose.yml files. See http://kb.sloppy.io/features/connecting-containers

Here is an example configuration as sloppy.yml

version: "v1"
project: "myproject"
services:
  frontend:
    golang_service:
      dependencies:
        - "../backend/account_service"
      domain: "//my_golang_service.sloppy.zone"
      env:
        - ACCOUNT_HOST: "account_service.backend.myproject"
      image: "dockerhub_username/golang_service:1.0.0"
      port: 80
  backend:
    account_service:
      image: "dockerhub_username/account_service:1.0.0"

This just covers the basic configuration. You will probably want to add volumes and environment variables. If you already have an existing docker-compose.yml, you can convert it to sloppy.yml using sloppose (github.com/sloppyio/sloppose).

答案2

得分: -1

这两个服务都在sloppy.io上运行吗?

英文:

are both services running on sloppy.io?

huangapple
  • 本文由 发表于 2017年8月21日 04:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/45786562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定