add deployments scripts

This commit is contained in:
zhuyasen 2022-09-27 22:30:59 +08:00
parent e16d0ac3de
commit 903c33447e
11 changed files with 237 additions and 32 deletions

View File

@ -14,6 +14,7 @@ run:
skip-dirs:
- pkg
- docs
- api
# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize

View File

@ -4,10 +4,11 @@ PROJECT_NAME := "github.com/zhufuyi/sponge"
PKG := "$(PROJECT_NAME)"
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
PROJECT_FILES := $(shell ls)
.PHONY: init
# init env
# installation of dependent tools
init:
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0
@ -24,81 +25,93 @@ init:
.PHONY: ci-lint
# make ci-lint
# check the code specification against the rules in the .golangci.yml file
ci-lint:
golangci-lint run ./...
.PHONY: build
# make build, Build the binary file
# go build the linux amd64 binary file
build:
@cd cmd/sponge && go build
@cd cmd/sponge && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l"
@echo "build finished, binary file in path 'cmd/sponge'"
.PHONY: run
# make run, run app
# run server
run:
@bash scripts/run.sh
.PHONY: dep
# make dep Get the dependencies
# download dependencies to the directory vendor
dep:
@go mod download
.PHONY: fmt
# make fmt
# go format *.go files
fmt:
@gofmt -s -w .
.PHONY: test
# make test
# go test *_test.go files
test:
go test -short ${PKG_LIST}
.PHONY: cover
# make cover
# generate test coverage
cover:
go test -short -coverprofile coverage.out -covermode=atomic ${PKG_LIST}
go tool cover -html=coverage.out
go test -short -coverprofile cover.out -covermode=atomic ${PKG_LIST}
go tool cover -html=cover.out
.PHONY: docker
# generate docker image
# build docker image
docker:
docker build -t sponge:latest -f build/Dockerfile
@tar zcf sponge.tar.gz ${PROJECT_FILES}
@mv -f sponge.tar.gz build/
docker build -t sponge/sponge:latest build/
@rm -rf build/sponge.tar.gz
.PHONY: docker-image
# copy the binary file to build the docker image, skip the compile to binary in docker
docker-image: build
@bash scripts/grpc_health_probe.sh
@mv -f cmd/sponge/sponge build/ && cp -f /tmp/grpc_health_probe build/
@mkdir -p build/config && cp -f config/conf.yml build/config/
docker build -f build/Dockerfile_sponge -t sponge/sponge:latest build/
@rm -rf build/sponge build/config/ build/grpc_health_probe
.PHONY: clean
# make clean
# clean binary file, cover.out, redundant dependency packages
clean:
@-rm -vrf sponge
@-rm -vrf cover.out
@-rm -vrf coverage.txt
@rm -vrf cmd/sponge/sponge
@rm -vrf cover.out
@go mod tidy
@echo "clean finished"
.PHONY: docs
# gen swagger doc
# generate swagger doc
docs:
@swag init -g cmd/sponge/main.go
@echo "see docs by: http://localhost:8080/swagger/index.html"
.PHONY: graph
# make graph 生成交互式的可视化Go程序调用图生成完毕后会在浏览器自动打开
# generate interactive visual function dependency graphs
graph:
@echo "generating graph ......"
@go-callvis github.com/zhufuyi/sponge
.PHONY: mockgen
# make mockgen gen mock file
# mockgen gen mock file
mockgen:
cd ./internal && for file in `egrep -rnl "type.*?interface" ./repository | grep -v "_test" `; do \
echo $$file ; \
@ -107,13 +120,13 @@ mockgen:
.PHONY: proto
# generate proto struct only
# generate *.pb.go codes from *.proto files
proto:
@bash scripts/protoc.sh
.PHONY: proto-doc
# generate proto doc
# generate doc from *.proto files
proto-doc:
@bash scripts/proto-doc.sh

View File

