Last active
October 24, 2021 03:59
-
-
Save asim/4490dfea8f581198e925dfa5b5e58020 to your computer and use it in GitHub Desktop.
micro.Service for istio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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