Gin多个Server

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"golang.org/x/sync/errgroup"
)

var (
	g errgroup.Group
)

func router01() http.Handler {
	e := gin.New()
	e.Use(gin.Recovery())
	e.GET("/", func(c *gin.Context) {
		c.String(200, "New New New Server>>111111")
	})

	return e
}

func router02() http.Handler {
	e := gin.New()
	e.Use(gin.Recovery())
	e.GET("/", func(c *gin.Context) {
		c.String(200, "Old Old Old Server<<2222222")
	})

	return e
}

func main() {
	server01 := &http.Server{
		Addr:         ":8181",
		Handler:      router01(),
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	server02 := &http.Server{
		Addr:         ":8282",
		Handler:      router02(),
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	g.Go(func() error {
		return server01.ListenAndServe()
	})

	g.Go(func() error {
		return server02.ListenAndServe()
	})

	if err := g.Wait(); err != nil {
		log.Fatal(err)
	}

}
编译
//设置国内代理
go env -w GOPROXY=https://goproxy.cn,direct
go get -u golang.org/x/sync/errgroup
go build weball.go
//生成mod
go mod init example.com/m
go mod tidy

Dockerfile

tee Dockerfile <<-'EOF'
FROM alpine:latest
ENV TZ='Asia/Shanghai'
ENV TIMEZONE Asia/Shanghai
RUN  mkdir /lib64  && \
     ln -s  /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

WORKDIR /data
ADD weball /data/weball
EXPOSE 8181 
EXPOSE 8282
CMD ["./weball"]
EOF

YAML

tee k8s.yaml <<-'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: weball-v1
  labels:
    app: weball
spec:
  replicas: 1
  selector:
    matchLabels:
      app: weball
  template:
    metadata:
      labels:
        app: weball
    spec:
      containers:
      - name: weball
        image: registry.cn-shenzhen.aliyuncs.com/lolos/loloss:weball-00
        ports:
        - containerPort: 8181
---
apiVersion: v1
kind: Service
metadata:
  name: weball-v1
  labels:
    app: weball
spec:
  type: NodePort
  ports:
    - name: old
      port: 8181
      targetPort: 8181
      nodePort: 28181
    - name: new
      port: 8282
      targetPort: 8282
      nodePort: 28282
  selector:
    app: weball
EOF
已完成的镜像
docker pull registry.cn-shenzhen.aliyuncs.com/lolos/loloss:weball-00

测试

[root@dockser tmp]# curl 192.168.8.62:28282
Old Old Old Server<<2222222[root@dockser tmp]# curl 192.168.8.62:28282
Old Old Old Server<<2222222[root@dockser tmp]# curl 192.168.8.62:28282
Old Old Old Server<<2222222[root@dockser tmp]# 
[root@dockser tmp]# curl 192.168.8.62:28181
New New New Server>>111111[root@dockser tmp]# curl 192.168.8.62:28181
New New New Server>>111111[root@dockser tmp]# curl 192.168.8.62:28181
New New New Server>>111111[root@dockser tmp]# 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值