@ -37,7 +37,7 @@ sponge 是一个微服务框架支持http和grpc及服务治理结合[goct
│ ├── handler # http的业务功能实现
│ ├── model # 数据库 model
│ ├── routers # http 路由
│ ├── server # 服务入口包括http和grpc服务入口
│ ├── server # 服务入口包括http和grpc服务
│ └── service # grpc的业务功能实现
├── pkg # 外部应用程序可以使用的库代码
├── scripts # 存放用于执行各种构建,安装,分析等操作的脚本

View File

@ -14,7 +14,7 @@ RUN cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 \
&& go mod download \
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /grpc_health_probe
# 通过二进制文件构建镜像
# 使用二进制文件构建镜像
FROM alpine:latest
MAINTAINER zhufuyi "g.zhufuyi@gmail.com"
@ -24,7 +24,7 @@ RUN apk add tzdata \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
# 添加curl用在http服务的检查
# 添加curl用在http服务的检查如果用部署在k8s可以不用安装
RUN apk add curl
# 添加grpc_health_probe用在grpc服务的健康检查
@ -35,3 +35,4 @@ COPY --from=build /go/src/sponge/config/conf.yml /app/config/conf.yml
WORKDIR /app
CMD ["./sponge", "-c", "config/conf.yml"]

View File

@ -1,6 +1,5 @@
# 需要先在本地编译好二进制文件和配置文件复制到和Dokerfile在同一个目录下
FROM alpine:latest
MAINTAINER zhufuyi "g.zhufuyi@gmail.com"
# 设置时区为上海
@ -19,5 +18,6 @@ RUN chmod +x /bin/grpc_health_probe
COPY config/ /app/config/
COPY sponge /app/sponge
RUN chmod +x /app/sponge
WORKDIR /app
CMD ["./sponge", "-c", "config/conf.yml"]

View File

@ -14,8 +14,8 @@ services:
volumes:
- $PWD/config:/app/config
healthcheck:
#test: ["CMD", "curl", "-f", "http://localhost:8080/health"] # http健康检查镜像里必须又curl命令
test: ["CMD", "grpc_health_probe", "-addr=localhost:9090"] # grpc健康检查镜像必须grpc_health_probe命令
#test: ["CMD", "curl", "-f", "http://localhost:8080/health"] # http健康检查镜像必须包含curl命令
test: ["CMD", "grpc_health_probe", "-addr=localhost:9090"] # grpc健康检查镜像必须包含grpc_health_probe命令
interval: 30s # 间隔时间
timeout: 5s # 超时时间
retries: 3 # 重试次数

View File

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: sponge

View File

@ -0,0 +1,96 @@
kind: ConfigMap
apiVersion: v1
metadata:
name: sponge-config
namespace: sponge
data:
conf.yml: |-
# app 设置
app:
name: "userExample" # 服务名称
env: "dev" # 运行环境dev:开发环境prod:生产环境pre:预生产环境
version: "v0.0.0" # 版本
host: "127.0.0.1" # 主机名称或ip
enableProfile: false # 是否开启性能分析功能true:开启false:关闭
enableMetrics: true # 是否开启指标采集true:开启false:关闭
enableLimit: false # 是否开启限流true:开启false:关闭
enableTracing: false # 是否开启链路跟踪true:开启false:关闭
enableRegistryDiscovery: false # 是否开启注册与发现true:开启false:关闭
# http 设置
http:
port: 8080 # 监听端口
readTimeout: 3 # 读超时,单位(秒)
writeTimeout: 90 # 写超时,单位(秒)如果enableProfile为true需要大于60spprof做profiling的默认值是60s
# grpc 服务设置
grpc:
port: 9090 # 监听端口
readTimeout: 3 # 读超时,单位(秒)
writeTimeout: 3 # 写超时,单位(秒)
# logger 设置
logger:
level: "info" # 输出日志级别 debug, info, warn, error默认是debug
format: "console" # 输出格式console或json默认是console
isSave: false # false:输出到终端true:输出到文件默认是false
logFileConfig: # isSave=true时有效
filename: "out.log" # 文件名称默认值out.log
maxSize: 20 # 最大文件大小(MB)默认值10MB
maxBackups: 50 # 保留旧文件的最大个数默认值100个
maxAge: 15 # 保留旧文件的最大天数默认值30天
isCompression: true # 是否压缩/归档旧文件默认值false
# mysql 设置
mysql:
# dsn格式<user>:<pass>@(127.0.0.1:3306)/<db>?[k=v& ......]
dsn: "root:123456@(192.168.3.37:3306)/account?parseTime=true&loc=Local&charset=utf8,utf8mb4"
enableLog: true # 是否开启打印所有日志
slowThreshold: 0 # 如果大于0只打印时间大于阈值的日志优先级比enableLog高单位(毫秒)
maxIdleConns: 3 #设置空闲连接池中连接的最大数量
maxOpenConns: 50 # 设置打开数据库连接的最大数量
connMaxLifetime: 30 # 设置了连接可复用的最大时间,单位(分钟)
# redis 设置
redis:
# dsn只适合redis6版本以上默认用户为defaulturl格式 [user]:<pass>@]127.0.0.1:6379/[db]
dsn: "default:123456@192.168.3.37:6379"
# 适合各个版本redis
addr: 127.0.0.1:6379
password: "123456"
dB: 0
minIdleConn: 20
dialTimeout: 30 # 链接超时,单位(秒)
readTimeout: 500 # 读超时,单位(毫秒)
writeTimeout: 500 # 写超时,单位(毫秒)
poolSize: 100
poolTimeout: 200 # 连接池超时,单位(秒)
# jaeger配置
jaeger:
agentHost: "192.168.3.37"
agentPort: "6831"
samplingRate: 1.0 # 采样率0~1之间0表示禁止采样大于等于1表示采样所有链路
# etcd配置
etcd:
addrs: ["192.168.3.37:2379"]
# limit配置
rateLimiter:
dimension: "path" # 限流维度支持path和ip两种默认是path
qpsLimit: 1000 # 持续每秒允许成功请求数默认是500
maxLimit: 2000 # 瞬时最大允许峰值默认是1000通常大于qpsLimit
# metrics配置
metrics:
port: 9082

View File

@ -0,0 +1,70 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: sponge-dm
namespace: sponge
spec:
replicas: 1
selector:
matchLabels:
app: sponge
template:
metadata:
name: sponge-pod
labels:
app: sponge
spec:
containers:
- name: sponge
image: sponge/sponge:latest
# If using a local image, use Never, the default is Always
imagePullPolicy: Never
command: ["./sponge", "-c", "/app/config/conf.yml"]
ports:
- name: http-port
containerPort: 8080
- name: grpc-port
containerPort: 9090
- name: metrics-port
containerPort: 9082
resources:
requests:
cpu: 10m
memory: 10Mi
limits:
cpu: 1000m
memory: 1000Mi
volumeMounts:
- name: sponge-vl
mountPath: /app/config/
readOnly: true
# 就绪探测
readinessProbe:
#httpGet:
#port: http-port
#path: /health
exec:
command: ["/bin/grpc_health_probe", "-addr=:9090"]
initialDelaySeconds: 10
timeoutSeconds: 2
periodSeconds: 30
successThreshold: 1
failureThreshold: 3
# 容器存活探测
livenessProbe:
#httpGet:
#port: http-port
#path: /health
exec:
command: ["/bin/grpc_health_probe", "-addr=:9090"]
initialDelaySeconds: 10
timeoutSeconds: 2
periodSeconds: 30
successThreshold: 1
failureThreshold: 3
volumes:
- name: sponge-vl
configMap:
name: sponge-config

View File

@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: sponge-svc
namespace: sponge
spec:
selector:
app: sponge
type: ClusterIP
ports:
- name: sponge-svc-http-port
port: 8080
targetPort: 8080
- name: sponge-svc-grpc-port
port: 9090
targetPort: 9090
- name: sponge-svc-grpc-metrics-port
port: 9082
targetPort: 9082

View File

@ -4,8 +4,9 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/zhufuyi/sponge/config"
"github.com/stretchr/testify/assert"
)
// 测试时需要连接真实数据