golang微服務框架go-micro 入門筆記1.搭建 go-micro環境
阿新 • • 發佈:2019-12-31
微服務的本質是讓專業的人做專業的事情,做出更好的東西。
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/複製程式碼
安裝 protoc
訪問如下網址
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
安裝protoc-gen-micro外掛
這個外掛主要作用是通過.proto
檔案生成適用於go-micro
的程式碼
go get -u -v github.com/micro/protoc-gen-micro複製程式碼
安裝 consul
下載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複製程式碼
hello,world
建立微服務
使用如下指令建立微服務
>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複製程式碼
生成適配proto的golang程式碼
注意:在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複製程式碼
啟動restful api介面支援支援
注意其中的--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外掛請求介面
推薦閱讀
開源專案golang go語言後臺管理框架restgo-admin