mirror of
https://github.com/go-micro/go-micro.git
synced 2025-11-29 21:47:44 +02:00
Add examples
This commit is contained in:
27
examples/helloworld/README.md
Normal file
27
examples/helloworld/README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Hello World
|
||||
|
||||
This is hello world using micro
|
||||
|
||||
## Contents
|
||||
|
||||
- main.go - is the main definition of the service, handler and client
|
||||
- proto - contains the protobuf definition of the API
|
||||
|
||||
## Dependencies
|
||||
|
||||
Install the following
|
||||
|
||||
- [micro](https://github.com/micro/micro)
|
||||
- [protoc-gen-micro](https://github.com/micro/protoc-gen-micro)
|
||||
|
||||
## Run Service
|
||||
|
||||
```shell
|
||||
micro run service --name helloworld
|
||||
```
|
||||
|
||||
## Query Service
|
||||
|
||||
```
|
||||
micro call helloworld Greeter.Hello '{"name": "John"}'
|
||||
```
|
||||
30
examples/helloworld/main.go
Normal file
30
examples/helloworld/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
pb "github.com/micro/examples/helloworld/proto"
|
||||
"github.com/micro/go-micro/v2"
|
||||
)
|
||||
|
||||
type Greeter struct{}
|
||||
|
||||
func (g *Greeter) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
||||
rsp.Greeting = "Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
service := micro.NewService(
|
||||
micro.Name("helloworld"),
|
||||
)
|
||||
|
||||
service.Init()
|
||||
|
||||
pb.RegisterGreeterHandler(service.Server(), new(Greeter))
|
||||
|
||||
if err := service.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
123
examples/helloworld/proto/greeter.pb.go
Normal file
123
examples/helloworld/proto/greeter.pb.go
Normal file
@@ -0,0 +1,123 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// source: github.com/micro/examples/helloworld/proto/greeter.proto
|
||||
|
||||
package greeter
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
// 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
|
||||
|
||||
type Request struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Request) Reset() { *m = Request{} }
|
||||
func (m *Request) String() string { return proto.CompactTextString(m) }
|
||||
func (*Request) ProtoMessage() {}
|
||||
func (*Request) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_f59dc6c921369088, []int{0}
|
||||
}
|
||||
|
||||
func (m *Request) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Request.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Request.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Request) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Request.Merge(m, src)
|
||||
}
|
||||
func (m *Request) XXX_Size() int {
|
||||
return xxx_messageInfo_Request.Size(m)
|
||||
}
|
||||
func (m *Request) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Request.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Request proto.InternalMessageInfo
|
||||
|
||||
func (m *Request) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Greeting string `protobuf:"bytes,2,opt,name=greeting,proto3" json:"greeting,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
XXX_sizecache int32 `json:"-"`
|
||||
}
|
||||
|
||||
func (m *Response) Reset() { *m = Response{} }
|
||||
func (m *Response) String() string { return proto.CompactTextString(m) }
|
||||
func (*Response) ProtoMessage() {}
|
||||
func (*Response) Descriptor() ([]byte, []int) {
|
||||
return fileDescriptor_f59dc6c921369088, []int{1}
|
||||
}
|
||||
|
||||
func (m *Response) XXX_Unmarshal(b []byte) error {
|
||||
return xxx_messageInfo_Response.Unmarshal(m, b)
|
||||
}
|
||||
func (m *Response) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
|
||||
return xxx_messageInfo_Response.Marshal(b, m, deterministic)
|
||||
}
|
||||
func (m *Response) XXX_Merge(src proto.Message) {
|
||||
xxx_messageInfo_Response.Merge(m, src)
|
||||
}
|
||||
func (m *Response) XXX_Size() int {
|
||||
return xxx_messageInfo_Response.Size(m)
|
||||
}
|
||||
func (m *Response) XXX_DiscardUnknown() {
|
||||
xxx_messageInfo_Response.DiscardUnknown(m)
|
||||
}
|
||||
|
||||
var xxx_messageInfo_Response proto.InternalMessageInfo
|
||||
|
||||
func (m *Response) GetGreeting() string {
|
||||
if m != nil {
|
||||
return m.Greeting
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Request)(nil), "Request")
|
||||
proto.RegisterType((*Response)(nil), "Response")
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterFile("github.com/micro/examples/helloworld/proto/greeter.proto", fileDescriptor_f59dc6c921369088)
|
||||
}
|
||||
|
||||
var fileDescriptor_f59dc6c921369088 = []byte{
|
||||
// 164 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x3c, 0x8d, 0x4d, 0x0a, 0xc2, 0x30,
|
||||
0x10, 0x46, 0xad, 0xa8, 0x6d, 0x67, 0x99, 0x55, 0x29, 0x28, 0xd2, 0x85, 0xe8, 0x26, 0x01, 0xdd,
|
||||
0x78, 0x03, 0x5d, 0xf7, 0x06, 0x6d, 0x1d, 0xd2, 0x40, 0x92, 0x89, 0x49, 0x8a, 0x1e, 0x5f, 0x8c,
|
||||
0x3f, 0xbb, 0x79, 0x30, 0xdf, 0x7b, 0x70, 0x96, 0x2a, 0x8e, 0x53, 0xcf, 0x07, 0x32, 0xc2, 0xa8,
|
||||
0xc1, 0x93, 0xc0, 0x67, 0x67, 0x9c, 0xc6, 0x20, 0x46, 0xd4, 0x9a, 0x1e, 0xe4, 0xf5, 0x4d, 0x38,
|
||||
0x4f, 0x91, 0x84, 0xf4, 0x88, 0x11, 0x3d, 0x4f, 0xd4, 0xac, 0x21, 0x6f, 0xf1, 0x3e, 0x61, 0x88,
|
||||
0x8c, 0xc1, 0xc2, 0x76, 0x06, 0xab, 0x6c, 0x9b, 0xed, 0xcb, 0x36, 0xdd, 0xcd, 0x0e, 0x8a, 0x16,
|
||||
0x83, 0x23, 0x1b, 0x90, 0xd5, 0x50, 0xa4, 0xad, 0xb2, 0xb2, 0x9a, 0xa7, 0x9f, 0x3f, 0x1f, 0x0f,
|
||||
0x90, 0x5f, 0x3e, 0x5e, 0xb6, 0x81, 0xe5, 0xf5, 0xdd, 0x64, 0x05, 0xff, 0x9a, 0xeb, 0x92, 0xff,
|
||||
0x24, 0xcd, 0xac, 0x5f, 0xa5, 0xf0, 0xe9, 0x15, 0x00, 0x00, 0xff, 0xff, 0x54, 0xa4, 0xf7, 0x61,
|
||||
0xb4, 0x00, 0x00, 0x00,
|
||||
}
|
||||
85
examples/helloworld/proto/greeter.pb.micro.go
Normal file
85
examples/helloworld/proto/greeter.pb.micro.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: github.com/micro/examples/helloworld/proto/greeter.proto
|
||||
|
||||
package greeter
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
client "github.com/micro/go-micro/v2/client"
|
||||
server "github.com/micro/go-micro/v2/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 Greeter service
|
||||
|
||||
type GreeterService interface {
|
||||
Hello(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error)
|
||||
}
|
||||
|
||||
type greeterService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewGreeterService(name string, c client.Client) GreeterService {
|
||||
return &greeterService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *greeterService) Hello(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error) {
|
||||
req := c.c.NewRequest(c.name, "Greeter.Hello", in)
|
||||
out := new(Response)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Greeter service
|
||||
|
||||
type GreeterHandler interface {
|
||||
Hello(context.Context, *Request, *Response) error
|
||||
}
|
||||
|
||||
func RegisterGreeterHandler(s server.Server, hdlr GreeterHandler, opts ...server.HandlerOption) error {
|
||||
type greeter interface {
|
||||
Hello(ctx context.Context, in *Request, out *Response) error
|
||||
}
|
||||
type Greeter struct {
|
||||
greeter
|
||||
}
|
||||
h := &greeterHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Greeter{h}, opts...))
|
||||
}
|
||||
|
||||
type greeterHandler struct {
|
||||
GreeterHandler
|
||||
}
|
||||
|
||||
func (h *greeterHandler) Hello(ctx context.Context, in *Request, out *Response) error {
|
||||
return h.GreeterHandler.Hello(ctx, in, out)
|
||||
}
|
||||
13
examples/helloworld/proto/greeter.proto
Normal file
13
examples/helloworld/proto/greeter.proto
Normal file
@@ -0,0 +1,13 @@
|
||||
syntax = "proto3";
|
||||
|
||||
service Greeter {
|
||||
rpc Hello(Request) returns (Response) {}
|
||||
}
|
||||
|
||||
message Request {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string greeting = 2;
|
||||
}
|
||||
Reference in New Issue
Block a user