grpc.go 1017 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
package server

import (
	"fmt"
	"github.com/asim/go-micro/plugins/registry/consul/v4"
	"github.com/asim/go-micro/plugins/server/grpc/v4"
	"go-micro.dev/v4"
	"go-micro.dev/v4/registry"
	"go-micro.dev/v4/server"
	"gomicro-base/api/demo"
	"gomicro-base/service/demo/internal/conf"
	"gomicro-base/service/demo/internal/service"
	"time"
)

func NewMicroServer(conf *conf.Config, svc *service.DemoService) micro.Service {
	reg := consul.NewRegistry(registry.Addrs(conf.Server.ConsulServer...))
	//reg := etcd.NewRegistry(registry.Addrs(conf.Server.EtcdServer...))
	opt := []micro.Option{
		micro.Server(grpc.NewServer()),
		micro.Name(conf.Server.Name),
		micro.Address(conf.Server.Grpc.Addr),
		micro.Registry(reg),
		micro.RegisterTTL(time.Second * 30),
		micro.RegisterInterval(time.Second * 10),
	}
	s := micro.NewService(opt...)
	s.Server().Init(server.Wait(nil))

	//reflection.Register()
	s.Init()

	err := demo.RegisterDemoHandler(s.Server(), &handler{svc: svc})
	if err != nil {
		fmt.Println(err)
	}
	return s
}