Skip to content
Permalink
Browse files

服务筛选

  • Loading branch information...
hb-chen committed Oct 13, 2019
1 parent ff70f21 commit 615d4604e251bf1188fe12a0be22e05ca9c4ebf9
Showing with 45 additions and 7 deletions.
  1. +24 −2 example/api/handler/example.go
  2. +14 −2 example/api/main.go
  3. +1 −1 example/srv/handler/example.go
  4. +6 −2 example/srv/main.go
@@ -7,6 +7,8 @@ import (

"github.com/hb-go/micro-quick-start/example/api/client"
api "github.com/micro/go-micro/api/proto"
mc "github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/errors"

example "github.com/hb-go/micro-quick-start/example/srv/proto/example"
@@ -36,13 +38,33 @@ func (e *Example) Call(ctx context.Context, req *api.Request, rsp *api.Response)

// make request
response, err := exampleClient.Call(ctx, &example.Request{
Name: extractValue(req.Post["name"]),
Name: extractValue(req.Post["name"]) + " 1",
})
if err != nil {
return errors.InternalServerError("go.micro.api.example.example.call", err.Error())
}

b, _ := json.Marshal(response)
response1, err := exampleClient.Call(
ctx,
&example.Request{
Name: extractValue(req.Post["name"]) + " 2",
},
mc.WithSelectOption(
selector.WithFilter(
selector.FilterLabel("key", "value1"),
),
),
)
if err != nil {
return errors.InternalServerError("go.micro.api.example.example.call", err.Error())
}

responses := []*example.Response{
response,
response1,
}

b, _ := json.Marshal(responses)

rsp.StatusCode = 200
rsp.Body = string(b)
@@ -6,10 +6,11 @@ import (
"github.com/hb-go/micro-quick-start/example/api/client"
"github.com/hb-go/micro-quick-start/example/api/handler"
"github.com/micro/go-micro"

"github.com/micro/go-micro/api"
ha "github.com/micro/go-micro/api/handler/api"

mc "github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/selector"

example "github.com/hb-go/micro-quick-start/example/api/proto/example"
)

@@ -30,6 +31,17 @@ func main() {
micro.WrapHandler(client.ExampleWrapper(service)),
)

// 筛选器
service.Init(
func(options *micro.Options) {
options.Client.Init(func(options *mc.Options) {
options.CallOptions.SelectOptions =
append(options.CallOptions.SelectOptions,
selector.WithFilter(selector.FilterVersion("v1")))
})
},
)

// Register Handler
example.RegisterExampleHandler(
service.Server(),
@@ -13,7 +13,7 @@ type Example struct{}
// Call is a single request handler called via client.Call or the generated client code
func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
log.Log("Received Example.Call request")
rsp.Msg = "Hello " + req.Name
rsp.Msg = "Hello " + req.Name + " v1 value1"
return nil
}

@@ -13,14 +13,18 @@ func main() {
// New Service
service := micro.NewService(
micro.Name("go.micro.srv.example"),
micro.Version("latest"),
micro.Version("v1"),
)

// Initialise service
metadata := make(map[string]string)
metadata["key"] = "value1"

service.Init()
service.Init(
micro.Name("go.micro.srv.example"),
micro.Version("latest"),
micro.Version("v1"),
micro.Metadata(metadata),
)

// Register Handler

0 comments on commit 615d460

Please sign in to comment.
You can’t perform that action at this time.