go-micro入门

go-micro入门

(1.)使用go-grpc创建了一个微服务。

syntax = "proto3";

package go.micro.srv.greeter;

service Say {
	rpc Hello(Request) returns (Response) {}
}

message Request {
	string name = 1;
}

message Response {
	string msg = 1;
}

说明:
编译user.proto接口定义文件
protoc --proto_path=proto:. --micro_out=proto/ --go_out=proto/ proto/user.proto
protoc参数说明:
--proto_path - proto文件目录
--micro_out - 生成的micro源码保存目录
--go_out - 生成的go源码保存目录
proto/user.proto - 最后面的参数就是我们要编译的proto文件

(2.)服务如下:

package main

import (
	"log"
	"time"

	hello "github.com/micro/examples/greeter/srv/proto/hello"
	"github.com/micro/go-grpc"
	"github.com/micro/go-micro"

	"golang.org/x/net/context"
)

type Say struct{}

func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
	log.Print("Received Say.Hello request")
	rsp.Msg = "Hello " + req.Name
	return nil
}

func main() {
	service := grpc.NewService(
		micro.Name("go.micro.srv.greeter"),
		micro.RegisterTTL(time.Second*30),
		micro.RegisterInterval(time.Second*10),
	)

	// optionally setup command line usage
	service.Init()

	// Register Handlers
	hello.RegisterSayHandler(service.Server(), new(Say))

	// Run server
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}

(3.)创建客户端测试

// create the greeter client using the service name and client
greeter := proto.NewGreeterClient("greeter", service.Client())

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

fmt.Println(rsp.Greeter)

其他

微服务插件之RabbitMQ:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/micro/go-micro"
    "github.com/micro/go-micro/broker"
)

var (
    topic = "com.foo.topic"
)

func pub(brk broker.Broker) {
    i := 0
    for range time.Tick(time.Second) {
        // build a message
        msg := &broker.Message{
            Header: map[string]string{
                "id": fmt.Sprintf("%d", i),
            },
            Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())),
        }
        // publish it
        if err := brk.Publish(topic, msg); err != nil {
            log.Printf("[pub] failed: %v", err)
        } else {
            fmt.Println("[pub] pubbed message:", string(msg.Body))
        }
        i++
    }
}

func sub(brk broker.Broker) {
    // subscribe a topic with queue specified
    _, err := brk.Subscribe(topic, func(p broker.Event) error {
        fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
        return nil
    }, broker.Queue(topic))
    if err != nil {
        fmt.Println(err)
    }
}

func main() {
    // New Service
    service := micro.NewService(
        micro.Name("com.foo.broker.example"), // name the client service
    )
    // Initialise service
    service.Init(micro.AfterStart(func() error {
        brk := service.Options().Broker
        go sub(brk)
        go pub(brk)
        return nil
    }))

    service.Run()
}

相关连接

微服务插件之RabbitMQ: https://github.com/microhq/go-plugins/tree/master/broker/rabbitmq

【励志篇】: 古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。
原文地址:https://www.cnblogs.com/tomtellyou/p/14357092.html