mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
Change initialisation and add metadata
This commit is contained in:
parent
7aa2c82ced
commit
36b5ca46fe
@ -77,7 +77,7 @@ import (
|
||||
type Example struct{}
|
||||
|
||||
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
|
||||
md, _ := c.GetMetaData(ctx)
|
||||
md, _ := c.GetMetadata(ctx)
|
||||
log.Info("Received Example.Call request with metadata: %v", md)
|
||||
rsp.Msg = server.Id + ": Hello " + req.Name
|
||||
return nil
|
||||
|
@ -165,7 +165,7 @@ func (h *httpBroker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.WithMetaData(context.Background(), e.Header)
|
||||
ctx := c.WithMetadata(context.Background(), e.Header)
|
||||
|
||||
h.RLock()
|
||||
for _, subscriber := range h.subscribers[e.Message.Topic] {
|
||||
@ -208,7 +208,7 @@ func (h *httpBroker) Publish(ctx context.Context, topic string, body []byte) err
|
||||
Body: body,
|
||||
}
|
||||
|
||||
header, _ := c.GetMetaData(ctx)
|
||||
header, _ := c.GetMetadata(ctx)
|
||||
|
||||
b, err := json.Marshal(&envelope{
|
||||
header,
|
||||
|
@ -68,7 +68,7 @@ func (n *nbroker) Init() error {
|
||||
}
|
||||
|
||||
func (n *nbroker) Publish(ctx context.Context, topic string, body []byte) error {
|
||||
header, _ := c.GetMetaData(ctx)
|
||||
header, _ := c.GetMetadata(ctx)
|
||||
|
||||
message := &broker.Message{
|
||||
Id: uuid.NewUUID().String(),
|
||||
@ -93,7 +93,7 @@ func (n *nbroker) Subscribe(topic string, function func(context.Context, *broker
|
||||
if err := json.Unmarshal(msg.Data, &e); err != nil {
|
||||
return
|
||||
}
|
||||
ctx := c.WithMetaData(context.Background(), e.Header)
|
||||
ctx := c.WithMetadata(context.Background(), e.Header)
|
||||
function(ctx, e.Message)
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -96,7 +96,7 @@ func (r *rpcClient) call(ctx context.Context, address string, request Request, r
|
||||
Body: reqB.Bytes(),
|
||||
}
|
||||
|
||||
md, ok := c.GetMetaData(ctx)
|
||||
md, ok := c.GetMetadata(ctx)
|
||||
if ok {
|
||||
for k, v := range md {
|
||||
msg.Header[k] = v
|
||||
|
36
cmd/cmd.go
36
cmd/cmd.go
@ -30,12 +30,28 @@ import (
|
||||
|
||||
var (
|
||||
Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "server_name",
|
||||
EnvVar: "MICRO_SERVER_NAME",
|
||||
Usage: "Name of the server. go.micro.srv.example",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "server_id",
|
||||
EnvVar: "MICRO_SERVER_ID",
|
||||
Usage: "Id of the server. Auto-generated if not specified",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "server_address",
|
||||
EnvVar: "MICRO_SERVER_ADDRESS",
|
||||
Value: ":0",
|
||||
Usage: "Bind address for the server. 127.0.0.1:8080",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "server_metadata",
|
||||
EnvVar: "MICRO_SERVER_METADATA",
|
||||
Value: &cli.StringSlice{},
|
||||
Usage: "A list of key-value pairs defining metadata. version=1.0.0",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "broker",
|
||||
EnvVar: "MICRO_BROKER",
|
||||
@ -73,8 +89,6 @@ var (
|
||||
)
|
||||
|
||||
func Setup(c *cli.Context) error {
|
||||
server.Address = c.String("server_address")
|
||||
|
||||
bAddrs := strings.Split(c.String("broker_address"), ",")
|
||||
|
||||
switch c.String("broker") {
|
||||
@ -104,6 +118,24 @@ func Setup(c *cli.Context) error {
|
||||
transport.DefaultTransport = tnats.NewTransport(tAddrs)
|
||||
}
|
||||
|
||||
metadata := make(map[string]string)
|
||||
for _, d := range c.StringSlice("server_metadata") {
|
||||
var key, val string
|
||||
parts := strings.Split(d, "=")
|
||||
key = parts[0]
|
||||
if len(parts) > 1 {
|
||||
val = strings.Join(parts[1:], "=")
|
||||
}
|
||||
metadata[key] = val
|
||||
}
|
||||
|
||||
server.DefaultServer = server.NewServer(
|
||||
server.Name(c.String("server_name")),
|
||||
server.Id(c.String("server_id")),
|
||||
server.Address(c.String("server_address")),
|
||||
server.Metadata(metadata),
|
||||
)
|
||||
|
||||
client.DefaultClient = client.NewClient()
|
||||
|
||||
return nil
|
||||
|
@ -10,13 +10,13 @@ const (
|
||||
mdKey = key(0)
|
||||
)
|
||||
|
||||
type MetaData map[string]string
|
||||
type Metadata map[string]string
|
||||
|
||||
func GetMetaData(ctx context.Context) (MetaData, bool) {
|
||||
md, ok := ctx.Value(mdKey).(MetaData)
|
||||
func GetMetadata(ctx context.Context) (Metadata, bool) {
|
||||
md, ok := ctx.Value(mdKey).(Metadata)
|
||||
return md, ok
|
||||
}
|
||||
|
||||
func WithMetaData(ctx context.Context, md MetaData) context.Context {
|
||||
func WithMetadata(ctx context.Context, md Metadata) context.Context {
|
||||
return context.WithValue(ctx, mdKey, md)
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ func main() {
|
||||
})
|
||||
|
||||
// create context with metadata
|
||||
ctx := c.WithMetaData(context.Background(), map[string]string{
|
||||
ctx := c.WithMetadata(context.Background(), map[string]string{
|
||||
"X-User-Id": "john",
|
||||
"X-From-Id": "script",
|
||||
})
|
||||
|
@ -20,7 +20,7 @@ func pub() {
|
||||
tick := time.NewTicker(time.Second)
|
||||
i := 0
|
||||
for _ = range tick.C {
|
||||
ctx := c.WithMetaData(context.Background(), map[string]string{
|
||||
ctx := c.WithMetadata(context.Background(), map[string]string{
|
||||
"id": fmt.Sprintf("%d", i),
|
||||
})
|
||||
|
||||
@ -36,7 +36,7 @@ func pub() {
|
||||
|
||||
func sub() {
|
||||
_, err := broker.Subscribe(topic, func(ctx context.Context, msg *broker.Message) {
|
||||
md, _ := c.GetMetaData(ctx)
|
||||
md, _ := c.GetMetadata(ctx)
|
||||
fmt.Println("[sub] received message:", string(msg.Body), "context", md)
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -12,8 +12,8 @@ import (
|
||||
type Example struct{}
|
||||
|
||||
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
|
||||
md, _ := c.GetMetaData(ctx)
|
||||
md, _ := c.GetMetadata(ctx)
|
||||
log.Info("Received Example.Call request with metadata: %v", md)
|
||||
rsp.Msg = server.Id + ": Hello " + req.Name
|
||||
rsp.Msg = server.Config().Id() + ": Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
@ -11,10 +11,10 @@ func main() {
|
||||
// optionally setup command line usage
|
||||
cmd.Init()
|
||||
|
||||
server.Name = "go.micro.srv.example"
|
||||
|
||||
// Initialise Server
|
||||
server.Init()
|
||||
server.Init(
|
||||
server.Name("go.micro.srv.example"),
|
||||
)
|
||||
|
||||
// Register Handlers
|
||||
server.Register(
|
||||
|
@ -16,7 +16,7 @@ type consulRegistry struct {
|
||||
services map[string]*Service
|
||||
}
|
||||
|
||||
func encodeMetaData(md map[string]string) []string {
|
||||
func encodeMetadata(md map[string]string) []string {
|
||||
var tags []string
|
||||
for k, v := range md {
|
||||
if b, err := json.Marshal(map[string]string{
|
||||
@ -28,7 +28,7 @@ func encodeMetaData(md map[string]string) []string {
|
||||
return tags
|
||||
}
|
||||
|
||||
func decodeMetaData(tags []string) map[string]string {
|
||||
func decodeMetadata(tags []string) map[string]string {
|
||||
md := make(map[string]string)
|
||||
for _, tag := range tags {
|
||||
var kv map[string]string
|
||||
@ -81,7 +81,7 @@ func (c *consulRegistry) Register(s *Service) error {
|
||||
|
||||
node := s.Nodes[0]
|
||||
|
||||
tags := encodeMetaData(node.MetaData)
|
||||
tags := encodeMetadata(node.Metadata)
|
||||
|
||||
_, err := c.Client.Catalog().Register(&consul.CatalogRegistration{
|
||||
Node: node.Id,
|
||||
@ -123,7 +123,7 @@ func (c *consulRegistry) GetService(name string) (*Service, error) {
|
||||
Id: s.ServiceID,
|
||||
Address: s.Address,
|
||||
Port: s.ServicePort,
|
||||
MetaData: decodeMetaData(s.ServiceTags),
|
||||
Metadata: decodeMetadata(s.ServiceTags),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ func (cw *consulWatcher) serviceHandler(idx uint64, data interface{}) {
|
||||
Id: e.Service.ID,
|
||||
Address: e.Node.Address,
|
||||
Port: e.Service.Port,
|
||||
MetaData: decodeMetaData(e.Service.Tags),
|
||||
Metadata: decodeMetadata(e.Service.Tags),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ type Registry interface {
|
||||
|
||||
type Service struct {
|
||||
Name string
|
||||
MetaData map[string]string
|
||||
Metadata map[string]string
|
||||
Nodes []*Node
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ type Node struct {
|
||||
Id string
|
||||
Address string
|
||||
Port int
|
||||
MetaData map[string]string
|
||||
Metadata map[string]string
|
||||
}
|
||||
|
||||
type options struct{}
|
||||
|
55
server/options.go
Normal file
55
server/options.go
Normal file
@ -0,0 +1,55 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/myodc/go-micro/transport"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
transport transport.Transport
|
||||
metadata map[string]string
|
||||
name string
|
||||
address string
|
||||
id string
|
||||
}
|
||||
|
||||
func newOptions(opt ...Option) options {
|
||||
var opts options
|
||||
|
||||
for _, o := range opt {
|
||||
o(&opts)
|
||||
}
|
||||
|
||||
if opts.transport == nil {
|
||||
opts.transport = transport.DefaultTransport
|
||||
}
|
||||
|
||||
if len(opts.address) == 0 {
|
||||
opts.address = DefaultAddress
|
||||
}
|
||||
|
||||
if len(opts.name) == 0 {
|
||||
opts.name = DefaultName
|
||||
}
|
||||
|
||||
if len(opts.id) == 0 {
|
||||
opts.id = DefaultId
|
||||
}
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
func (o options) Name() string {
|
||||
return o.name
|
||||
}
|
||||
|
||||
func (o options) Id() string {
|
||||
return o.name + "-" + o.id
|
||||
}
|
||||
|
||||
func (o options) Address() string {
|
||||
return o.address
|
||||
}
|
||||
|
||||
func (o options) Metadata() map[string]string {
|
||||
return o.metadata
|
||||
}
|
@ -2,7 +2,6 @@ package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
|
||||
c "github.com/myodc/go-micro/context"
|
||||
"github.com/myodc/go-micro/transport"
|
||||
@ -16,29 +15,16 @@ import (
|
||||
)
|
||||
|
||||
type rpcServer struct {
|
||||
mtx sync.RWMutex
|
||||
address string
|
||||
opts options
|
||||
rpc *rpc.Server
|
||||
exit chan chan error
|
||||
opts options
|
||||
rpc *rpc.Server
|
||||
exit chan chan error
|
||||
}
|
||||
|
||||
func newRpcServer(address string, opt ...Option) Server {
|
||||
var opts options
|
||||
|
||||
for _, o := range opt {
|
||||
o(&opts)
|
||||
}
|
||||
|
||||
if opts.transport == nil {
|
||||
opts.transport = transport.DefaultTransport
|
||||
}
|
||||
|
||||
func newRpcServer(opts ...Option) Server {
|
||||
return &rpcServer{
|
||||
opts: opts,
|
||||
address: address,
|
||||
rpc: rpc.NewServer(),
|
||||
exit: make(chan chan error),
|
||||
opts: newOptions(opts...),
|
||||
rpc: rpc.NewServer(),
|
||||
exit: make(chan chan error),
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,7 +58,7 @@ func (s *rpcServer) accept(sock transport.Socket) {
|
||||
ct := msg.Header["Content-Type"]
|
||||
delete(msg.Header, "Content-Type")
|
||||
|
||||
ctx := c.WithMetaData(context.Background(), msg.Header)
|
||||
ctx := c.WithMetadata(context.Background(), msg.Header)
|
||||
|
||||
if err := s.rpc.ServeRequestWithContext(ctx, cc); err != nil {
|
||||
return
|
||||
@ -86,15 +72,17 @@ func (s *rpcServer) accept(sock transport.Socket) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *rpcServer) Address() string {
|
||||
s.mtx.RLock()
|
||||
address := s.address
|
||||
s.mtx.RUnlock()
|
||||
return address
|
||||
func (s *rpcServer) Config() options {
|
||||
return s.opts
|
||||
}
|
||||
|
||||
func (s *rpcServer) Init() error {
|
||||
return nil
|
||||
func (s *rpcServer) Init(opts ...Option) {
|
||||
for _, opt := range opts {
|
||||
opt(&s.opts)
|
||||
}
|
||||
if len(s.opts.id) == 0 {
|
||||
s.opts.id = s.opts.name + "-" + DefaultId
|
||||
}
|
||||
}
|
||||
|
||||
func (s *rpcServer) NewReceiver(handler interface{}) Receiver {
|
||||
@ -118,16 +106,14 @@ func (s *rpcServer) Register(r Receiver) error {
|
||||
func (s *rpcServer) Start() error {
|
||||
registerHealthChecker(s)
|
||||
|
||||
ts, err := s.opts.transport.Listen(s.address)
|
||||
ts, err := s.opts.transport.Listen(s.opts.address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("Listening on %s", ts.Addr())
|
||||
|
||||
s.mtx.RLock()
|
||||
s.address = ts.Addr()
|
||||
s.mtx.RUnlock()
|
||||
s.opts.address = ts.Addr()
|
||||
|
||||
go ts.Accept(s.accept)
|
||||
|
||||
|
@ -14,8 +14,8 @@ import (
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
Address() string
|
||||
Init() error
|
||||
Config() options
|
||||
Init(...Option)
|
||||
NewReceiver(interface{}) Receiver
|
||||
NewNamedReceiver(string, interface{}) Receiver
|
||||
Register(Receiver) error
|
||||
@ -23,45 +23,58 @@ type Server interface {
|
||||
Stop() error
|
||||
}
|
||||
|
||||
type options struct {
|
||||
transport transport.Transport
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
|
||||
var (
|
||||
Address string
|
||||
Name string
|
||||
Id string
|
||||
DefaultServer Server
|
||||
DefaultAddress = ":0"
|
||||
DefaultName = "go-server"
|
||||
DefaultId = uuid.NewUUID().String()
|
||||
DefaultServer Server = newRpcServer()
|
||||
)
|
||||
|
||||
func Name(n string) Option {
|
||||
return func(o *options) {
|
||||
o.name = n
|
||||
}
|
||||
}
|
||||
|
||||
func Id(id string) Option {
|
||||
return func(o *options) {
|
||||
o.id = id
|
||||
}
|
||||
}
|
||||
|
||||
func Address(a string) Option {
|
||||
return func(o *options) {
|
||||
o.address = a
|
||||
}
|
||||
}
|
||||
|
||||
func Transport(t transport.Transport) Option {
|
||||
return func(o *options) {
|
||||
o.transport = t
|
||||
}
|
||||
}
|
||||
|
||||
func Init() error {
|
||||
defer log.Flush()
|
||||
|
||||
if len(Name) == 0 {
|
||||
Name = "go-server"
|
||||
func Metadata(md map[string]string) Option {
|
||||
return func(o *options) {
|
||||
o.metadata = md
|
||||
}
|
||||
|
||||
if len(Id) == 0 {
|
||||
Id = Name + "-" + uuid.NewUUID().String()
|
||||
}
|
||||
|
||||
if DefaultServer == nil {
|
||||
DefaultServer = newRpcServer(Address)
|
||||
}
|
||||
|
||||
return DefaultServer.Init()
|
||||
}
|
||||
|
||||
func NewServer(address string, opt ...Option) Server {
|
||||
return newRpcServer(address, opt...)
|
||||
func Config() options {
|
||||
return DefaultServer.Config()
|
||||
}
|
||||
|
||||
func Init(opt ...Option) {
|
||||
if DefaultServer == nil {
|
||||
DefaultServer = newRpcServer(opt...)
|
||||
}
|
||||
DefaultServer.Init(opt...)
|
||||
}
|
||||
|
||||
func NewServer(opt ...Option) Server {
|
||||
return newRpcServer(opt...)
|
||||
}
|
||||
|
||||
func NewReceiver(handler interface{}) Receiver {
|
||||
@ -82,9 +95,10 @@ func Run() error {
|
||||
}
|
||||
|
||||
// parse address for host, port
|
||||
config := DefaultServer.Config()
|
||||
var host string
|
||||
var port int
|
||||
parts := strings.Split(DefaultServer.Address(), ":")
|
||||
parts := strings.Split(config.Address(), ":")
|
||||
if len(parts) > 1 {
|
||||
host = strings.Join(parts[:len(parts)-1], ":")
|
||||
port, _ = strconv.Atoi(parts[len(parts)-1])
|
||||
@ -94,13 +108,14 @@ func Run() error {
|
||||
|
||||
// register service
|
||||
node := ®istry.Node{
|
||||
Id: Id,
|
||||
Address: host,
|
||||
Port: port,
|
||||
Id: config.Id(),
|
||||
Address: host,
|
||||
Port: port,
|
||||
Metadata: config.Metadata(),
|
||||
}
|
||||
|
||||
service := ®istry.Service{
|
||||
Name: Name,
|
||||
Name: config.Name(),
|
||||
Nodes: []*registry.Node{node},
|
||||
}
|
||||
|
||||
@ -122,7 +137,8 @@ func Run() error {
|
||||
}
|
||||
|
||||
func Start() error {
|
||||
log.Infof("Starting server %s id %s", Name, Id)
|
||||
config := DefaultServer.Config()
|
||||
log.Infof("Starting server %s id %s", config.Name(), config.Id())
|
||||
return DefaultServer.Start()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user