example

package
v0.0.0-...-7d51ed3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 18 Imported by: 0

README

Example

This is an example directory which has a simple application used for starting and running the example demo.

Build

If you need to rebuild the example source, run

# in the root directory containing the `Makefile` run
$ make generate

Running the example app

The example application will start a NATS server running on the default port 4222.

# assuming that you are in the example directory
# this example app will exit with CTRL+C or will self exit after 5s
$ go run ./cmd

Documentation

Index

Constants

View Source
const (
	Greeter_SayHello_FullMethodName      = "/example.Greeter/SayHello"
	Greeter_SayHelloAgain_FullMethodName = "/example.Greeter/SayHelloAgain"
	Greeter_SayGoodbye_FullMethodName    = "/example.Greeter/SayGoodbye"
	Greeter_SaveMetadata_FullMethodName  = "/example.Greeter/SaveMetadata"
)

Variables

View Source
var File_example_proto protoreflect.FileDescriptor
View Source
var File_messages_proto protoreflect.FileDescriptor
View Source
var Greeter_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "example.Greeter",
	HandlerType: (*GreeterServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "SayHello",
			Handler:    _Greeter_SayHello_Handler,
		},
		{
			MethodName: "SayHelloAgain",
			Handler:    _Greeter_SayHelloAgain_Handler,
		},
		{
			MethodName: "SayGoodbye",
			Handler:    _Greeter_SayGoodbye_Handler,
		},
		{
			MethodName: "SaveMetadata",
			Handler:    _Greeter_SaveMetadata_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "example.proto",
}

Greeter_ServiceDesc is the grpc.ServiceDesc for Greeter service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

Functions

func NewNATSGRPCClientToGreeterServer

func NewNATSGRPCClientToGreeterServer(ctx context.Context, nc *nats.Conn, client GreeterClient, cfg micro.Config, opts ...ConcurrentServiceOption) (micro.Service, error)

NewNATSGRPCClientToGreeterServer returns the gRPC server wrapping a gRPC client as a NATS micro service.

Example:

nc, err := nats.Connect(ns.ClientURL())
if err != nil {
  panic(err)
}

var opts := []grpc.DailOption

conn, err := grpc.NewClient("localhost:1234", opts...)
if err != nil {
    panic(err)
}

defer conn.Close()

client := NewGreeterClient(conn)

cfg := micro.Config{
    Name: "GreeterWrapper-Demo",
    Version: "1.0.0",
    QueueGroup: "example",
    Description: "NATS micro service adaptor wrapping GreeterClient",
}

mc, err := NewNATSGRPCClientToGreeterServer(context.Background(), nc, client, cfg)
if err != nil {
  panic(err)
}

fmt.Printf("%s -> %s\n", mc.Info().Name, mc.Info().ID)

func NewNATSGreeterServer

func NewNATSGreeterServer(ctx context.Context, nc *nats.Conn, server GreeterServer, cfg micro.Config, opts ...ConcurrentServiceOption) (micro.Service, error)

NewNATSGreeterServer returns the gRPC server as a NATS micro service.

Example:

nc, err := nats.Connect(ns.ClientURL())
if err != nil {
  panic(err)
}

cfg := micro.Config{
    Name: "GreeterServer-Demo",
    Version: "1.0.0",
    QueueGroup: "example",
    Description: "NATS micro service adaptor wrapping GreeterServer",
}

mc, err := NewNATSGreeterServer(context.Background(), nc, GreeterService{}, cfg)
if err != nil {
  panic(err)
}

fmt.Printf("%s -> %s\n", mc.Info().Name, mc.Info().ID)

func RegisterGreeterServer

func RegisterGreeterServer(s grpc.ServiceRegistrar, srv GreeterServer)

Types

type ConcurrentService

type ConcurrentService struct {
	// contains filtered or unexported fields
}

ConcurrentService is a wrapper around the micro.Service interface, extending with additional functionality.

func (*ConcurrentService) AddEndpoint

func (m *ConcurrentService) AddEndpoint(name string, handler micro.Handler, opts ...micro.EndpointOpt) error

