ロードバランシング
このレシピでは、Nginx をリバースプロキシサーバーとして使い、複数の Echo サーバー間で トラフィックをロードバランスする方法を示します。
package main
import ( "context" "fmt" "net/http" "os"
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5/middleware")
var index = ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Upstream Server</title> <style> h1, p { font-weight: 300; } </style> </head> <body> <p> Hello from upstream server %s </p> </body> </html>`
func main() { name := os.Args[1] port := os.Args[2]
e := echo.New() e.Use(middleware.Recover()) e.Use(middleware.RequestLogger())
e.GET("/", func(c *echo.Context) error { return c.HTML(http.StatusOK, fmt.Sprintf(index, name)) })
sc := echo.StartConfig{Address: port} if err := sc.Start(context.Background(), e); err != nil { e.Logger.Error("failed to start server", "error", err) }}サーバーを起動する
Section titled “サーバーを起動する”cd upstreamgo run server.go server1 :8081go run server.go server2 :80821) Nginx をインストールする
Section titled “1) Nginx をインストールする”Nginx installation guide を参照してください。
2) Nginx を設定する
Section titled “2) Nginx を設定する”次の内容で /etc/nginx/sites-enabled/localhost ファイルを作成します。
upstream localhost { server localhost:8081; server localhost:8082;}
server { listen 8080; server_name localhost; access_log /var/log/nginx/localhost.access.log combined;
location / { proxy_pass http://localhost; }}3) Nginx を再起動する
Section titled “3) Nginx を再起動する”service nginx restarthttps://localhost:8080 にアクセスすると、“server 1” または “server 2” から配信された Web ページが表示されます。
Hello from upstream server server1