Go Micro V3+Gin使用提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、安装
- 二、使用
- 1.常用命令
- 2.调用服务
- 3.Go Micro+gin示例
- 总结
前言
github.com/micro/micro/v3/service + github.com/gin-gonic/gin搭建微服务
链接: gomicro中文文档.
#安装go-micro
go get -u -v github.com/micro/go-micro
#安装工具集
go get -u -v github.com/micro/micro
#安装protobuf插件
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro
二、使用
1.常用命令
# 启动服务器 micro server # 设置为本地环境 micro env set local #新建服务 micro new helloworld # 检查状态 micro status # 查看服务 micro services # 检查日志 micro logs helloworld # 启动你的服务 micro run helloworld # 干掉服务 micro kill helloworld2.调用服务
# 检查服务状态 micro status # 检查服务注册情况 micro services # 调用指定的服务 micro helloworld --name=Alice
参数可以作为标志传递 --name 表示的就是 Name 字段参数.
如果你想要一个使用纯 json 和原始方法的更简单方式
micro call helloworld Helloworld.Call '{"name": "Alice"}'
3.Go Micro+gin示例
- 开启micro服务器
打开控制台,执行命令
micro server
如下 2. 新建服务
新建service文件夹
micro new helloworld cd helloworld make init go mod vendor make proto
如果出现未登录或未认证的error执行micro login --username admin --password micro后再新建服务
helloworld/proto/helloworld.proto文件
syntax = "proto3";
package helloworld;
option go_package = "./proto;helloworld";
service Helloworld {
rpc Call(Request) returns (Response) {}
rpc Stream(StreamingRequest) returns (stream StreamingResponse) {}
rpc PingPong(stream Ping) returns (stream Pong) {}
}
message Message {
string say = 1;
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
message StreamingRequest {
int64 count = 1;
}
message StreamingResponse {
int64 count = 1;
}
message Ping {
int64 stroke = 1;
}
message Pong {
int64 stroke = 1;
}
helloworld/main.go文件
package main
import (
"helloworld/handler"
pb "helloworld/proto"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
)
func main() {
// Create service
srv := service.New(
service.Name("helloworld"),
)
// Register handler
pb.RegisterHelloworldHandler(srv.Server(), handler.New())
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}
- 启动服务
micro run . # 查看服务 #xiong@xiong-virtual-machine:~/go/server/helloworld$ micro status NAME VERSION SOURCE STATUS BUILD UPDATED METADATA helloworld latest /home/xiong/go/server/helloworld running n/a 5s ago owner=admin, group=micro
- 新建web文件夹
新建proto文件夹,把helloworld/proto文件夹下的两个.go文件拷贝到proto文件夹下,然后开始建立web服务
新建web/main.go文件
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
proto "web/proto"
"github.com/micro/micro/v3/service"
)
func main() {
// 初始化路由
router := gin.Default()
// 路由匹配
router.GET("/", GetHelloWorld)
// 启动运行
router.Run(":8090")
}
func GetHelloWorld(c *gin.Context) {
srv := service.New()
// create the proto client for helloworld
client := proto.NewHelloworldService("helloworld", srv.Client())
// call an endpoint on the service
rsp, err := client.Call(context.Background(), &proto.Request{
Name: "John",
})
if err != nil {
fmt.Println("Error calling helloworld: ", err)
return
}
c.JSON(200, rsp.Msg)
}
这时候执行go run main.go会报错
{
"id": "go.micro.client",
"code": 500,
"detail": {
"id": "go.micro.client",
"code": 500,
"detail": "service auth: route not found",
"status": "Internal Server Error"
},
"status": "Internal Server Error"
}
这是没有认证(!不知道作者咋想的,调用服务还得认证!),可以使用如下方法:
#先build go build # admin 和 micro是micro的账号密码 ./web --proxy_address=127.0.0.1:8081 --auth_id admin --auth_secret micro
打开浏览器输入`127.0.0.1:8090/`就可以查看结果了总结
micro v2 与micro v3有很大区别,使用时要注意!!!



