Golang 微服务 - 05 Docker上运行服务

创建虚拟机Ubuntu

注意:以下步骤均在已创建的Ubuntu虚拟机中完成

Ubuntu中安装配置Golang

curl --silent --output go1.11-linux-amd64.tar.gz https://dl.google.com/go/go1.11.linux-amd64.tar.gz
sudo tar -zxvf go1.11-linux-amd64.tar.gz -C /usr/local

下载安装包 go1.11.linux-amd64.tar.gz 并解压到 /usr/local

mkdir -p ~/golang/gopath

sudo vi /etc/profile
export GOPATH=~/golang/gopath
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
source /etc/profile

go env

创建目录 ~/golang/gopath

打开 /etc/profile,分别配置 GOPATH、GOROOT、PATH,保存后需要重新加载该文件

验证配置是否生效

Ubuntu中安装go-micro

go get github.com/micro/go-micro

安装 go-micro 过程中可能会报错,需要执行附录A中的脚本

Ubuntu中安装Docker

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

可能因为网络原因,从 https://get.docker.com 下载安装脚本会失败

(如果安装失败,可以参看《运行第一个容器》)

sudo docker run hello-world

验证 docker 是否安装成功(如果安装成功,则会打印“Hello from Dokcer!”)

Ubuntu中下载源码

cd $GOPATH/src
git clone https://github.com/nealguo/go-micro-demo.git
mv go-micro-demo/consignment-service consignment-service
mv go-micro-demo/vessel-service vessel-service

进入目录 $GOPATH/src
下载源码 https://github.com/nealguo/go-micro-demo.git
移动源码 consignment-service 和 vessel-service

Ubuntu中启动服务

cd $GOPATH/src/vessel-service
make build && make run

在 tty1 中启动 vessel-service,输入结果如下

cd $GOPATH/src/consignment-service
make build && make run

在 tty2 (快捷键 Ctrl+Alt+F2) 中启动 consignment-service,输入结果如下

附录A

#!/bin/bash
mkdir -p $GOPATH/src/google.golang.org
cd $GOPATH/src/google.golang.org
git clone https://github.com/grpc/grpc-go.git grpc
git clone https://github.com/google/go-genproto.git genproto
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/net.git
git clone https://github.com/golang/text.git
git clone https://github.com/golang/sys.git
go get -u github.com/golang/protobuf/{proto,ptypes,protoc-gen-go}
go install google.golang.org/grpc

涉及到 protoc 和 grpc 等安装

参考

《Go-compiled binary won't...》https://stackoverflow.com/questions...
《Get Docker CE for Ubuntu》https://docs.docker.com/install/linux/docker-ce/ubuntu/...
《Dockerfile 常用指令》https://mp.weixin.qq.com/...
《Dockerfile 构建镜像》https://mp.weixin.qq.com/...
《Go实践微服务》https://yuanxuxu.com/2018/06/21/go-microservice-in-action-go-micro/

转载于:https://my.oschina.net/deltatech/blog/2245583

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值