apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.52k stars 26.43k forks source link

SpringBoot+dubbo实现分组聚合 #584

Closed ld-github closed 4 years ago

ld-github commented 7 years ago
@Configuration
public class DubboReferenceConfig extends DubboConfig {

    @Bean
    @Qualifier("managerServiceReferenceBean")
    public ReferenceBean<ManagerService> managerServiceReferenceBean() {

        ReferenceBean<ManagerService> referenceBean = new ReferenceBean<ManagerService>();

        List<MethodConfig> methods = new ArrayList<MethodConfig>();

        methods.add(managerServiceGetAllMethodConfig());

        referenceBean.setApplication(applicationConfig());
        referenceBean.setRegistry(registryConfig());
        referenceBean.setConsumer(consumerConfig());
        referenceBean.setInterface(ManagerService.class);
        referenceBean.setMethods(methods);
        referenceBean.setGroup("*");

        return referenceBean;
    }

    @Bean
    public MethodConfig managerServiceGetAllMethodConfig() {

        MethodConfig methodConfig = new MethodConfig();

        methodConfig.setMerger("true");
        methodConfig.setName("getAll");

        return methodConfig;
    }

}

首先环境正常,这是我的源码,按照官方文档中的xml初始化的ReferenceBean,但是无法实现分组聚合的效果,设置Group为"*"号,无法找到provider,异常信息Caused by: com.alibaba.dubbo.remoting.RemotingException: com.alibaba.dubbo.remoting.RemotingException: Not found exported service,当我设置Group为注册过的Group时,能正常工作,但是不能设置多个Group,例如:9999,9999a这种格式,它会去找9999,9999a的gourp,而没有根据","做split分别去找9999和9999a两个分组,求助,谢谢!

ld-github commented 7 years ago

官方配置如下:

搜索所有分组

<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" />

合并指定分组

<dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" />

指定方法合并结果,其它未指定的方法,将只调用一个Group

<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="true" />
</dubbo:service>

某个方法不合并结果,其它都合并结果

<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true">
    <dubbo:method name="getMenuItems" merger="false" />
</dubbo:service>
danielamorais commented 5 years ago

Hey,

I need help to understand this issue.. could someone write this issue in English? I could be interested in work on this.

SailLiao commented 5 years ago

@danielamorais org.apache.dubbo.config.annotation.Reference add code

String merger() default "";

use

@Reference(version = "1.0.0", group = "*", merger = "true")
private DemoService demoService;

class org.apache.dubbo.config.ReferenceConfig extends AbstractMethodConfig, it has merger but annotation.Reference not. class org.apache.dubbo.config.spring.util.AnnotationUtils.getAttributes(Annotation annotation, PropertyResolver propertyResolver,boolean ignoreDefaultValue, String... ignoreAttributeNames) will bind annotation value , but add merger() default must is "", beceuse

if (ignoreDefaultValue && nullSafeEquals(attributeValue, getDefaultValue(annotation, attributeName))) {
  continue;
}

will ignore ^_^

superleo-cn commented 5 years ago

I think we can close the issue since the attribute of group of tag only supports one name:

Class: org.apache.dubbo.config.ReferenceConfig.java

// inspect the below method 
private void init() {
    ...
    String serviceKey = URL.buildKey(interfaceName, group, version);
}

Dive into the file URL.java, we cannot find any split operation on the attribute group.

public static String buildKey(String path, String group, String version) {
        StringBuilder buf = new StringBuilder();
        if (group != null && group.length() > 0) {
            buf.append(group).append("/"); //  Just appends the "group" to the path
        }
        buf.append(path);
        if (version != null && version.length() > 0) {
            buf.append(":").append(version);
        }
        return buf.toString();
    }
qixiaobo commented 3 years ago

I think we can close the issue since the attribute of group of tag dubbo:reference only supports one name:

Class: org.apache.dubbo.config.ReferenceConfig.java

// inspect the below method 
private void init() {
    ...
    String serviceKey = URL.buildKey(interfaceName, group, version);
}

Dive into the file URL.java, we cannot find any split operation on the attribute group.

public static String buildKey(String path, String group, String version) {
        StringBuilder buf = new StringBuilder();
        if (group != null && group.length() > 0) {
            buf.append(group).append("/"); //  Just appends the "group" to the path
        }
        buf.append(path);
        if (version != null && version.length() > 0) {
            buf.append(":").append(version);
        }
        return buf.toString();
    }

You're right .But we may have a look at this code

public RegistryDirectory(Class<T> serviceType, URL url) {
        super(url);
        if (serviceType == null)
            throw new IllegalArgumentException("service type is null.");
        if (url.getServiceKey() == null || url.getServiceKey().length() == 0)
            throw new IllegalArgumentException("registry serviceKey is null.");
        this.serviceType = serviceType;
        this.serviceKey = url.getServiceKey();
        this.queryMap = StringUtils.parseQueryString(url.getParameterAndDecoded(Constants.REFER_KEY));
        this.overrideDirectoryUrl = this.directoryUrl = url.setPath(url.getServiceInterface()).clearParameters().addParameters(queryMap).removeParameter(Constants.MONITOR_KEY);
        String group = directoryUrl.getParameter(Constants.GROUP_KEY, "");
        this.multiGroup = group != null && ("*".equals(group) || group.contains(","));
        String methods = queryMap.get(Constants.METHODS_KEY);
        this.serviceMethods = methods == null ? null : Constants.COMMA_SPLIT_PATTERN.split(methods);
    }