We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
srv.go ` package main
import ( "context" "fmt"
mcusend "AlarmService/proto/mcusend" "time" "github.com/micro/go-micro/v2" "github.com/micro/go-plugins/registry/etcdv3"
)
type MCUsend struct{}
func (e *MCUsend) Mcusend(ctx context.Context, req *mcusend.McuRequest, rsp *mcusend.McuResponse) error { mac := req.Macaddr sendType := req.Mculine
rsp.Desc = mac fmt.Println(mac) fmt.Println(sendType) return nil
} func (e *MCUsend) Devicesend(ctx context.Context, req *mcusend.DeviceRequest, rsp *mcusend.DeviceResponse) error {
fmt.Println(ok) fmt.Println(md) id := req.Id rsp.Desc = "0" fmt.Println(id) return nil
}
func main() { // 创建服务,除了服务名,其它选项可加可不加,比如Version版本号、Metadata元数据等 registre := etcdv3.NewRegistry() service := micro.NewService( micro.Registry(registre), micro.Name("greeter.service"), micro.Version("latest"), micro.RegisterTTL(10time.Second), micro.RegisterInterval(5time.Second), micro.Metadata(map[string]string{ "type": "MCUsend", }), ) service.Init()
// 注册服务 mcusend.RegisterMCUsendHandler(service.Server(), new(MCUsend)) // proto.RegisterGreeterHandler(service.Server(), new(Greeter)) // 启动服务 if err := service.Run(); err != nil { fmt.Println(err) }
} ` client.go
` package main
import ( selectors "AlarmService/pkg/selector" mcusend "AlarmService/proto/mcusend" "context" "fmt" "time"
"github.com/micro/go-micro/v2" // "github.com/micro/go-micro/v2/metadata" microCli "github.com/micro/go-micro/v2/client" "github.com/micro/go-plugins/registry/etcdv3"
func main() { // 定义服务,可以传入其它可选参数 registre := etcdv3.NewRegistry() service := micro.NewService( micro.Registry(registre), micro.Name("greeter.client"), micro.RegisterTTL(time.Second30), micro.RegisterInterval(time.Second15), micro.Metadata(map[string]string{ "type": "MCUsend", }), ) // service := micro.NewService(micro.Name("greeter.client")) service.Init()
microCli.DefaultClient = microCli.NewClient( microCli.Selector(selectors.DCSelector("MCUsend")), ) req := microCli.NewRequest("greeter.service", "MCUsend.Devicesend", &mcusend.DeviceRequest{Id: "12313"}) rsp := &mcusend.DeviceResponse{} if err := microCli.Call(context.Background(), req, rsp); err != nil { fmt.Println(err) } fmt.Println(rsp)
} 想利用Metadata进行过滤,出现异常 {"id":"go.micro.client","code":500,"detail":"service greeter.service: not found","status":"Internal Server Error"} 排查问题发现在DCSelector中n.opts.Registry.GetService(“greeter.service”)的值为[] **selector.go** package connect
想利用Metadata进行过滤,出现异常 {"id":"go.micro.client","code":500,"detail":"service greeter.service: not found","status":"Internal Server Error"} 排查问题发现在DCSelector中n.opts.Registry.GetService(“greeter.service”)的值为[] **selector.go**
import ( "fmt" "math/rand" "sync" "time"
"github.com/micro/go-micro/v2/client/selector" "github.com/micro/go-micro/v2/registry"
type dcSelector struct { select_type string opts selector.Options }
func init() { rand.Seed(time.Now().Unix()) }
func (n *dcSelector) Init(opts ...selector.Option) error { for _, o := range opts { o(&n.opts) } return nil }
func (n *dcSelector) Options() selector.Options { return n.opts }
func (n *dcSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) { fmt.Println("Registry :", n.opts.Registry) // services, err := n.opts.Registry.ListServices() // fmt.Printf(serv)
services, err := n.opts.Registry.GetService(service) fmt.Println("services :", services) //这里的值为[] fmt.Println("err :", err) if err != nil { return nil, err } if len(services) == 0 { return nil, selector.ErrNotFound } var nodes []*registry.Node fmt.Printf("selector_type selector:%v\n", n.select_type) // Filter the nodes for type for _, service := range services { for _, node := range service.Nodes { if node.Metadata["type"] == n.select_type { nodes = append(nodes, node) fmt.Printf("Select==%v node:%v\n", n.select_type, node) } else { fmt.Printf("Select!=%v node:%v\n", n.select_type, node) } } fmt.Println("Select version:", service.Version) fmt.Println("Select Metadata:", service.Metadata) } if len(nodes) == 0 { return nil, selector.ErrNotFound } var i int var mtx sync.Mutex return func() (*registry.Node, error) { mtx.Lock() defer mtx.Unlock() i++ return nodes[i%len(nodes)], nil }, nil
func (n *dcSelector) Mark(service string, node *registry.Node, err error) { return }
func (n *dcSelector) Reset(service string) { return }
func (n *dcSelector) Close() error { return nil }
func (n *dcSelector) String() string { return "dc" }
// Return a new first node selector func DCSelector(select_type string, opts ...selector.Option) selector.Selector { var sopts selector.Options fmt.Println("select_type:", select_type) for _, opt := range opts { fmt.Println("1") opt(&sopts) } if sopts.Registry == nil { fmt.Println("2") sopts.Registry = registry.DefaultRegistry } return &dcSelector{select_type, sopts} } ` 请教各位大佬这个是什么原因?
The text was updated successfully, but these errors were encountered:
建议类似的问题把源码库贴出来,中间件的异常是很难看出来的,需要调试。
Sorry, something went wrong.
No branches or pull requests
srv.go
`
package main
import (
"context"
"fmt"
)
type MCUsend struct{}
func (e *MCUsend) Mcusend(ctx context.Context, req *mcusend.McuRequest, rsp *mcusend.McuResponse) error {
mac := req.Macaddr
sendType := req.Mculine
}
func (e *MCUsend) Devicesend(ctx context.Context, req *mcusend.DeviceRequest, rsp *mcusend.DeviceResponse) error {
}
func main() {
// 创建服务,除了服务名,其它选项可加可不加,比如Version版本号、Metadata元数据等
registre := etcdv3.NewRegistry()
service := micro.NewService(
micro.Registry(registre),
micro.Name("greeter.service"),
micro.Version("latest"),
micro.RegisterTTL(10time.Second),
micro.RegisterInterval(5time.Second),
micro.Metadata(map[string]string{
"type": "MCUsend",
}),
)
service.Init()
}
`
client.go
`
package main
import (
selectors "AlarmService/pkg/selector"
mcusend "AlarmService/proto/mcusend"
"context"
"fmt"
"time"
)
func main() {
// 定义服务,可以传入其它可选参数
registre := etcdv3.NewRegistry()
service := micro.NewService(
micro.Registry(registre),
micro.Name("greeter.client"),
micro.RegisterTTL(time.Second30),
micro.RegisterInterval(time.Second15),
micro.Metadata(map[string]string{
"type": "MCUsend",
}),
)
// service := micro.NewService(micro.Name("greeter.client"))
service.Init()
}
想利用Metadata进行过滤,出现异常 {"id":"go.micro.client","code":500,"detail":"service greeter.service: not found","status":"Internal Server Error"} 排查问题发现在DCSelector中n.opts.Registry.GetService(“greeter.service”)的值为[] **selector.go**
package connect
import (
"fmt"
"math/rand"
"sync"
"time"
)
type dcSelector struct {
select_type string
opts selector.Options
}
func init() {
rand.Seed(time.Now().Unix())
}
func (n *dcSelector) Init(opts ...selector.Option) error {
for _, o := range opts {
o(&n.opts)
}
return nil
}
func (n *dcSelector) Options() selector.Options {
return n.opts
}
func (n *dcSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
fmt.Println("Registry :", n.opts.Registry)
// services, err := n.opts.Registry.ListServices()
// fmt.Printf(serv)
}
func (n *dcSelector) Mark(service string, node *registry.Node, err error) {
return
}
func (n *dcSelector) Reset(service string) {
return
}
func (n *dcSelector) Close() error {
return nil
}
func (n *dcSelector) String() string {
return "dc"
}
// Return a new first node selector
func DCSelector(select_type string, opts ...selector.Option) selector.Selector {
var sopts selector.Options
fmt.Println("select_type:", select_type)
for _, opt := range opts {
fmt.Println("1")
opt(&sopts)
}
if sopts.Registry == nil {
fmt.Println("2")
sopts.Registry = registry.DefaultRegistry
}
return &dcSelector{select_type, sopts}
}
`
请教各位大佬这个是什么原因?
The text was updated successfully, but these errors were encountered: