侧边栏壁纸
  • 累计撰写 16 篇文章
  • 累计创建 17 个标签
  • 累计收到 1 条评论

Micro初探

xiuxiubiu
2020-08-06 / 0 评论 / 0 点赞 / 1,004 阅读 / 6,664 字 / 正在检测是否收录...

概念

Micro是一个构建和管理分布式系统的系统。
Micro的关键组成有以下几部分:

  • Runtime: 一个管理服务的runtime环境包含验证(auth)、配置(config)、服务发现(discovery)、网络(networking)、存储(storage)。
  • Framework:一个编写在runtime环境中运行的服务的Go语言框架。
  • Clients:其他程序访问Micro服务的多语言客户端。

安装

使用Go安装
安装成功后,会在$GOPATH/bin/中生成micro文件

go get github.com/micro/micro/v2

二进制包安装

# MacOS
curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash

# Linux
wget -q  https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash

# Windows
powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"

micro server

编写服务前,首先使用如下命令启动micro程序:

micro server

micro运行成功后,会持续输出大量的日志信息:

2020-08-05 13:15:33  file=server/server.go:126 level=info service=micro Loading core services
2020-08-05 13:15:33  file=server/server.go:151 level=info service=micro Registering micro.config
2020-08-05 13:15:33  file=server/server.go:151 level=info service=micro Registering micro.auth
......
2020-08-05 13:15:35  file=v2@v2.9.1/service.go:200 level=info service=proxy Starting [service] go.micro.proxy
2020-08-05 13:15:35  file=grpc/grpc.go:864 level=info service=proxy Server [grpc] Listening on [::]:55723
2020-08-05 13:15:35  file=grpc/grpc.go:697 level=info service=proxy Registry [mdns] Registering node: go.micro.proxy-13f02ed7-ff55-4359-9e37-90641e1af128

micro启动后使用micro list services查看正在运行的服务

$ micro list services
go.micro.api
go.micro.auth
go.micro.bot
go.micro.broker
go.micro.config
go.micro.debug
go.micro.network
go.micro.proxy
go.micro.registry
go.micro.router
go.micro.runtime
go.micro.server
go.micro.web

创建服务

使用micro new [service]命令创建一个新的服务,生成代码的module名称会和service一致,比如micro new github.com/xiuxiubiu/micro-helloworld,会在当前目录生成github.com/xiuxiubiu/micro-hellworld目录,且项目module名称为github.com/xiuxiubiu/micro-helloworld,具体输出如下:

$ micro # micro new github.com/xiuxiubiu/micro-helloworld
Creating service go.micro.service.micro-helloworld in github.com/xiuxiubiu/micro-helloworld

.
├── main.go
├── generate.go
├── plugin.go
├── handler
│   └── micro-helloworld.go
├── subscriber
│   └── micro-helloworld.go
├── proto
│   └── micro-helloworld
│       └── micro-helloworld.proto
├── Dockerfile
├── Makefile
├── README.md
├── .gitignore
└── go.mod


download protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:

visit https://github.com/protocolbuffers/protobuf/releases

download protobuf for micro:

go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v2/cmd/protoc-gen-micro

compile the proto file micro-helloworld.proto:

cd github.com/xiuxiubiu/micro-helloworld
make proto

编译proto文件前,首先安装protoprotoc-gen-goprotoc-gen-micro三款工具,他们的用途是将proto文件转换为Go代码。

$ go get -u github.com/golang/protobuf/proto

$ go get -u github.com/golang/protobuf/protoc-gen-go

$ go get github.com/micro/micro/v2/cmd/protoc-gen-micro

然后进入github.com/xiuxiubiu/micro-helloworld目录编译proto文件

$ cd github.com/xiuxiubiu/micro-helloworld

$ make proto
protoc --proto_path=. --micro_out=Mproto/imports/api.proto=github.com/micro/go-micro/v2/api/proto:. --go_out=Mproto/imports/api.proto=github.com/micro/go-micro/v2/api/proto:. proto/micro-helloworld/micro-helloworld.proto
2020/08/06 09:14:17 WARNING: Missing 'go_package' option in "proto/micro-helloworld/micro-helloworld.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

