创建虚拟机Ubuntu
- 使用 Oracle VM VirtualBox 创建 ubuntu 16.04.5 (VirtualBox下载地址)
- 镜像为 ubuntu-16.04.5-server-amd64.iso (镜像下载地址)
- 虚拟机创建步骤略
注意:以下步骤均在已创建的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/