Skip to content

Instantly share code, notes, and snippets.

@asim
Last active October 24, 2021 03:59
Show Gist options
  • Save asim/4490dfea8f581198e925dfa5b5e58020 to your computer and use it in GitHub Desktop.
Save asim/4490dfea8f581198e925dfa5b5e58020 to your computer and use it in GitHub Desktop.
micro.Service for istio
package istio
import (
"time"
"github.com/micro/go-micro"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/selector"
gcli "github.com/micro/go-plugins/client/grpc"
"github.com/micro/go-plugins/registry/noop"
"github.com/micro/go-plugins/selector/named"
gsrv "github.com/micro/go-plugins/server/grpc"
)
// NewService returns a grpc service compatible with go-micro.Service
func NewService(opts ...micro.Option) micro.Service {
// our grpc client
c := gcli.NewClient()
// our grpc server
s := gsrv.NewServer()
// noop registry
r := noop.NewRegistry()
// named selector
se := named.NewSelector(selector.Registry(r))
// set selector
c.Init(client.Selector(se))
// create options with priority for our opts
options := []micro.Option{
micro.Client(c),
micro.Server(s),
micro.Registry(r),
}
// append passed in opts
options = append(options, opts...)
// generate and return a service
return micro.NewService(options...)
}
// NewFunction returns a grpc service compatible with go-micro.Function
func NewFunction(opts ...micro.Option) micro.Function {
// our grpc client
c := gcli.NewClient()
// our grpc server
s := gsrv.NewServer()
// noop registry
r := noop.NewRegistry()
// named selector
se := named.NewSelector(selector.Registry(r))
// set selector
c.Init(client.Selector(se))
// create options with priority for our opts
options := []micro.Option{
micro.Client(c),
micro.Server(s),
micro.Registry(r),
micro.RegisterTTL(time.Minute),
micro.RegisterInterval(time.Second * 30),
}
// append passed in opts
options = append(options, opts...)
// generate and return a function
return micro.NewFunction(options...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment