1.系统: window10
2.micro 版本
C:\Users\Administrator>micro --version
micro version 1.10.0
3.go版本
C:\Users\Administrator>go version
go version go1.12.9 windows/amd64
4.protoc版本
C:\Users\Administrator>protoc --version
libprotoc 3.9.2
生成go-micro的protoc文件
syntax = "proto3";
package go.micro.srv.greeter;
service Say {
rpc Hello(Request) returns (Response) {}
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
main函数
package main
import (
"log"
"time"
hello "grpcdemo/grpc/server/srv/proto"
"github.com/micro/go-micro"
"github.com/micro/go-micro/service/grpc"
"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.demo"),
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)
}
}
生成go-micro文件命令
protoc -IH:\downloads\protoc-3.9.2-win64\include -I. -ID:\workspace\src -ID:\workspace\src\github.com\grpc-ecosystem\grpc-gateway\third_party\googleapis --go_out=plugins=grpc:. --micro_out=. hello.proto
gateway相关
proto文件
syntax = "proto3";
package proto;
import "google/api/annotations.proto";
service Say {
rpc Hello(Request) returns (Response) {
option (google.api.http) = {
post: "/greeter/hello"
body: "*"
};
}
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
main.go
package main
import (
"flag"
"net/http"
"context"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
hello "grpcdemo/grpc/gateway/proto"
)
var (
// the go.micro.srv.greeter address
endpoint = flag.String("endpoint", "localhost:9090", "go.micro.srv.greeter address")
)
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := hello.RegisterSayHandlerFromEndpoint(ctx, mux, *endpoint, opts)
if err != nil {
return err
}
return http.ListenAndServe(":8080", mux)
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}
D:\workspace\src\grpcdemo\grpc\gateway\proto>protoc -IH:\downloads\protoc-3.9.2-win64\include -I. -ID:\workspace\src -ID:\workspace\src\github.com\grpc-ecosystem\grpc-gateway\third_party\googleapis --go_out=plugins=grpc:. --grpc-gateway_out=logtostderr=true:. hello.proto
1.启动consul作为注册中心
consul agent -dev
验证consul,访问地址http://localhost:8500/ui/dc1/services
启动服务
server
D:\workspace\src\grpcdemo\grpc\server\srv>go run main.go --registry consul --server_address localhost:9090
2019/10/01 19:35:50 Server [grpc] Listening on 127.0.0.1:9090
2019/10/01 19:35:50 Broker [http] Connected to [::]:62483
2019/10/01 19:35:50 Registering node: go.micro.srv.demo-2e486455-1176-44b4-b4fc-fbd68c3fbd57