06-GoMicro安装

最简单的Service搭建

为了让客户端能找到,Service需要一个服务发现的模块。Micro默认使用Consul,你也可以使用etcd,kubernetes…。这里使用Consul。

安装Consul

https://www.consul.io/intro/getting-started/install.html 下载自己的平台下编译好的二进制文件,然后放到PATH能检索到的位置。可以使用命令行测试consul --version
然后,我们先运行一个服务发现的实例:consul agent -dev -advertise=127.0.0.1

编写服务

默认使用Protobuf,需要先安装好环境,https://github.com/google/protobuf/releases 下载,放到%GOPATH/bin%下。

官方go插件

安装Go的插件go get -u github.com/golang/protobuf/protoc-gen-go,默认生成在%GOPATH/bin%下。
这种方式安装的Protobuf是针对一般使用场景,但是缺少部分Micro实现,所以需要使用Micro方式。

Micro-go插件

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

// 主要不同是需要 plugins=micro 这个选项,官方版没有这个功能 
protoc -I. --go_out=plugins=micro:. *.proto

Go-Micro 服务

// server.go
package main

import (
    "fmt"
    "os"

    "github.com/micro/cli"
    proto "github.com/micro/examples/service/proto"
    "github.com/micro/go-micro"
    "golang.org/x/net/context"
)

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"),
        micro.Version("v1.0"),
        micro.Metadata(map[string]string{
            "type": "helloworld",
        }),
    )

    // Init will parse the command line flags. Any flags set will
    // override the above settings. Options defined here will
    // override anything set on the command line.
    service.Init(
        // Add runtime action
        // We could actually do this above
        micro.Action(func(c *cli.Context) {
            if c.Bool("run_client") {
                //runClient(service)
                os.Exit(0)
            }
        }),
    )

    // By default we'll run the server unless the flags catch us

    // Setup the server

    // Register handler
    proto.RegisterGreeterHandler(service.Server(), new(Greeter))

    // Run the server
    if err := service.Run(); err != nil {
        fmt.Println(err)
    }
}

客户端更简单:

// client.go
package main

import (
    "fmt"

    "github.com/micro/cli"
    proto "github.com/micro/examples/service/proto"
    "github.com/micro/go-micro"
    "golang.org/x/net/context"
)
// Setup and the client
func runClient(service micro.Service) {
    // Create new greeter client
    greeter := proto.NewGreeterClient("greeter", service.Client())

    // Call the greeter
    rsp, err := greeter.Hello(context.TODO(), &proto.HelloRequest{Name: "John"})
    if err != nil {
        fmt.Println(err)
        return
    }

    // Print response
    fmt.Println(rsp.Greeting)
}

func main(){
        // Create a new service. Optionally include some options here.
    service := micro.NewService(
        micro.Name("greeter"),
        micro.Version("v1.0"),
        micro.Metadata(map[string]string{
            "type": "helloworld",
        }),

        // Setup some flags. Specify --run_client to run the client

        // Add runtime flags
        // We could do this below too
        micro.Flags(cli.BoolFlag{
            Name:  "run_client",
            Usage: "Launch the client",
        }),
    )
    runClient(service)
}

执行测试:

go run server.go
// 稍等片刻 
go run client.go

Web页面: http://localhost:8500/ui
在浏览器中输入 http://127.0.0.1:8500/v1/health/service/greeter 能看到服务的状态。

Micro创建模版

This directory contains templates generated with the micro new [service] command.
Find out more on the usage at micro/new.

API

micro new --type api --alias template github.com/micro/examples/template/api

FNC

micro new --type fnc --alias template github.com/micro/examples/template/fnc

SRV

micro new --type srv --alias template github.com/micro/examples/template/srv

WEB

micro new --type web --alias template github.com/micro/examples/template/web

总结

微服务是一个庞大的话题,它包含几个核心的问题点:
1. 服务发现与治理
2. 负载均衡,高可用,健康检查
3. RPC,IPC,HTTP,编解码
4. 不用语言下的开发和集成问题
5. 服务运维,包含看板等

其中,1、2有很多可用/参考的方案。3、4是开发效率和可扩展性的问题。5是运维以及是否能长期使用的问题。

后面,我们从每个方面一一学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值