AddEndpoint registers endpoint with given name on a specific subject.

func (*ConcurrentService) AddGroup

func (m *ConcurrentService) AddGroup(group string, opts ...micro.GroupOpt) micro.Group

AddGroup returns a Group interface, allowing for more complex endpoint topologies. A group can be used to register endpoints with given prefix.

func (*ConcurrentService) Info

func (m *ConcurrentService) Info() micro.Info

Info returns the service info.

func (*ConcurrentService) Reset

func (m *ConcurrentService) Reset()

Reset resets all statistics (for all endpoints) on a service instance.

func (*ConcurrentService) Stats

func (m *ConcurrentService) Stats() micro.Stats

Stats returns statistics for the service endpoint and all monitoring endpoints.

func (*ConcurrentService) Stop

func (m *ConcurrentService) Stop() error

Stop drains the endpoint subscriptions and marks the service as stopped.

func (*ConcurrentService) Stopped

func (m *ConcurrentService) Stopped() bool

Stopped informs whether [Stop] was executed on the service.

type ConcurrentServiceOption

type ConcurrentServiceOption func(*ConcurrentService)

ConcurrentServiceOption is a function used to configure a ConcurrentService.

func WithConcurrentJobs

func WithConcurrentJobs(jobs int) ConcurrentServiceOption

WithConcurrentJobs sets the number of concurrent jobs to be executed.

type GreeterClient

type GreeterClient interface {
	// Sends a greeting
	SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
	// Sends another greeting
	SayHelloAgain(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)
	SayGoodbye(ctx context.Context, in *SayGoodbyeRequest, opts ...grpc.CallOption) (*SayGoodbyeReply, error)
	SaveMetadata(ctx context.Context, in *structpb.Struct, opts ...grpc.CallOption) (*structpb.Struct, error)
}

GreeterClient is the client API for Greeter service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

The greeting service definition.

func NewGreeterClient

func NewGreeterClient(cc grpc.ClientConnInterface) GreeterClient

type GreeterServer

type GreeterServer interface {
	// Sends a greeting
	SayHello(context.Context, *HelloRequest) (*HelloReply, error)
	// Sends another greeting
	SayHelloAgain(context.Context, *HelloRequest) (*HelloReply, error)
	SayGoodbye(context.Context, *SayGoodbyeRequest) (*SayGoodbyeReply, error)
	SaveMetadata(context.Context, *structpb.Struct) (*structpb.Struct, error)
	// contains filtered or unexported methods
}

GreeterServer is the server API for Greeter service. All implementations must embed UnimplementedGreeterServer for forward compatibility.

The greeting service definition.

type HelloReply

type HelloReply struct {
	Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
	// contains filtered or unexported fields
}

The response message containing the greetings

func (*HelloReply) Descriptor deprecated

func (*HelloReply) Descriptor() ([]byte, []int)

Deprecated: Use HelloReply.ProtoReflect.Descriptor instead.

func (*HelloReply) GetMessage

func (x *HelloReply) GetMessage() string

func (*HelloReply) ProtoMessage

func (*HelloReply) ProtoMessage()

func (*HelloReply) ProtoReflect

func (x *HelloReply) ProtoReflect() protoreflect.Message

func (*HelloReply) Reset

func (x *HelloReply) Reset()

func (*HelloReply) String

func (x *HelloReply) String() string

type HelloRequest

type HelloRequest struct {
	Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	// contains filtered or unexported fields
}

The request message containing the user's name.

func (*HelloRequest) Descriptor deprecated

func (*HelloRequest) Descriptor() ([]byte, []int)

Deprecated: Use HelloRequest.ProtoReflect.Descriptor instead.

func (*HelloRequest) GetName

func (x *HelloRequest) GetName() string

func (*HelloRequest) ProtoMessage

func (*HelloRequest) ProtoMessage()

func (*HelloRequest) ProtoReflect

func (x *HelloRequest) ProtoReflect() protoreflect.Message

func (*HelloRequest) Reset

