微服务的本质是让专业的人做专业的事情,做出更好的东西。
golang 具备高并发,静态编译等特性,在性能、安全等方面具备非常大的优势。 go-micro 是基于 golang 的微服务编程框架, go-micro 操作简单、编码高效、功能强大。但是网络上资料偏少,本系列文章定位最简单最容易上手的 go-micro 入门教程,所有案列来自实操,而非网络上的复制粘贴。
本章节的目的是让大家最快速搭建好 go-micro 环境
#linux 下 export GO111MODULE=on export GOPROXY=https://goproxy.io # windows下设置如下环境变量 setx GO111MODULE on setx GOPROXY https://goproxy.io复制代码
# 使用如下指令安装 go get -u -v github.com/micro/micro go get -u -v github.com/micro/go-micro复制代码
如果没有git请自行安装git
#下载地址 https://git-scm.com/downloads/复制代码
访问如下网址
https://github.com/protocolbuffers/protobuf/releases复制代码
下载,不同的版本文件名称不一样,我们这里选择 protoc-3.9.1-win64.zip
protoc-3.9.1-win64.zip复制代码
解压到目标文件架,我们以 e:dev 为例
e:/dev/protoc-3.9.1-win64复制代码
添加 e:devprotoc-3.9.1-win64bin 到环境变量 path
这个插件主要作用是通过 .proto 文件生成适用于 go-micro 的代码
go get -u -v github.com/micro/protoc-gen-micro复制代码
下载windows版本
https://www.consul.io/downloads.html复制代码
解压到
e:/dev/consul复制代码
添加 e:devconsul 到环境变量 path 使用如下指查看是否安装成功,如下所示安装成功
>consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators复制代码
使用如下指令创建微服务
>micro new techidea8.com/microapp/hello
Creating service go.micro.srv.hello in E:/winlion/gopath/src/techidea8.com/microapp/hello
.
├── main.go
├── plugin.go
├── handler
│ └── hello.go
├── subscriber
│ └── hello.go
├── proto/hello
│ └── hello.proto
├── Dockerfile
├── Makefile
├── README.md
└── go.mod
download protobuf for micro:
brew install protobuf
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro
compile the proto file hello.proto:
cd E:/winlion/gopath/src/techidea8.com/microapp/hello
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/hello/hello.proto复制代码
注意:在win系统下 $GOPATH 环境变量无效,因此如上脚本将创建微服务失败,因此我们需要对如上脚本进行处理
#切换到项目目录下 >cd /d E:/winlion/gopath/src/techidea8.com/microapp/hello # 根据proto生成文件 >protoc --proto_path=. --go_out=. --micro_out=. proto/hello/hello.proto复制代码
>go run main.go 2019/08/19 13:00:46 Transport [http] Listening on [::]:54689 2019/08/19 13:00:46 Broker [http] Connected to [::]:54690 2019/08/19 13:00:46 Registry [mdns] Registering node: go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 2019/08/19 13:00:46 Subscribing go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 to topic: go.micro.srv.hello 2019/08/19 13:00:46 Subscribing go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 to topic: go.micro.srv.hello复制代码
>micro list services go.micro.srv.hello topic:go.micro.srv.hello复制代码
注意其中的--namespace参数,我们每一个微服务都属于一个命名空间,通过api暴露出来该命名空间后,满足 go.micro.srv.* 格式的微服务都可以访问。如 go.micro.srv.hello 可以通过如下格式访问
http://127.0.0.1:8080/user/call复制代码
>micro api --namespace=go.micro.srv 2019/08/19 13:07:11 Registering API Default Handler at / 2019/08/19 13:07:11 HTTP API Listening on [::]:8080 2019/08/19 13:07:11 Transport [http] Listening on [::]:54934 2019/08/19 13:07:11 Broker [http] Connected to [::]:54935 2019/08/19 13:07:11 Registry [mdns] Registering node: go.micro.api-1753185c-b8e1-49c4-aa0f-617f243a8e2a复制代码
restd插件请求接口