栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Go Micro V3+Gin使用

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Go Micro V3+Gin使用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

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 helloworld
2.调用服务
# 检查服务状态
micro status

# 检查服务注册情况
micro services

# 调用指定的服务
micro helloworld --name=Alice

参数可以作为标志传递 --name 表示的就是 Name 字段参数.

如果你想要一个使用纯 json 和原始方法的更简单方式

micro call helloworld Helloworld.Call '{"name": "Alice"}'
3.Go Micro+gin示例
  1. 开启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)
	}
}
  1. 启动服务
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
  1. 新建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有很大区别,使用时要注意!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号