Docker
通常微服务情景时,或按服务交付时,我们使用docker部署。
dockerfile
# =============== build and run ===============# build: docker build -t go-service:latest .# run: docker run -dit go-service:latest# =============== build stage ===============FROM golang:1.16.5-buster AS build# envENV GOOS=linux \GOARCH=amd64 \CGO_ENABLED=0 \GO111MODULE=on \GOPROXY="https://goproxy.cn,direct"# dependentWORKDIR /appCOPY go.* ./RUN go mod download -x all# buildCOPY . ./# ldflags:# -s: disable symbol table# -w: disable DWARF generation# run "go tool link -help" to get the full list of ldflagsRUN go env && go build -ldflags "-s -w" -o go-service -v ./main.go# =============== final stage ===============FROM alpine:latest AS final# resourcesWORKDIR /appCOPY --from=build /app/go-service ./COPY --from=build /app/conf/base.toml ./conf/base.tomlCOPY --from=build /app/conf/prod ./conf/prod# set time zoneRUN apk add --no-cache tzdata# exposeEXPOSE 80ENTRYPOINT ["env","GO_ENV=prod","/app/go-service", "-other", "flags"]
nginx
当我们主机部署时,单机多域名服务时,就加一层nginx做转发。
http
server {listen 80;server_name test.com;root /rootdir;location ~ /services{#go 服务器可以指定到其他的机器上,这里设定本机服务器的6666端口proxy_pass http://127.0.0.1:6666;#Proxy Settingsproxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;#proxy_max_temp_file_size 0;#proxy_connect_timeout 90;#proxy_send_timeout 90;#proxy_read_timeout 90;#proxy_buffer_size 4k;#proxy_buffers 4 32k;#proxy_busy_buffers_size 64k;#proxy_temp_file_write_size 64k;}}
websocket
{http{map $http_upgrade $connection_upgrade {default upgrade;'' close;}}// other...server {listen 80;server_name test.com;root /rootdir;location ~ /services{proxy_pass http://127.0.0.1:8888;#Proxy Settingsproxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# websocketproxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection $connection_upgrade;proxy_read_timeout 300s; #keeplive}}}
