Microservice-微服务-micro-环境搭建-ubuntu
- 1.系统
- 1.系统信息
- 2.替换软件源
- 3.升级
- 4.安装必备软件
- 2.Golang
- 1.下载安装
- 2.设置环境变量
- 3.测试
- 2.Docker
- 1.docker
- 1.下载安装
- 2.测试
- 2.docker-compose
- 1.下载安装
- 2.测试
- 3.protobuf
- 1.protobuf
- 1.解决git clone速度慢
- 2.下载安装
- 3.测试
- 2.golang-protobuf
- 1.下载安装
- 2.测试
- 4.gRPC
- 1.下载安装
- 2.测试
- 5.consul
- 1.下载安装
- 2.测试
- 6.micro
- 1.不支持consul注册服务【默认mdns/etcd】
- 1.下载
- 2.编译安装
- 3.插件安装
- 4.测试
- 2.支持consul注册服务
1.系统
1.系统信息
sudo cat /proc/version
2.替换软件源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
sudo sed -i 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
sudo sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
# sudo sed -i 's/cn.security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
# sudo sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
source /etc/apt/sources.list
3.升级
sudo apt-get upgrade -y
sudo apt-get update
sudo apt upgrade -y
sudo apt update
4.安装必备软件
sudo apt install -y vim
sudo apt-get install -y vim
sudo apt install -y git
sudo apt-get install -y git
sudo apt install -y wget
sudo apt-get install -y wget
sudo apt install -y curl
sudo apt-get install -y curl
sudo apt install -y net-tools
sudo apt-get install -y net-tools
sudo apt install -y openssh-server
sudo apt-get install -y openssh-server
sudo apt install -y bridge-utils
sudo apt-get install -y bridge-utils
sudo apt install -y dnsutils
sudo apt-get install -y dnsutils
sudo apt install -y tar
sudo apt-get install -y tar
sudo apt install -y unzip
sudo apt-get install -y unzip
sudo apt install -y tree
sudo apt-get install -y tree
sudo apt install -y make
sudo apt-get install -y make
sudo apt install -y g++
sudo apt-get install -y g++
sudo apt install -y gcc
sudo apt-get install -y gcc
sudo apt install -y autoconf
sudo apt-get install -y autoconf
sudo apt install -y automake
sudo apt-get install -y automake
sudo apt install -y python-pip
sudo apt-get install -y python-pip
sudo apt install -y python-dev
sudo apt-get install -y python-dev
sudo apt install -y libffi-dev
sudo apt-get install -y libffi-dev
sudo apt install -y openssl
sudo apt-get install -y openssl
sudo apt install -y openssl-dev
sudo apt-get install -y openssl-dev
sudo apt install -y gcc
sudo apt-get install -y gcc
sudo apt install -y libc-dev
sudo apt-get install -y libc-dev
sudo apt install -y make
sudo apt-get install -y make
sudo apt install -y libtool
sudo apt-get install -y libtool
sudo apt-get install -y teeapt-transport-https
sudo apt-get install -y ca-certificates
sudo apt-get install -y software-properties-common
sudo apt-get clean
sudo apt-get upgrade -y
sudo apt-get update
sudo apt upgrade -y
sudo apt update
2.Golang
1.下载安装
cd ~ && wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
cd ~ && sudo tar -zxvf go1.14.2.linux-amd64.tar.gz -C /usr/local
ls /usr/local/ | grep go
2.设置环境变量
mkdir -p ~/workspace/go/bin ~/workspace/go/src ~/workspace/go/pkg && tree ~/workspace/go/
sudo tee -a /etc/profile <<-'EOF'
export GOROOT=/usr/local/go
export GOPATH=~/workspace/go
export GOBIN=$GOPATH/bin
export GO111MODULE="on"
export GOPROXY=https://goproxy.io
export PATH=$PATH:$GOROOT/bin:$GOPATH:$GOBIN:$GOPROXY
EOF
source /etc/profile
3.测试
go env
go version
2.Docker
1.docker
1.下载安装
sudo apt-get upgrade -y
sudo apt-get update
sudo apt upgrade -y
sudo apt update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt update
# 安装docker
sudo apt-get -y install docker-ce
sudo groupadd docker
sudo gpasswd -a ${USER} docker
sudo service docker restart
newgrp - docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://1nj0zren.mirror.aliyuncs.com",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com",
"https://dockerhub.azk8s.cn",
"https://docker.mirrors.ustc.edu.cn",
"https://reg-mirror.qiniu.com",
"https://hub-mirror.c.163.com",
"https://mirror.ccs.tencentyun.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.测试
docker info
docker version
2.docker-compose
1.下载安装
cd ~
mkdir -p ~/.pip && cd $_ && tee ~/.pip/pip.conf <<-'EOF'
[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/
EOF
source ~/.pip/pip.conf
sudo apt install -y python-pip python-dev libffi-dev openssl-dev gcc libc-dev make
sudo pip install docker-compose
2.测试
docker-compose version
3.protobuf
1.protobuf
1.解决git clone速度慢
sudo cp /etc/hosts /etc/hosts.backup
curl -skX GET 'https://github.com.ipaddress.com/' | grep -oP '<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>.*?</li>'| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'| awk '{print $1" github.com"}'> ~/myhosts
curl -skX GET 'https://fastly.net.ipaddress.com/github.global.ssl.fastly.net' | grep -oP '<th>IPv4 Addresses</th><td><ul class="comma-separated"><li>.*?</li>'| grep -oP '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'| awk '{print $1" github.global.ssl.fastly.net"}'>> ~/myhosts
sudo /bin/bash -c 'cat ~/myhosts>>/etc/hosts'
cat /etc/hosts
sudo service networking restart
rm -f ~/myhosts
2.下载安装
cd ~
git clone https://github.com/protocolbuffers/protobuf.git && ls | grep protobuf
# 开始安装
cd ./protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig # [很重要]刷新共享库
3.测试
protoc --version
protoc --help
sudo cp /etc/hosts.backup /etc/hosts
sudo service networking restart
2.golang-protobuf
1.下载安装
go get -u -v github.com/golang/protobuf/...
cd $GOPATH/bin && ls -la | grep protoc-gen-go
sudo tee -a /etc/protofile <<-'EOF'
export PATH=$PATH:$GOPATH/bin
EOF
source /etc/profile
2.测试
终端输入protoc-,然后按下Tab键查看是否补全,补全即OK!
protoc-
4.gRPC
1.下载安装
go get -u -v google.golang.org/grpc/...
2.测试
1.server启动
cd $GOPATH/pkg/mod/google.golang.org/grpc*/examples/helloworldexamples/helloworldexamples/helloworldexamples/helloworld/*_client && go run main.go
5.consul
1.下载安装
cd ~ && sudo apt install -y wget unzip
wget https://releases.hashicorp.com/consul/1.7.2/consul_1.7.2_linux_amd64.zip && ls
sudo unzip ./consul_1.7.2_linux_amd64.zip -d /usr/local/bin/
2.测试
consul
6.micro
1.不支持consul注册服务【默认mdns/etcd】
1.下载
go get -u -v github.com/micro/micro/...
2.编译安装
cd $GOPATH/pkg/mod/github.com/micro/ && ls |grep micro
选择版本进入编译到/bin/目录下,名称为micro,源文件为main.go
cd $GOPATH/pkg/mod/github.com/micro/micro@v1.18.0
sudo $GOROOT/bin/go build -o /bin/micro main.go
3.插件安装
go get -u -v github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u -v github.com/micro/protoc-gen-micro
4.测试
micro --version
micro --help
2.支持consul注册服务
在生成的srv服务进行修改main.go即可
原main.go
package main
import (
"github.com/micro/go-micro/util/log"
"github.com/micro/go-micro"
"micro/rpc/srv/handler"
"micro/rpc/srv/subscriber"
srv "micro/rpc/srv/proto/srv"
)
func main() {
// New Service
service := micro.NewService(
micro.Name("go.micro.srv.srv"),
micro.Version("latest"),
)
// Initialise service
service.Init()
// Register Handler
srv.RegisterSrvHandler(service.Server(), new(handler.Srv))
// Register Struct as Subscriber
micro.RegisterSubscriber("go.micro.srv.srv", service.Server(), new(subscriber.Srv))
// Register Function as Subscriber
micro.RegisterSubscriber("go.micro.srv.srv", service.Server(), subscriber.Handler)
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
修改后【中文注释部分即为修改部分】:
package main
import (
"micro/rpc/srv/handler"
"micro/rpc/srv/subscriber"
"github.com/micro/go-micro"
"github.com/micro/go-micro/util/log"
srv "micro/rpc/srv/proto/srv"
"github.com/micro/go-micro/registry" // 添加注册插件
"github.com/micro/go-plugins/registry/consul" // 添加注册插件的consul支持
)
func main() {
// consul注册
consulReg := consul.NewRegistry(
registry.Addrs("127.0.0.1:8500"),
)
// New Service
service := micro.NewService(
micro.Name("go.micro.srv.srv"),
micro.Version("latest"),
micro.Registry(consulReg), // micro添加consul的注册
)
// Initialise service
service.Init()
// Register Handler
srv.RegisterSrvHandler(service.Server(), new(handler.Srv))
// Register Struct as Subscriber
micro.RegisterSubscriber("go.micro.srv.srv", service.Server(), new(subscriber.Srv))
// Register Function as Subscriber
micro.RegisterSubscriber("go.micro.srv.srv", service.Server(), subscriber.Handler)
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
重新run[consul 一定要开启consul agent -dev]即可
go run main.go