|
|
|
@ -21,31 +21,48 @@ import ( |
|
|
|
|
"github.com/go-kratos/kratos/v2/transport/http" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type router struct { |
|
|
|
|
service string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type RouterOption func(o *router) |
|
|
|
|
|
|
|
|
|
// WithRouterService set the caller service name used by the route
|
|
|
|
|
func WithRouterService(service string) RouterOption { |
|
|
|
|
return func(o *router) { |
|
|
|
|
o.service = service |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NodeFilter polaris dynamic router selector
|
|
|
|
|
func (p *Polaris) NodeFilter() selector.NodeFilter { |
|
|
|
|
func (p *Polaris) NodeFilter(opts ...RouterOption) selector.NodeFilter { |
|
|
|
|
o := router{service: p.service} |
|
|
|
|
for _, opt := range opts { |
|
|
|
|
opt(&o) |
|
|
|
|
} |
|
|
|
|
return func(ctx context.Context, nodes []selector.Node) []selector.Node { |
|
|
|
|
if len(nodes) == 0 { |
|
|
|
|
return nodes |
|
|
|
|
} |
|
|
|
|
if appInfo, ok := kratos.FromContext(ctx); ok { |
|
|
|
|
req := &polaris.ProcessRoutersRequest{ |
|
|
|
|
ProcessRoutersRequest: model.ProcessRoutersRequest{ |
|
|
|
|
SourceService: model.ServiceInfo{ |
|
|
|
|
Service: appInfo.Name(), |
|
|
|
|
Namespace: p.namespace, |
|
|
|
|
}, |
|
|
|
|
SourceService: model.ServiceInfo{Namespace: p.namespace, Service: o.service}, |
|
|
|
|
DstInstances: buildPolarisInstance(p.namespace, nodes), |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
req.AddArguments(model.BuildCallerServiceArgument(p.namespace, appInfo.Name())) |
|
|
|
|
if appInfo, ok := kratos.FromContext(ctx); ok { |
|
|
|
|
req.SourceService.Service = appInfo.Name() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
req.AddArguments(model.BuildCallerServiceArgument(p.namespace, req.ProcessRoutersRequest.SourceService.Service)) |
|
|
|
|
|
|
|
|
|
// process transport
|
|
|
|
|
if tr, ok := transport.FromServerContext(ctx); ok { |
|
|
|
|
if tr, ok := transport.FromClientContext(ctx); ok { |
|
|
|
|
req.AddArguments(model.BuildMethodArgument(tr.Operation())) |
|
|
|
|
req.AddArguments(model.BuildPathArgument(tr.Operation())) |
|
|
|
|
|
|
|
|
|
for _, key := range tr.RequestHeader().Keys() { |
|
|
|
|
req.AddArguments(model.BuildHeaderArgument(key, tr.RequestHeader().Get(key))) |
|
|
|
|
req.AddArguments(model.BuildHeaderArgument(strings.ToLower(key), tr.RequestHeader().Get(key))) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// http
|
|
|
|
@ -83,10 +100,11 @@ func (p *Polaris) NodeFilter() selector.NodeFilter { |
|
|
|
|
newNode = append(newNode, v) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return newNode |
|
|
|
|
} |
|
|
|
|
if len(newNode) == 0 { |
|
|
|
|
return nodes |
|
|
|
|
} |
|
|
|
|
return newNode |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func buildPolarisInstance(namespace string, nodes []selector.Node) *pb.ServiceInstancesInProto { |
|
|
|
|