WARNING的意思是:proto文件缺少go_package选项来指定包名,在proto文件中添加option go_package即可解决,不指定也不影响使用。

syntax = "proto3";
  
package go.micro.service.helloworld;

go_package = "proto/helloworld";

运行服务

使用micro run [source]命令运行服务

$ micro run .
go: finding module for package github.com/micro/go-micro/v2
go: finding module for package github.com/micro/go-micro/v2/logger
...
2020-08-05 21:21:26  file=v2@v2.9.1/service.go:200 level=info Starting [service] go.micro.service.helloworld
2020-08-05 21:21:26  file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:50141
2020-08-05 21:21:26  file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:50142
2020-08-05 21:21:26  file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: go.micro.service.helloworld-7810bae1-6c4f-4122-bf02-e1b571541190
2020-08-05 21:21:26  file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.helloworld

使用micro list services查看已启动的服务,或者使用micro logs [service]命令查看服务日志:

$ micro logs helloworld
...
2020-08-05 21:21:26  file=v2@v2.9.1/service.go:200 level=info Starting [service] go.micro.service.helloworld
2020-08-05 21:21:26  file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:50141
2020-08-05 21:21:26  file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:50142
2020-08-05 21:21:26  file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: go.micro.service.helloworld-7810bae1-6c4f-4122-bf02-e1b571541190
2020-08-05 21:21:26  file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.helloworld

调用服务

可以使用如下方式调用micro server上正在运行的服务

命令行调用

命令行是最简单的调用方式:

$ micro call go.micro.service.helloworld Helloworld.Call '{"name":"Jane"}'
{
	"msg": "Hello Jane"
}

可以通过以下方式查看服务有哪些可调用的端点:

  • micro get service [service]命令

    $ micro get service go.micro.service.helloworld
    service  go.micro.service.helloworld
    
    version latest
    
    
    ...
    
    Endpoint: Helloworld.PingPong
    
    ...
    
    
    Endpoint: Helloworld.Stream
    
    ...
    
    
    Endpoint: Helloworld.Handle
    
    ...
    
  • 查看proto文件

    service Helloworld {
        rpc Call(Request) returns (Response) {}
        rpc Stream(StreamingRequest) returns (stream StreamingResponse) {}
        rpc PingPong(stream Ping) returns (stream Pong) {}
    }
    
  • 访问http://localhost:8082实时查看服务信息

Go Micro框架调用

proto可以使用我github中已生成好的micro-helloworld,也可替换成自己生成的proto代码。

package main

import (
     "context"
     "fmt"
     "time"

    "github.com/micro/go-micro/v2"
    proto "github.com/xiuxiubiu/micro-helloworld/proto/micro-helloworld"
)

func main() {
    // create and initialise a new service
    service := micro.NewService()
    service.Init()

    // create the proto client for helloworld
    client := proto.NewMicroHelloworldService("go.micro.service.micro-helloworld", service.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
    }

    // print the response
    fmt.Println("Response: ", rsp.Msg)

    // let's delay the process for exiting for reasons you'll see below
    time.Sleep(time.Second * 5)
}

运行以上代码调用服务:

$ go mod init go-micro-client
go: creating new go.mod: module go-micro-client

$ micro run .
go: finding module for package github.com/micro/go-micro/v2
go: finding module for package github.com/xiuxiubiu/micro-helloworld/proto/micro-helloworld
go: found github.com/micro/go-micro/v2 in github.com/micro/go-micro/v2 v2.9.1
go: found github.com/xiuxiubiu/micro-helloworld/proto/micro-helloworld in github.com/xiuxiubiu/micro-helloworld v0.0.0-20200806012757-eccf9d8d1a00
Response:  Hello John

其他语言客户端调用

micro/clients仓库中有各种语言和框架的客户端实现,它们可以很方便去连接Micro服务。

0

评论