dpjhq00684 2016-04-26 18:45
浏览 47

通过TCP进行负载平衡并禁用Go服务器上的HTTP连接重用

I am trying to find a way to load balance incoming requests on my Go servers. I have tried nginx, haproxy and some Go repositories on GitHub like:

https://github.com/darkhelmet/balance

that allows you to do layer 4 load balancing through TCP.

I have tried TCP load balancing through haproxy and through the darkhelmet/balance library. It seems like when I do load balancing through TCP, my connections are re used so every request from my PC gets sent to the same server. So it becomes hard for me to do benchmarks with for example the "wrk" benchmarking tool. And also other tests when I need to see if websocket connections on different go apps can communicate through a redis server via pub sub methods and so on.

When I refresh the web browser I can see that all requests are sent to the same server, even tho I have 3 Go servers in the backend.

Someone said this to me: "Disable HTTP connection re-use on your web servers." But I do not know how to do this.

The code I use for my go servers to benchmark is this:

package main

import (
    "fmt"
    "log"
    "net/http"
    "strconv"

    "github.com/julienschmidt/httprouter"
)

func fib(n int) int {
    if n == 0 {
        return 0
    } else if n == 1 {
        return 1
    } else {
        return fib(n-1) + fib(n-2)
    }
}

func main() {
    router := httprouter.New()
    router.GET("/", toHandle(root))
    router.GET("/fibonacci/:n", toHandle(fibonacci))
    log.Fatal(http.ListenAndServe(":80", router))
}

type errHandle func(http.ResponseWriter, *http.Request, httprouter.Params) error

func toHandle(eh errHandle) httprouter.Handle {
    return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
        if err := eh(w, r, ps); err != nil {
            log.Println(err)
        }
    }
}

func root(w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
    w.Write([]byte("To the moon! goapp1"))
    return nil
}

func fibonacci(w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
    n, err := strconv.Atoi(ps.ByName("n"))
    if err != nil {
        return err
    }
    result := fib(n)
    w.Write([]byte(fmt.Sprintf("result: %d", result)))
    return nil
}

How can I achieve this? To be able to do roundrobin from the same client/ip/machine through tcp load balancing?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题