go-micro開發的路,還很長,大家多多交流。。。。
借鑒后打開撒打發了的經驗,https://blog.csdn.net/chenxun_2010/article/details/80949539
一、什么是go-micro
- Go Micro是一個插件化的基礎框架,基於此可以構建微服務。Micro的設計哲學是『可插拔』的插件化架構。在架構之外,它默認實現了consul作為服務發現,通過http進行通信,通過protobuf和json進行編解碼。我們一步步深入下去。
Go Micro是:
- 一個用Golang編寫的包
- 一系列插件化的接口定義
- 基於RPc
- Go Micro為下面的模塊定義了接口:
- 服務發現
- 編解碼
- 服務端、客戶端
- 訂閱、發布消息
二、使用go-micro編寫微服務
安裝protoc
1.github上下載一個cpp包:https://github.com/google/protobuf/releases make make install安裝即可
windows: https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protoc-3.6.1-win32.zip
2.protoc-gen-go
go get -u github.com/golang/protobuf/protoc-gen-go
3.安裝protoc-gen-micro
go get github.com/micro/protoc-gen-micro
4.獲取go-micro包
go get -u github.com/micro/go-micro
【獲取的工程中,如果有報錯(go get報錯unrecognized import path “golang.org/x/net/context”),則這樣解決:
操作的辦法就是:(http://www.bubuko.com/infodetail-2295919.html)
】
這樣就可以成功獲取到go-micro的包了。
安裝Consul
micro默認使用consul作為微服務發現
-
Consul is used as the default service discovery system.
-
-
Discovery is pluggable. Find plugins for etcd, kubernetes, zookeeper and more in the micro/go-plugins repo.
https://www.consul.io/intro/getting-started/install.html
啟動cansul方式參考如下:注意修改自己-data-dir目錄路勁
-
consul agent -server -node chenxun-server -bind=192.168.199.62 -
-
-
# consul agent - server -bootstrap-expect 1 -node chenxun-server -bind=192.168.1.251 -
-
-
# ./consul agent - server -bootstrap-expect 1 -
-
准備proto文件: 文件保存為chenxun.proto,名稱隨便寫,在實際項目中根據項目寫就好了
greeter.proto
syntax = "proto3";
service Greeter {
rpc Hello(HelloRequest) returns (HelloResponse) {}
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 2;
}
Generate the proto:(生成,我是放在linux上生成的,然后拷貝到windows上)
protoc --proto_path=$GOPATH/src:. --micro_out=. --go_out=. chenxun.proto
-rw-r--r-- 1 root root 2389 Aug 24 03:15 greeter.micro.go -rw-r--r-- 1 root root 4053 Aug 24 03:15 greeter.pb.go -rw-r--r-- 1 root root 165 Aug 24 03:15 greeter.proto
IDE使用的是GoLand

比如我把這三個文件放在gopath路勁下面的src目錄下面的mygoproject/gomirco
那么在import的時候寫: import "mygoproject/gomirco"
Service端代碼:
package main
import (
"context"
"fmt"
micro "github.com/micro/go-micro"
proto "mygoproject/gomirco" //這里寫你的proto文件放置路勁
)
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("greeter"),
)
// Init will parse the command line flags.
service.Init()
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
Client端代碼:
package main
import (
"context"
"fmt"
micro "github.com/micro/go-micro"
proto "mygoproject/gomirco" //這里寫你的proto文件放置路勁
)
func main() {
// Create a new service. Optionally include some options here.
service := micro.NewService(micro.Name("greeter.client"))
service.Init()
// Create new greeter client
greeter := proto.NewGreeterService("greeter", service.Client())
// Call the greeter
rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "老兵"})
if err != nil {
fmt.Println(err)
}
// Print response
fmt.Println(rsp.Greeting)
}
運行service:
-
go run examples/service/main.go
-
運行client:
go run examples/client/main.go
-------------------------------------------------------------------
consul ui訪問,可以看到你的service

