moremind commented on code in PR #5953: URL: https://github.com/apache/shenyu/pull/5953#discussion_r1986494922
########## shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java: ########## @@ -274,6 +376,120 @@ private ReferenceConfig<GenericService> buildReference(final MetaData metaData, return reference; } + /** + * buildReference param with dubboUpstream. + * + * @param metaData metaData + * @param ruleData ruleData + * @param namespace namespace + * @param dubboUpstream dubboUpstream + * @return the reference config + */ + private ReferenceConfig<GenericService> buildReference(final MetaData metaData, final RuleData ruleData, final String namespace, final DubboUpstream dubboUpstream) { + if (Objects.isNull(dubboUpstream)) { + return this.buildReference(metaData, namespace); + } + + ReferenceConfig<GenericService> reference = new ReferenceConfig<>(); + reference.setGeneric("true"); + reference.setAsync(true); + reference.setApplication(applicationConfig); + + RegistryConfig registryConfigTemp = new RegistryConfig(); + registryConfigTemp.setProtocol(dubboUpstream.getProtocol()); + registryConfigTemp.setId("shenyu_proxy"); Review Comment: not use magic value ########## shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java: ########## @@ -274,6 +376,120 @@ private ReferenceConfig<GenericService> buildReference(final MetaData metaData, return reference; } + /** + * buildReference param with dubboUpstream. + * + * @param metaData metaData + * @param ruleData ruleData + * @param namespace namespace + * @param dubboUpstream dubboUpstream + * @return the reference config + */ + private ReferenceConfig<GenericService> buildReference(final MetaData metaData, final RuleData ruleData, final String namespace, final DubboUpstream dubboUpstream) { + if (Objects.isNull(dubboUpstream)) { + return this.buildReference(metaData, namespace); + } + + ReferenceConfig<GenericService> reference = new ReferenceConfig<>(); + reference.setGeneric("true"); + reference.setAsync(true); + reference.setApplication(applicationConfig); + + RegistryConfig registryConfigTemp = new RegistryConfig(); + registryConfigTemp.setProtocol(dubboUpstream.getProtocol()); + registryConfigTemp.setId("shenyu_proxy"); + registryConfigTemp.setRegister(false); + registryConfigTemp.setAddress(dubboUpstream.getRegistry()); + Optional.ofNullable(dubboUpstream.getGroup()).ifPresent(registryConfigTemp::setGroup); + Optional.ofNullable(dubboUpstream.getVersion()).ifPresent(registryConfigTemp::setVersion); + if (StringUtils.isNotBlank(namespace)) { + if (!registryConfigTemp.getAddress().contains(Constants.NAMESPACE)) { + registryConfigTemp.setAddress(registryConfigTemp.getAddress() + "?" + Constants.NAMESPACE + "=" + namespace); + } else { + String newAddress = registryConfigTemp.getAddress().substring(0, registryConfigTemp.getAddress().indexOf(Constants.NAMESPACE) + 1) + Constants.NAMESPACE + "=" + namespace; + registryConfigTemp.setAddress(newAddress); + } + } + reference.setRegistry(registryConfigTemp); + + DubboRuleHandle dubboRuleHandle = AbstractDubboPluginDataHandler.RULE_CACHED_HANDLE.get().obtainHandle(ruleData.getId()); + ConsumerConfig consumerConfigTmp = new ConsumerConfig(); + if (ObjectUtils.isNotEmpty(dubboRuleHandle)) { + consumerConfigTmp.refresh(); + Optional.ofNullable(consumerConfig.getThreadpool()).ifPresent(consumerConfigTmp::setThreadpool); + Optional.ofNullable(consumerConfig.getCorethreads()).ifPresent(consumerConfigTmp::setCorethreads); + Optional.ofNullable(consumerConfig.getThreads()).ifPresent(consumerConfigTmp::setThreads); + Optional.ofNullable(consumerConfig.getQueues()).ifPresent(consumerConfigTmp::setQueues); + consumerConfigTmp.setRetries(dubboRuleHandle.getRetries()); + consumerConfigTmp.setTimeout((int) dubboRuleHandle.getTimeout()); + } + + reference.setConsumer(consumerConfigTmp); + reference.setInterface(metaData.getServiceName()); + // default protocol is dubbo + reference.setProtocol("dubbo"); + reference.setCheck(false); + + Map<String, String> parameters = new HashMap<>(2); + parameters.put("dispatcher", "direct"); + reference.setParameters(parameters); + + this.configReferenceConfigWithMetaDataRpcExt(metaData.getRpcExt(), reference); + return reference; + } + + /** + * Config ReferenceConfig dubbo param with metaData rpc ext. + * + * @param rpcExt the rpc ext + * @param reference the reference + */ + private void configReferenceConfigWithMetaDataRpcExt(final String rpcExt, final ReferenceConfig<GenericService> reference) { + DubboParam dubboParam = parserToDubboParam(rpcExt); + if (Objects.isNull(dubboParam)) { + return; + } + + if (StringUtils.isNoneBlank(dubboParam.getVersion())) { + reference.setVersion(dubboParam.getVersion()); + } + if (StringUtils.isNoneBlank(dubboParam.getGroup())) { + reference.setGroup(dubboParam.getGroup()); + } + if (StringUtils.isNoneBlank(dubboParam.getUrl())) { + reference.setUrl(dubboParam.getUrl()); + } + if (StringUtils.isNoneBlank(dubboParam.getCluster())) { + reference.setCluster(dubboParam.getCluster()); + } + if (StringUtils.isNoneBlank(dubboParam.getLoadbalance())) { + reference.getParameters().put(Constants.DUBBO_LOAD_BALANCE, dubboParam.getLoadbalance()); + } + if ("protobuf".equals(dubboParam.getSerialization())) { Review Comment: not use magic value ########## shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java: ########## @@ -164,6 +178,64 @@ public ReferenceConfig<GenericService> initRefN(final MetaData metaData, final S return build(metaData, namespace); } + /** + * Init ref reference config use dubboUpstream. + * + * @param selectorId the selectorId + * @param ruleData the rule data + * @param metaData the meta data + * @param namespace namespace + * @param dubboUpstream dubboUpstream + * @return the reference config + */ + public ReferenceConfig<GenericService> initRefN(final String selectorId, final RuleData ruleData, final MetaData metaData, final String namespace, final DubboUpstream dubboUpstream) { + try { + String cacheKey = generateUpstreamCacheKey(selectorId, ruleData.getId(), metaData.getId(), namespace, dubboUpstream); + ReferenceConfig<GenericService> referenceConfig = cache.get(cacheKey); + if (StringUtils.isNoneBlank(referenceConfig.getInterface())) { + return referenceConfig; + } + } catch (ExecutionException e) { + LOG.error("initRefN dubbo ref exception", e); + } + return build(metaData, ruleData, namespace, selectorId, dubboUpstream); + } + + /** + * generate dubbo upstream reference cache key. + * + * @param selectorId selectorId + * @param ruleId ruleId + * @param metaDataId metaDataId + * @param namespace namespace + * @param dubboUpstream dubboUpstream + * @return the reference config cache key + */ + public String generateUpstreamCacheKey(final String selectorId, final String ruleId, final String metaDataId, final String namespace, final DubboUpstream dubboUpstream) { + StringJoiner stringJoiner = new StringJoiner("_"); Review Comment: search exist value -- 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: notifications-unsubscr...@shenyu.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org