Commit 80839e9e authored by kzkzzzz's avatar kzkzzzz

feat: 调整client

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