Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# LoadBalancerDemo
![]("githubimg.png")
# LoadBalancerDemo

![Load Balancer Architecture](githubimg.png)

## Overview
This project demonstrates a simple load balancing setup using Nginx as a reverse proxy to distribute traffic across multiple Go service instances.

## Architecture
- **Nginx**: Acts as a load balancer, distributing incoming requests across three Go service instances
- **Go Services**: Three identical service instances that handle HTTP requests
- **Docker**: Everything is containerized using Docker and orchestrated with Docker Compose

## Endpoints
- `/`: Welcome page showing server ID
- `/getResult`: Returns a message with the current server ID

## Setup and Running
1. Make sure you have Docker and Docker Compose installed
2. Clone this repository
3. Run the following command:
```bash
docker-compose up --build -d
```

## Project Structure
- `nginx/`: Contains Nginx configuration and Dockerfile
- `golangService/`: Contains Go service code and Dockerfile
- `docker-compose.yml`: Defines the multi-container application setup
14 changes: 12 additions & 2 deletions golangService/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ import (
"os"
)

func Handler(w http.ResponseWriter, r *http.Request) {
func getResultHandler(w http.ResponseWriter, r *http.Request) {
ServerID := os.Getenv("SERVERID")
io.WriteString(w, "msg from server"+ServerID)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
ServerID := os.Getenv("SERVERID")
io.WriteString(w, "Welcome to server "+ServerID)
}

func main() {
http.HandleFunc("/getResult", Handler)
http.HandleFunc("/", rootHandler)
http.HandleFunc("/getResult", getResultHandler)

if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
Expand Down