gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/service/grpc/grpc.go (about)

     1  package grpc
     2  
     3  import (
     4  	"time"
     5  
     6  	"gitee.com/liuxuezhan/go-micro-v1.18.0"
     7  	broker "gitee.com/liuxuezhan/go-micro-v1.18.0/broker"
     8  	client "gitee.com/liuxuezhan/go-micro-v1.18.0/client/grpc"
     9  	server "gitee.com/liuxuezhan/go-micro-v1.18.0/server/grpc"
    10  )
    11  
    12  // NewService returns a grpc service compatible with go-micro.Service
    13  func NewService(opts ...micro.Option) micro.Service {
    14  	// our grpc client
    15  	c := client.NewClient()
    16  	// our grpc server
    17  	s := server.NewServer()
    18  	// our grpc broker
    19  	b := broker.NewBroker()
    20  
    21  	// create options with priority for our opts
    22  	options := []micro.Option{
    23  		micro.Client(c),
    24  		micro.Server(s),
    25  		micro.Broker(b),
    26  	}
    27  
    28  	// append passed in opts
    29  	options = append(options, opts...)
    30  
    31  	// generate and return a service
    32  	return micro.NewService(options...)
    33  }
    34  
    35  // NewFunction returns a grpc service compatible with go-micro.Function
    36  func NewFunction(opts ...micro.Option) micro.Function {
    37  	// our grpc client
    38  	c := client.NewClient()
    39  	// our grpc server
    40  	s := server.NewServer()
    41  	// our grpc broker
    42  	b := broker.NewBroker()
    43  
    44  	// create options with priority for our opts
    45  	options := []micro.Option{
    46  		micro.Client(c),
    47  		micro.Server(s),
    48  		micro.Broker(b),
    49  		micro.RegisterTTL(time.Minute),
    50  		micro.RegisterInterval(time.Second * 30),
    51  	}
    52  
    53  	// append passed in opts
    54  	options = append(options, opts...)
    55  
    56  	// generate and return a function
    57  	return micro.NewFunction(options...)
    58  }