example: sessions: scale out using redis

Former-commit-id: a811648e0bdf83c289656426f8ba67d785b7d5cc
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-19 12:55:48 +03:00
parent 3faaf954bb
commit 302597faac
4 changed files with 66 additions and 2 deletions

View File

@ -0,0 +1,16 @@
FROM golang:latest AS builder
RUN apt-get update
WORKDIR /go/src/app
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Caching go modules and build the binary.
COPY go.mod .
RUN go mod download
COPY . .
RUN go install
FROM scratch
COPY --from=builder /go/bin/app .
ENTRYPOINT ["./app"]

View File

@ -0,0 +1,24 @@
# docker-compose up [--build]
version: '3'
services:
redis-server:
image: redis
app1:
build: .
depends_on:
- redis-server
ports:
- 8080:8080
environment:
- PORT=8080
- REDIS_ADDR=redis-server:6379
app2:
build: .
depends_on:
- redis-server
ports:
- 9090:9090
environment:
- PORT=9090
- REDIS_ADDR=redis-server:6379

View File

@ -0,0 +1,6 @@
module app
go 1.14
require github.com/kataras/iris/v12 v12.1.9-0.20200719040914-c21125c094d6

View File

@ -1,6 +1,9 @@
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/kataras/iris/v12"
@ -18,7 +21,7 @@ func main() {
// you can replace them based on your running redis' server settings:
db := redis.New(redis.Config{
Network: "tcp",
Addr: "127.0.0.1:6379",
Addr: getenv("REDIS_ADDR", "127.0.0.1:6379"),
Timeout: time.Duration(30) * time.Second,
MaxActive: 10,
Password: "",
@ -55,5 +58,20 @@ func main() {
sess.UseDatabase(db)
app := example.NewApp(sess)
app.Listen(":8080")
// TIP scaling-out Iris sessions using redis:
// $ docker-compose up
// http://localhost:8080/set/$key/$value
// The value will be available on all Iris servers as well.
// E.g. http://localhost:9090/get/$key and vice versa.
addr := fmt.Sprintf(":%s", getenv("PORT", "8080"))
app.Listen(addr)
}
func getenv(key string, def string) string {
if v := os.Getenv(strings.ToUpper(key)); v != "" {
return v
}
return def
}