go-micro部署Docker

创建Dockerfile

1
vim Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
FROM golang:latest

# 安装软件
RUN sed -i "s/deb.debian.org/repo.huaweicloud.com/" /etc/apt/sources.list && \
apt-get update && \
apt-get install -y jq unzip && \
rm -rf /var/lib/apt/lists/*

# 安装 protoc
RUN protoc_version=$(wget -qO- -t1 -T2 "https://api.github.com/repos/protocolbuffers/protobuf/releases/latest" | jq -r '.tag_name') && \
wget https://ghproxy.com/https://github.com/protocolbuffers/protobuf/releases/download/${protoc_version}/protoc-${protoc_version:1}-linux-x86_64.zip && \
unzip protoc-${protoc_version:1}-linux-x86_64.zip bin/protoc -d $(go env GOPATH) && \
rm -f protoc-${protoc_version:1}-linux-x86_64.zip

# go 代理
RUN go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,direct

# 安装 go-micro
RUN go install github.com/go-micro/cli/cmd/go-micro@latest

# go-micro 创建工程
RUN go-micro new service helloworld && \
cd helloworld && \
sed -i '/micro.Version(version),/a\\t\tmicro.Address(":8080"),' main.go && \
make init proto tidy

CMD cd helloworld && go run main.go

构建

1
docker build -t go-micro-helloworld .

运行

1
docker run -d -p 8080:8080 go-micro-helloworld

测试

1
curl -XPOST -H 'Content-Type: application/json' -H 'Micro-Endpoint: Helloworld.Call' -d '{"name": "John"}' http://localhost:8080