Similarityoung commented on code in PR #862:
URL: https://github.com/apache/dubbo-go-pixiu/pull/862#discussion_r3143219212
##########
pkg/client/dubbo/dubbo.go:
##########
@@ -172,106 +196,157 @@ func (dc *Client) Close() error {
return nil
}
-// Call invoke service
-func (dc *Client) Call(req *client.Request) (res any, err error) {
- // if GET with no args, values would be nil
- values, err := dc.genericArgs(req)
+// Call invoke service.
+func (dc *Client) Call(ctx context.Context, req *DubboOutboundRequest) (any,
error) {
+ if req == nil {
+ return nil, errors.New("dubbo outbound request is nil")
+ }
+
+ spec := dc.resolveFromOutbound(req)
+ types, vals, finalValues, err := dc.preparePayload(req)
if err != nil {
return nil, err
}
- target, ok := values.(*dubboTarget)
- if !ok {
- return nil, errors.New("map parameters failed")
- }
- dm := req.API.IntegrationRequest
- method := dm.Method
- types := []string{}
- vals := []hessian.Object{}
- finalValues := []byte{}
+ gs, err := dc.Get(spec)
+ if err != nil {
+ return nil, err
+ }
+ if gs == nil {
+ return nil, errors.New("dubbo generic service is nil")
+ }
- if target != nil {
- logger.Debugf("[dubbo-go-pixiu] dubbo invoke, method:%s,
types:%s, reqData:%v", method, target.Types, target.Values)
- types = target.Types
- vals = make([]hessian.Object, len(target.Values))
- for i, v := range target.Values {
- vals[i] = v
- }
- if len(types) == 0 {
- types = javaClassNameElem(vals)
- }
- var err error
- finalValues, err = json.Marshal(vals)
- if err != nil {
- logger.Warnf("[dubbo-go-pixiu] reqData convert to
string failed: %v", err)
- }
- } else {
- logger.Debugf("[dubbo-go-pixiu] dubbo invoke, method:%s,
types:%s, reqData:%v", method, nil, nil)
+ invokeCtx, cancel := prepareInvokeContext(ctx, req.Timeout)
+ if cancel != nil {
+ defer cancel()
}
- gs := dc.Get(dm)
- tr := otel.Tracer(traceNameDubbogoClient)
- ctx, span := tr.Start(req.Context, spanNameDubbogoClient)
- trace.SpanFromContext(req.Context).SpanContext()
- span.SetAttributes(attribute.Key(spanTagMethod).String(method))
- span.SetAttributes(attribute.Key(spanTagType).StringSlice(types))
-
span.SetAttributes(attribute.Key(spanTagValues).String(string(finalValues)))
+ spanCtx, span := otel.Tracer(traceNameDubbogoClient).Start(invokeCtx,
spanNameDubbogoClient)
defer span.End()
-
- // tracing inject manually;
- carrier := propagation.MapCarrier{}
- otel.GetTextMapPropagator().Inject(ctx, carrier)
- ctxWithAttachment := context.WithValue(ctx, constant.AttachmentKey,
map[string]string(carrier))
-
- rst, err := gs.Invoke(ctxWithAttachment, method, types, vals)
+ span.SetAttributes(
+ attribute.String(spanTagMethod, req.Method),
+ attribute.StringSlice(spanTagType, types),
+ attribute.String(spanTagValues, string(finalValues)),
+ )
+
+ spanCtx = context.WithValue(spanCtx, constant.AttachmentKey,
mergeOutboundAttachments(spanCtx, req.Attachments))
+ ctxWithAttachment := withAttachments(spanCtx)
+ rst, err := gs.Invoke(ctxWithAttachment, req.Method, types, vals)
if err != nil {
- // TODO statusCode I don’t know what dubbo will return when it
times out, so I will return it directly. I will judge it when I call it.
span.RecordError(err)
return nil, err
}
- logger.Debugf("[dubbo-go-pixiu] dubbo client resp:%v", rst)
-
+ logger.Debugf("[dubbo-go-pixiu] dubbo invoke result:%v", rst)
return rst, nil
}
-func (dc *Client) genericArgs(req *client.Request) (any, error) {
- values, err := dc.MapParams(req)
- if err != nil {
- return nil, err
+func (dc *Client) resolveFromOutbound(req *DubboOutboundRequest)
resolvedReferSpec {
+ spec := resolvedReferSpec{
+ Interface: req.Service,
+ Group: req.Group,
+ Version: req.Version,
+ EffectiveProtocol: req.Protocol,
+ EffectiveSerialization: req.Serialization,
+ ConsumerDefaults: dc.resolveGlobalConsumerDefaults(),
+ }
+
+ if strings.TrimSpace(req.Address) != "" {
+ spec.Mode = "direct"
+ spec.URL = req.Protocol + "://" + req.Address
+ return spec
}
- return values, nil
+ registryIDs := make([]string, 0, len(dc.registries))
+ useNacosWarmup := false
+ for id, registry := range dc.registries {
+ registryIDs = append(registryIDs, id)
+ if registry != nil && registry.Protocol == "nacos" {
+ useNacosWarmup = true
+ }
+ }
+ sort.Strings(registryIDs)
+
+ spec.Mode = "registry"
+ spec.RegistryIDs = registryIDs
+ spec.UseNacosWarmup = useNacosWarmup
+ return spec
}
-// MapParams params mapping to api.
-func (dc *Client) MapParams(req *client.Request) (any, error) {
- r := req.API.IntegrationRequest
- values := newDubboTarget(r.MappingParams)
- if dc.dubboProxyConfig != nil && dc.dubboProxyConfig.IsDefaultMap {
- values = newDubboTarget(defaultMappingParams)
- }
- for _, mappingParam := range r.MappingParams {
- source, _, err := client.ParseMapSource(mappingParam.Name)
- if err != nil {
- return nil, err
+func (dc *Client) resolveGlobalConsumerDefaults() resolvedConsumerDefaults {
+ defaults := resolvedConsumerDefaults{
+ Cluster: "failover",
+ Retries: "3",
+ RequestTimeout: cst.DefaultReqTimeout,
+ }
+
+ if dc.dubboProxyConfig == nil {
+ return defaults
+ }
+
+ defaults.LoadBalance = dc.dubboProxyConfig.LoadBalance
+ if strings.TrimSpace(dc.dubboProxyConfig.Retries) != "" {
Review Comment:
雨哥觉得需要对每个 api 配置 retry 吗,我目前想只需要全局的 retry 而不对每个单独的 api 设置,因为构造dubbo client
时,如果retry 不同会导致制造不同的 client,而无法复用之前创建好的 client。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]