func (x *HelloRequest) Reset()

func (*HelloRequest) String

func (x *HelloRequest) String() string

type JobHandler

type JobHandler struct {
	// contains filtered or unexported fields
}

JobHandler is job handler for the concurrent service.

type NATSGreeterClient

type NATSGreeterClient struct {
	// contains filtered or unexported fields
}

NATSGreeterClient is a client connecting to a NATS GreeterServer.

func NewNATSGreeterClient

func NewNATSGreeterClient(nc *nats.Conn, name string) *NATSGreeterClient

NewNATSGreeterClient returns a new GreeterServer client. Example:

nc, err := nats.Connect(ns.ClientURL())
if err != nil {
  panic(err)
}

client := NewNATSGreeterClient(nc, "example-service-name")

func (*NATSGreeterClient) SaveMetadata

func (c *NATSGreeterClient) SaveMetadata(ctx context.Context, req *structpb.Struct) (*structpb.Struct, error)

func (*NATSGreeterClient) SayGoodbye

func (*NATSGreeterClient) SayHello

func (c *NATSGreeterClient) SayHello(ctx context.Context, req *HelloRequest) (*HelloReply, error)

Sends a greeting

func (*NATSGreeterClient) SayHelloAgain

func (c *NATSGreeterClient) SayHelloAgain(ctx context.Context, req *HelloRequest) (*HelloReply, error)

Sends another greeting

type SayGoodbyeReply

type SayGoodbyeReply struct {
	Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
	// contains filtered or unexported fields
}

func (*SayGoodbyeReply) Descriptor deprecated

func (*SayGoodbyeReply) Descriptor() ([]byte, []int)

Deprecated: Use SayGoodbyeReply.ProtoReflect.Descriptor instead.

func (*SayGoodbyeReply) GetMessage

func (x *SayGoodbyeReply) GetMessage() string

func (*SayGoodbyeReply) ProtoMessage

func (*SayGoodbyeReply) ProtoMessage()

func (*SayGoodbyeReply) ProtoReflect

func (x *SayGoodbyeReply) ProtoReflect() protoreflect.Message

func (*SayGoodbyeReply) Reset

func (x *SayGoodbyeReply) Reset()

func (*SayGoodbyeReply) String

func (x *SayGoodbyeReply) String() string

type SayGoodbyeRequest

type SayGoodbyeRequest struct {
	Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	// contains filtered or unexported fields
}

func (*SayGoodbyeRequest) Descriptor deprecated

func (*SayGoodbyeRequest) Descriptor() ([]byte, []int)

Deprecated: Use SayGoodbyeRequest.ProtoReflect.Descriptor instead.

func (*SayGoodbyeRequest) GetName

func (x *SayGoodbyeRequest) GetName() string

func (*SayGoodbyeRequest) ProtoMessage

func (*SayGoodbyeRequest) ProtoMessage()

func (*SayGoodbyeRequest) ProtoReflect

func (x *SayGoodbyeRequest) ProtoReflect() protoreflect.Message

func (*SayGoodbyeRequest) Reset

func (x *SayGoodbyeRequest) Reset()

func (*SayGoodbyeRequest) String

func (x *SayGoodbyeRequest) String() string

type UnimplementedGreeterServer

type UnimplementedGreeterServer struct{}

UnimplementedGreeterServer must be embedded to have forward compatible implementations.

NOTE: this should be embedded by value instead of pointer to avoid a nil pointer dereference when methods are called.

func (UnimplementedGreeterServer) SaveMetadata

func (UnimplementedGreeterServer) SayGoodbye

func (UnimplementedGreeterServer) SayHello

func (UnimplementedGreeterServer) SayHelloAgain

type UnsafeGreeterServer

type UnsafeGreeterServer interface {
	// contains filtered or unexported methods
}

UnsafeGreeterServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to GreeterServer will result in compilation errors.

Directories

Path Synopsis
This is a demo application impleneting the gRPC service and then running an embeded NATS server.
This is a demo application impleneting the gRPC service and then running an embeded NATS server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL