mirror of
https://github.com/zhufuyi/sponge.git
synced 2024-05-03 09:44:53 +08:00
add deployments scripts
This commit is contained in:
parent
e16d0ac3de
commit
903c33447e
@ -14,6 +14,7 @@ run:
|
|||||||
skip-dirs:
|
skip-dirs:
|
||||||
- pkg
|
- pkg
|
||||||
- docs
|
- docs
|
||||||
|
- api
|
||||||
# which files to skip: they will be analyzed, but issues from them
|
# which files to skip: they will be analyzed, but issues from them
|
||||||
# won't be reported. Default value is empty list, but there is
|
# won't be reported. Default value is empty list, but there is
|
||||||
# no need to include all autogenerated files, we confidently recognize
|
# no need to include all autogenerated files, we confidently recognize
|
||||||
|
57
Makefile
57
Makefile
@ -4,10 +4,11 @@ PROJECT_NAME := "github.com/zhufuyi/sponge"
|
|||||||
PKG := "$(PROJECT_NAME)"
|
PKG := "$(PROJECT_NAME)"
|
||||||
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
|
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
|
||||||
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
|
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
|
||||||
|
PROJECT_FILES := $(shell ls)
|
||||||
|
|
||||||
|
|
||||||
.PHONY: init
|
.PHONY: init
|
||||||
# init env
|
# installation of dependent tools
|
||||||
init:
|
init:
|
||||||
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0
|
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
|
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0
|
||||||
@ -24,81 +25,93 @@ init:
|
|||||||
|
|
||||||
|
|
||||||
.PHONY: ci-lint
|
.PHONY: ci-lint
|
||||||
# make ci-lint
|
# check the code specification against the rules in the .golangci.yml file
|
||||||
ci-lint:
|
ci-lint:
|
||||||
golangci-lint run ./...
|
golangci-lint run ./...
|
||||||
|
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
# make build, Build the binary file
|
# go build the linux amd64 binary file
|
||||||
build:
|
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'"
|
@echo "build finished, binary file in path 'cmd/sponge'"
|
||||||
|
|
||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
# make run, run app
|
# run server
|
||||||
run:
|
run:
|
||||||
@bash scripts/run.sh
|
@bash scripts/run.sh
|
||||||
|
|
||||||
|
|
||||||
.PHONY: dep
|
.PHONY: dep
|
||||||
# make dep Get the dependencies
|
# download dependencies to the directory vendor
|
||||||
dep:
|
dep:
|
||||||
@go mod download
|
@go mod download
|
||||||
|
|
||||||
|
|
||||||
.PHONY: fmt
|
.PHONY: fmt
|
||||||
# make fmt
|
# go format *.go files
|
||||||
fmt:
|
fmt:
|
||||||
@gofmt -s -w .
|
@gofmt -s -w .
|
||||||
|
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
# make test
|
# go test *_test.go files
|
||||||
test:
|
test:
|
||||||
go test -short ${PKG_LIST}
|
go test -short ${PKG_LIST}
|
||||||
|
|
||||||
|
|
||||||
.PHONY: cover
|
.PHONY: cover
|
||||||
# make cover
|
# generate test coverage
|
||||||
cover:
|
cover:
|
||||||
go test -short -coverprofile coverage.out -covermode=atomic ${PKG_LIST}
|
go test -short -coverprofile cover.out -covermode=atomic ${PKG_LIST}
|
||||||
go tool cover -html=coverage.out
|
go tool cover -html=cover.out
|
||||||
|
|
||||||
|
|
||||||
.PHONY: docker
|
.PHONY: docker
|
||||||
# generate docker image
|
# build docker image
|
||||||
docker:
|
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
|
.PHONY: clean
|
||||||
# make clean
|
# clean binary file, cover.out, redundant dependency packages
|
||||||
clean:
|
clean:
|
||||||
@-rm -vrf sponge
|
@rm -vrf cmd/sponge/sponge
|
||||||
@-rm -vrf cover.out
|
@rm -vrf cover.out
|
||||||
@-rm -vrf coverage.txt
|
|
||||||
@go mod tidy
|
@go mod tidy
|
||||||
@echo "clean finished"
|
@echo "clean finished"
|
||||||
|
|
||||||
|
|
||||||
.PHONY: docs
|
.PHONY: docs
|
||||||
# gen swagger doc
|
# generate swagger doc
|
||||||
docs:
|
docs:
|
||||||
@swag init -g cmd/sponge/main.go
|
@swag init -g cmd/sponge/main.go
|
||||||
@echo "see docs by: http://localhost:8080/swagger/index.html"
|
@echo "see docs by: http://localhost:8080/swagger/index.html"
|
||||||
|
|
||||||
|
|
||||||
.PHONY: graph
|
.PHONY: graph
|
||||||
# make graph 生成交互式的可视化Go程序调用图,生成完毕后会在浏览器自动打开
|
# generate interactive visual function dependency graphs
|
||||||
graph:
|
graph:
|
||||||
@echo "generating graph ......"
|
@echo "generating graph ......"
|
||||||
@go-callvis github.com/zhufuyi/sponge
|
@go-callvis github.com/zhufuyi/sponge
|
||||||
|
|
||||||
|
|
||||||
.PHONY: mockgen
|
.PHONY: mockgen
|
||||||
# make mockgen gen mock file
|
# mockgen gen mock file
|
||||||
mockgen:
|
mockgen:
|
||||||
cd ./internal && for file in `egrep -rnl "type.*?interface" ./repository | grep -v "_test" `; do \
|
cd ./internal && for file in `egrep -rnl "type.*?interface" ./repository | grep -v "_test" `; do \
|
||||||
echo $$file ; \
|
echo $$file ; \
|
||||||
@ -107,13 +120,13 @@ mockgen:
|
|||||||
|
|
||||||
|
|
||||||
.PHONY: proto
|
.PHONY: proto
|
||||||
# generate proto struct only
|
# generate *.pb.go codes from *.proto files
|
||||||
proto:
|
proto:
|
||||||
@bash scripts/protoc.sh
|
@bash scripts/protoc.sh
|
||||||
|
|
||||||
|
|
||||||
.PHONY: proto-doc
|
.PHONY: proto-doc
|
||||||
# generate proto doc
|
# generate doc from *.proto files
|
||||||
proto-doc:
|
proto-doc:
|
||||||
@bash scripts/proto-doc.sh
|
@bash scripts/proto-doc.sh
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ sponge 是一个微服务框架,支持http和grpc及服务治理,结合[goct
|
|||||||
│ ├── handler # http的业务功能实现
|
│ ├── handler # http的业务功能实现
|
||||||
│ ├── model # 数据库 model
|
│ ├── model # 数据库 model
|
||||||
│ ├── routers # http 路由
|
│ ├── routers # http 路由
|
||||||
│ ├── server # 服务入口,包括http和grpc服务入口
|
│ ├── server # 服务入口,包括http和grpc服务
|
||||||
│ └── service # grpc的业务功能实现
|
│ └── service # grpc的业务功能实现
|
||||||
├── pkg # 外部应用程序可以使用的库代码
|
├── pkg # 外部应用程序可以使用的库代码
|
||||||
├── scripts # 存放用于执行各种构建,安装,分析等操作的脚本
|
├── scripts # 存放用于执行各种构建,安装,分析等操作的脚本
|
||||||
|
@ -14,7 +14,7 @@ RUN cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-health-probe@v0.4.12 \
|
|||||||
&& go mod download \
|
&& go mod download \
|
||||||
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /grpc_health_probe
|
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /grpc_health_probe
|
||||||
|
|
||||||
# 通过二进制文件构建镜像
|
# 使用二进制文件构建镜像
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
MAINTAINER zhufuyi "g.zhufuyi@gmail.com"
|
MAINTAINER zhufuyi "g.zhufuyi@gmail.com"
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ RUN apk add tzdata \
|
|||||||
&& echo "Asia/Shanghai" > /etc/timezone \
|
&& echo "Asia/Shanghai" > /etc/timezone \
|
||||||
&& apk del tzdata
|
&& apk del tzdata
|
||||||
|
|
||||||
# 添加curl,用在http服务的检查
|
# 添加curl,用在http服务的检查,如果用部署在k8s,可以不用安装
|
||||||
RUN apk add curl
|
RUN apk add curl
|
||||||
|
|
||||||
# 添加grpc_health_probe,用在grpc服务的健康检查
|
# 添加grpc_health_probe,用在grpc服务的健康检查
|
||||||
@ -35,3 +35,4 @@ COPY --from=build /go/src/sponge/config/conf.yml /app/config/conf.yml
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
CMD ["./sponge", "-c", "config/conf.yml"]
|
CMD ["./sponge", "-c", "config/conf.yml"]
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
# 需要先在本地编译好二进制文件和配置文件复制到和Dokerfile在同一个目录下
|
# 需要先在本地编译好二进制文件和配置文件复制到和Dokerfile在同一个目录下
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
MAINTAINER zhufuyi "g.zhufuyi@gmail.com"
|
MAINTAINER zhufuyi "g.zhufuyi@gmail.com"
|
||||||
|
|
||||||
# 设置时区为上海
|
# 设置时区为上海
|
||||||
@ -19,5 +18,6 @@ RUN chmod +x /bin/grpc_health_probe
|
|||||||
COPY config/ /app/config/
|
COPY config/ /app/config/
|
||||||
COPY sponge /app/sponge
|
COPY sponge /app/sponge
|
||||||
RUN chmod +x /app/sponge
|
RUN chmod +x /app/sponge
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
CMD ["./sponge", "-c", "config/conf.yml"]
|
CMD ["./sponge", "-c", "config/conf.yml"]
|
||||||
|
@ -14,9 +14,9 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- $PWD/config:/app/config
|
- $PWD/config:/app/config
|
||||||
healthcheck:
|
healthcheck:
|
||||||
#test: ["CMD", "curl", "-f", "http://localhost:8080/health"] # http健康检查,注:镜像里必须又curl命令
|
#test: ["CMD", "curl", "-f", "http://localhost:8080/health"] # http健康检查,注:镜像必须包含curl命令
|
||||||
test: ["CMD", "grpc_health_probe", "-addr=localhost:9090"] # grpc健康检查,注:镜像必须有grpc_health_probe命令
|
test: ["CMD", "grpc_health_probe", "-addr=localhost:9090"] # grpc健康检查,注:镜像必须包含grpc_health_probe命令
|
||||||
interval: 30s # 间隔时间
|
interval: 30s # 间隔时间
|
||||||
timeout: 5s # 超时时间
|
timeout: 5s # 超时时间
|
||||||
retries: 3 # 重试次数
|
retries: 3 # 重试次数
|
||||||
start_period: 10s # 启动多久后开始检查
|
start_period: 10s # 启动多久后开始检查
|
||||||
|
4
deployments/kubernetes/namespace.yaml
Normal file
4
deployments/kubernetes/namespace.yaml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: Namespace
|
||||||
|
metadata:
|
||||||
|
name: sponge
|
96
deployments/kubernetes/sponge-configmap.yml
Normal file
96
deployments/kubernetes/sponge-configmap.yml
Normal 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,需要大于60s,pprof做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版本以上,默认用户为default,url格式 [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
|
70
deployments/kubernetes/sponge-deployment.yml
Normal file
70
deployments/kubernetes/sponge-deployment.yml
Normal 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
|
19
deployments/kubernetes/sponge-svc.yml
Normal file
19
deployments/kubernetes/sponge-svc.yml
Normal 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
|
@ -4,8 +4,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/zhufuyi/sponge/config"
|
"github.com/zhufuyi/sponge/config"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 测试时需要连接真实数据
|
// 测试时需要连接真实数据
|
||||||
|
Loading…
Reference in New Issue
Block a user