client.go 1.08 KB
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9
package client

import (
	"github.com/asim/go-micro/plugins/client/grpc/v4"
	"github.com/asim/go-micro/plugins/registry/consul/v4"
	"go-micro.dev/v4"
	"go-micro.dev/v4/client"
	"go-micro.dev/v4/registry"
	"go-micro.dev/v4/selector"
10
	"gomicro-base/api/member"
kzkzzzz's avatar
kzkzzzz committed
11 12 13 14
	"gomicro-base/service/wallet/internal/conf"
)

var (
15 16
	microService micro.Service
	cf           *conf.Config
kzkzzzz's avatar
kzkzzzz committed
17 18 19 20 21 22 23 24 25 26 27
)

func NewMicroClient(c *conf.Config) {
	cf = c
	microRegistry := consul.NewRegistry(registry.Addrs(c.Server.ConsulServer...))
	// 轮询访问
	newSelector := selector.NewSelector(
		selector.Registry(microRegistry),
		selector.SetStrategy(selector.RoundRobin),
	)

28
	microService = micro.NewService(
kzkzzzz's avatar
kzkzzzz committed
29 30 31 32 33
		micro.Client(grpc.NewClient()),
		micro.Selector(newSelector),
		micro.WrapClient(NewTimeoutWrapper),
		//micro.WrapClient(NewLogWrapper),
	)
34 35 36 37
}

func GetService() micro.Service {
	return microService
kzkzzzz's avatar
kzkzzzz committed
38 39 40
}

func GetClient() client.Client {
41
	return microService.Client()
kzkzzzz's avatar
kzkzzzz committed
42 43 44 45 46
}

func Reconnect() {
	NewMicroClient(cf)
}
47 48 49 50

func GetMemberSvc() member.MemberService {
	return member.NewMemberService(conf.Conf.Client.Member, microService.Client())
}