Commit 80839e9e authored by kzkzzzz's avatar kzkzzzz

feat: 调整client

parent 9d5884e0
Pipeline #1362 failed with stages
Server:
Name: "service.demo"
ConsulServer:
ConsulAddr:
- 192.168.233.1:8500
Grpc:
Addr: "" # 为空随机端口, 使用注册中心服务发现
......
......@@ -24,8 +24,8 @@ type Config struct {
type Server struct {
Name string
ConsulServer []string
EtcdServer []string
ConsulAddr []string
EtcdAddr []string
Grpc struct {
Addr string
}
......
......@@ -14,8 +14,8 @@ import (
)
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...))
reg := consul.NewRegistry(registry.Addrs(conf.Server.ConsulAddr...))
//reg := etcd.NewRegistry(registry.Addrs(conf.Server.EtcdAddr...))
opt := []micro.Option{
micro.Server(grpc.NewServer()),
micro.Name(conf.Server.Name),
......
Server:
Name: "service.member"
ConsulServer:
ConsulAddr:
- 192.168.233.1:8500
Grpc:
Addr: "" # 为空随机端口, 使用注册中心服务发现
......
......@@ -26,8 +26,8 @@ type Config struct {
type Server struct {
Name string
ConsulServer []string
EtcdServer []string
ConsulAddr []string
EtcdAddr []string
Grpc struct {
Addr string
}
......
......@@ -14,8 +14,8 @@ import (
)
func NewMicroServer(conf *conf.Config, svc *service.MemberService) micro.Service {
reg := consul.NewRegistry(registry.Addrs(conf.Server.ConsulServer...))
//reg := etcd.NewRegistry(registry.Addrs(conf.Server.EtcdServer...))
reg := consul.NewRegistry(registry.Addrs(conf.Server.ConsulAddr...))
//reg := etcd.NewRegistry(registry.Addrs(conf.Server.EtcdAddr...))
opt := []micro.Option{
micro.Server(grpc.NewServer()),
micro.Name(conf.Server.Name),
......
......@@ -4,47 +4,29 @@ 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"
"gomicro-base/api/member"
"gomicro-base/service/wallet/internal/conf"
)
var (
microService micro.Service
cf *conf.Config
memberSvc member.MemberService
)
func NewMicroClient(c *conf.Config) {
cf = c
microRegistry := consul.NewRegistry(registry.Addrs(c.Server.ConsulServer...))
microRegistry := consul.NewRegistry(registry.Addrs(c.Server.ConsulAddr...))
// 轮询访问
newSelector := selector.NewSelector(
selector.Registry(microRegistry),
selector.SetStrategy(selector.RoundRobin),
)
microService = micro.NewService(
microService := micro.NewService(
micro.Client(grpc.NewClient()),
micro.Selector(newSelector),
micro.Registry(microRegistry),
micro.WrapClient(NewTimeoutWrapper),
//micro.WrapClient(NewLogWrapper),
)
}
func GetService() micro.Service {
return microService
}
func GetClient() client.Client {
return microService.Client()
}
func Reconnect() {
NewMicroClient(cf)
memberSvc = member.NewMemberService(conf.Conf.Client.Member, microService.Client())
}
func GetMemberSvc() member.MemberService {
return member.NewMemberService(conf.Conf.Client.Member, microService.Client())
return memberSvc
}
Server:
Name: "service.wallet"
ConsulServer:
ConsulAddr:
- 192.168.233.1:8500
Grpc:
Addr: "" # 为空随机端口, 使用注册中心服务发现
......
......@@ -27,8 +27,8 @@ type Config struct {
type Server struct {
Name string
ConsulServer []string
EtcdServer []string
ConsulAddr []string
EtcdAddr []string
Grpc struct {
Addr string
}
......
......@@ -14,8 +14,8 @@ import (
)
func NewMicroServer(conf *conf.Config, svc *service.WalletService) micro.Service {
reg := consul.NewRegistry(registry.Addrs(conf.Server.ConsulServer...))
//reg := etcd.NewRegistry(registry.Addrs(conf.Server.EtcdServer...))
reg := consul.NewRegistry(registry.Addrs(conf.Server.ConsulAddr...))
//reg := etcd.NewRegistry(registry.Addrs(conf.Server.EtcdAddr...))
opt := []micro.Option{
micro.Server(grpc.NewServer()),
micro.Name(conf.Server.Name),
......
......@@ -12,7 +12,6 @@ import (
"go-micro.dev/v4/client"
"go-micro.dev/v4/errors"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/selector"
"net/http"
"os"
"sync"
......@@ -74,15 +73,10 @@ func main() {
func start() {
microRegistry = consul.NewRegistry(registry.Addrs(consulAddr...))
// 轮询访问
newSelector := selector.NewSelector(
selector.Registry(microRegistry),
selector.SetStrategy(selector.RoundRobin),
)
service := micro.NewService(
micro.Client(grpc.NewClient()),
micro.Selector(newSelector),
micro.Registry(microRegistry),
micro.WrapClient(NewTimeoutWrapper),
)
microClient = service.Client()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment