Cleanup registry handler/service
This commit is contained in:
84
registry/service/handler/handler.go
Normal file
84
registry/service/handler/handler.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/errors"
|
||||
"github.com/micro/go-micro/registry"
|
||||
"github.com/micro/go-micro/registry/service"
|
||||
pb "github.com/micro/go-micro/registry/service/proto"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
// internal registry
|
||||
Registry registry.Registry
|
||||
}
|
||||
|
||||
func (r *Registry) GetService(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse) error {
|
||||
services, err := r.Registry.GetService(req.Service)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
for _, srv := range services {
|
||||
rsp.Services = append(rsp.Services, service.ToProto(srv))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Register(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error {
|
||||
var regOpts []registry.RegisterOption
|
||||
if req.Options != nil {
|
||||
ttl := time.Duration(req.Options.Ttl) * time.Second
|
||||
regOpts = append(regOpts, registry.RegisterTTL(ttl))
|
||||
}
|
||||
|
||||
err := r.Registry.Register(service.ToService(req), regOpts...)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Deregister(ctx context.Context, req *pb.Service, rsp *pb.EmptyResponse) error {
|
||||
err := r.Registry.Deregister(service.ToService(req))
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) ListServices(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
|
||||
services, err := r.Registry.ListServices()
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
for _, srv := range services {
|
||||
rsp.Services = append(rsp.Services, service.ToProto(srv))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Watch(ctx context.Context, req *pb.WatchRequest, rsp pb.Registry_WatchStream) error {
|
||||
watcher, err := r.Registry.Watch(registry.WatchService(req.Service))
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
|
||||
for {
|
||||
next, err := watcher.Next()
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
err = rsp.Send(&pb.Result{
|
||||
Action: next.Action,
|
||||
Service: service.ToProto(next.Service),
|
||||
})
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.registry", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
224
registry/service/proto/registry.micro.go
Normal file
224
registry/service/proto/registry.micro.go
Normal file
@@ -0,0 +1,224 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: micro/go-micro/registry/service/proto/registry.proto
|
||||
|
||||
package go_micro_registry
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
client "github.com/micro/go-micro/client"
|
||||
server "github.com/micro/go-micro/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Client API for Registry service
|
||||
|
||||
type RegistryService interface {
|
||||
GetService(ctx context.Context, in *GetRequest, opts ...client.CallOption) (*GetResponse, error)
|
||||
Register(ctx context.Context, in *Service, opts ...client.CallOption) (*EmptyResponse, error)
|
||||
Deregister(ctx context.Context, in *Service, opts ...client.CallOption) (*EmptyResponse, error)
|
||||
ListServices(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error)
|
||||
Watch(ctx context.Context, in *WatchRequest, opts ...client.CallOption) (Registry_WatchService, error)
|
||||
}
|
||||
|
||||
type registryService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewRegistryService(name string, c client.Client) RegistryService {
|
||||
if c == nil {
|
||||
c = client.NewClient()
|
||||
}
|
||||
if len(name) == 0 {
|
||||
name = "go.micro.registry"
|
||||
}
|
||||
return ®istryService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *registryService) GetService(ctx context.Context, in *GetRequest, opts ...client.CallOption) (*GetResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Registry.GetService", in)
|
||||
out := new(GetResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *registryService) Register(ctx context.Context, in *Service, opts ...client.CallOption) (*EmptyResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Registry.Register", in)
|
||||
out := new(EmptyResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *registryService) Deregister(ctx context.Context, in *Service, opts ...client.CallOption) (*EmptyResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Registry.Deregister", in)
|
||||
out := new(EmptyResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *registryService) ListServices(ctx context.Context, in *ListRequest, opts ...client.CallOption) (*ListResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Registry.ListServices", in)
|
||||
out := new(ListResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *registryService) Watch(ctx context.Context, in *WatchRequest, opts ...client.CallOption) (Registry_WatchService, error) {
|
||||
req := c.c.NewRequest(c.name, "Registry.Watch", &WatchRequest{})
|
||||
stream, err := c.c.Stream(ctx, req, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := stream.Send(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ®istryServiceWatch{stream}, nil
|
||||
}
|
||||
|
||||
type Registry_WatchService interface {
|
||||
SendMsg(interface{}) error
|
||||
RecvMsg(interface{}) error
|
||||
Close() error
|
||||
Recv() (*Result, error)
|
||||
}
|
||||
|
||||
type registryServiceWatch struct {
|
||||
stream client.Stream
|
||||
}
|
||||
|
||||
func (x *registryServiceWatch) Close() error {
|
||||
return x.stream.Close()
|
||||
}
|
||||
|
||||
func (x *registryServiceWatch) SendMsg(m interface{}) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *registryServiceWatch) RecvMsg(m interface{}) error {
|
||||
return x.stream.Recv(m)
|
||||
}
|
||||
|
||||
func (x *registryServiceWatch) Recv() (*Result, error) {
|
||||
m := new(Result)
|
||||
err := x.stream.Recv(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Server API for Registry service
|
||||
|
||||
type RegistryHandler interface {
|
||||
GetService(context.Context, *GetRequest, *GetResponse) error
|
||||
Register(context.Context, *Service, *EmptyResponse) error
|
||||
Deregister(context.Context, *Service, *EmptyResponse) error
|
||||
ListServices(context.Context, *ListRequest, *ListResponse) error
|
||||
Watch(context.Context, *WatchRequest, Registry_WatchStream) error
|
||||
}
|
||||
|
||||
func RegisterRegistryHandler(s server.Server, hdlr RegistryHandler, opts ...server.HandlerOption) error {
|
||||
type registry interface {
|
||||
GetService(ctx context.Context, in *GetRequest, out *GetResponse) error
|
||||
Register(ctx context.Context, in *Service, out *EmptyResponse) error
|
||||
Deregister(ctx context.Context, in *Service, out *EmptyResponse) error
|
||||
ListServices(ctx context.Context, in *ListRequest, out *ListResponse) error
|
||||
Watch(ctx context.Context, stream server.Stream) error
|
||||
}
|
||||
type Registry struct {
|
||||
registry
|
||||
}
|
||||
h := ®istryHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Registry{h}, opts...))
|
||||
}
|
||||
|
||||
type registryHandler struct {
|
||||
RegistryHandler
|
||||
}
|
||||
|
||||
func (h *registryHandler) GetService(ctx context.Context, in *GetRequest, out *GetResponse) error {
|
||||
return h.RegistryHandler.GetService(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *registryHandler) Register(ctx context.Context, in *Service, out *EmptyResponse) error {
|
||||
return h.RegistryHandler.Register(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *registryHandler) Deregister(ctx context.Context, in *Service, out *EmptyResponse) error {
|
||||
return h.RegistryHandler.Deregister(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *registryHandler) ListServices(ctx context.Context, in *ListRequest, out *ListResponse) error {
|
||||
return h.RegistryHandler.ListServices(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *registryHandler) Watch(ctx context.Context, stream server.Stream) error {
|
||||
m := new(WatchRequest)
|
||||
if err := stream.Recv(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return h.RegistryHandler.Watch(ctx, m, ®istryWatchStream{stream})
|
||||
}
|
||||
|
||||
type Registry_WatchStream interface {
|
||||
SendMsg(interface{}) error
|
||||
RecvMsg(interface{}) error
|
||||
Close() error
|
||||
Send(*Result) error
|
||||
}
|
||||
|
||||
type registryWatchStream struct {
|
||||
stream server.Stream
|
||||
}
|
||||
|
||||
func (x *registryWatchStream) Close() error {
|
||||
return x.stream.Close()
|
||||
}
|
||||
|
||||
func (x *registryWatchStream) SendMsg(m interface{}) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *registryWatchStream) RecvMsg(m interface{}) error {
|
||||
return x.stream.Recv(m)
|
||||
}
|
||||
|
||||
func (x *registryWatchStream) Send(m *Result) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
1002
registry/service/proto/registry.pb.go
Normal file
1002
registry/service/proto/registry.pb.go
Normal file
File diff suppressed because it is too large
Load Diff
98
registry/service/proto/registry.proto
Normal file
98
registry/service/proto/registry.proto
Normal file
@@ -0,0 +1,98 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package go.micro.registry;
|
||||
|
||||
service Registry {
|
||||
rpc GetService(GetRequest) returns (GetResponse) {};
|
||||
rpc Register(Service) returns (EmptyResponse) {};
|
||||
rpc Deregister(Service) returns (EmptyResponse) {};
|
||||
rpc ListServices(ListRequest) returns (ListResponse) {};
|
||||
rpc Watch(WatchRequest) returns (stream Result) {};
|
||||
}
|
||||
|
||||
// Service represents a go-micro service
|
||||
message Service {
|
||||
string name = 1;
|
||||
string version = 2;
|
||||
map<string,string> metadata = 3;
|
||||
repeated Endpoint endpoints = 4;
|
||||
repeated Node nodes = 5;
|
||||
Options options = 6;
|
||||
}
|
||||
|
||||
// Node represents the node the service is on
|
||||
message Node {
|
||||
string id = 1;
|
||||
string address = 2;
|
||||
int64 port = 3;
|
||||
map<string,string> metadata = 4;
|
||||
}
|
||||
|
||||
// Endpoint is a endpoint provided by a service
|
||||
message Endpoint {
|
||||
string name = 1;
|
||||
Value request = 2;
|
||||
Value response = 3;
|
||||
map<string, string> metadata = 4;
|
||||
}
|
||||
|
||||
// Value is an opaque value for a request or response
|
||||
message Value {
|
||||
string name = 1;
|
||||
string type = 2;
|
||||
repeated Value values = 3;
|
||||
}
|
||||
|
||||
// Options are registry options
|
||||
message Options {
|
||||
int64 ttl = 1;
|
||||
}
|
||||
|
||||
// Result is returns by the watcher
|
||||
message Result {
|
||||
string action = 1; // create, update, delete
|
||||
Service service = 2;
|
||||
int64 timestamp = 3; // unix timestamp
|
||||
}
|
||||
|
||||
message EmptyResponse {}
|
||||
|
||||
message GetRequest {
|
||||
string service = 1;
|
||||
}
|
||||
|
||||
message GetResponse {
|
||||
repeated Service services = 1;
|
||||
}
|
||||
|
||||
message ListRequest {
|
||||
// TODO: filtering
|
||||
}
|
||||
|
||||
message ListResponse {
|
||||
repeated Service services = 1;
|
||||
}
|
||||
|
||||
message WatchRequest {
|
||||
// service is optional
|
||||
string service = 1;
|
||||
}
|
||||
|
||||
// EventType defines the type of event
|
||||
enum EventType {
|
||||
Create = 0;
|
||||
Delete = 1;
|
||||
Update = 2;
|
||||
}
|
||||
|
||||
// Event is registry event
|
||||
message Event {
|
||||
// Event Id
|
||||
string id = 1;
|
||||
// type of event
|
||||
EventType type = 2;
|
||||
// unix timestamp of event
|
||||
int64 timestamp = 3;
|
||||
// service entry
|
||||
Service service = 4;
|
||||
}
|
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/micro/go-micro/client"
|
||||
"github.com/micro/go-micro/registry"
|
||||
pb "github.com/micro/go-micro/registry/proto"
|
||||
pb "github.com/micro/go-micro/registry/service/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@@ -2,7 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/registry"
|
||||
pb "github.com/micro/go-micro/registry/proto"
|
||||
pb "github.com/micro/go-micro/registry/service/proto"
|
||||
)
|
||||
|
||||
func values(v []*registry.Value) []*pb.Value {
|
||||
|
@@ -2,7 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"github.com/micro/go-micro/registry"
|
||||
pb "github.com/micro/go-micro/registry/proto"
|
||||
pb "github.com/micro/go-micro/registry/service/proto"
|
||||
)
|
||||
|
||||
type serviceWatcher struct {
|
||||
|
Reference in New Issue
Block a user