sofastack-guides / kc-sofastack-demo

SOFAStack Demo for SOFAStack Cloud Native Workshop on KubeCon China 2019
Apache License 2.0
46 stars 47 forks source link

application.properties中注册中心集群部署的情况下配置怎么写? #16

Closed bwu41 closed 3 years ago

bwu41 commented 3 years ago

按官网集成部署集群模式搭建了SOFA Registry 地址分别是:192.168.4.100, 192.168.4.101, 192.168.4.102

在项目application.properties中添加注册中心:

  1. 配置一台的时候可以成功

    # 添加服务注册中心地址
    com.alipay.sofa.rpc.registry.address=sofa://192.168.4.100:9603
  2. 配置集群地址的时候 失败

    
    # 添加服务注册中心地址
    com.alipay.sofa.rpc.registry.address=sofa://192.168.4.100:9603, sofa://192.168.4.101:9603, sofa://192.168.4.102:9603

com.alipay.sofa.rpc.registry.address=sofa://192.168.4.100:9603, 192.168.4.101:9603, 192.168.4.102:9603



调用RPC会报错:
`2021-01-17 16:11:56.835 ERROR 1 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServrew exception [Request processing failed; nested exception is com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-020060001: Cannot get [com.xxx.xxx.service.XXXXService:1.0], please check the registry log. ] with root cause`

----

猜测是添加服务中心地址(com.alipay.sofa.rpc.registry.address)的语法有问题,但又找不到其他可参考资料,还请好心人不吝赐教。

@ujjboy 
bwu41 commented 3 years ago

看了下源码,貌似不支持上面注册多个ip地址的写法:

/**
 * SOFARegistry 配置
 * <p>
 * 配置格式: com.alipay.sofa.rpc.registry.address=sofa://xxx:9600?k1=v1
 *
 * @author <a href="mailto:zhiyuan.lzy@antfin.com">LiWei</a>
 */
public class SofaRegistryConfigurator implements RegistryConfigureProcessor {

    public SofaRegistryConfigurator() {
    }

    @Override
    public RegistryConfig buildFromAddress(String address) {
        String sofaRegistryAddress = RegistryParseUtil.parseAddress(address,
            SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA);
        Map<String, String> map = RegistryParseUtil.parseParam(address,
            SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA);

        return new RegistryConfig().setAddress(sofaRegistryAddress)
            .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA).setParameters(map);
    }

    @Override
    public String registryType() {
        return SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_SOFA;
    }
}
public class RegistryParseUtil {

    /**
     * Parse address string.
     *
     * @param config the config 
     * @param protocol the protocol 
     * @return the string
     */
    public static String parseAddress(String config, String protocol) {
        String address = null;

        if (StringUtils.isNotEmpty(config) && config.startsWith(protocol)) {
            final String nacosProtocol = protocol + "://";
            String value = config.substring(nacosProtocol.length());
            if (!value.contains("?")) {
                address = value;
            } else {
                int index = value.lastIndexOf('?');
                address = value.substring(0, index);
            }
        }

        return address;
    }

    /**
     * Parse param map.
     *
     * @param address the address 
     * @param protocol the protocol 
     * @return the map
     */
    public static Map<String, String> parseParam(String address, String protocol) {

        String host = parseAddress(address, protocol);

        //for config ?
        String paramString = address.substring(address.indexOf(host) + host.length());

        if (StringUtils.isNotEmpty(paramString) && paramString.startsWith("?")) {
            paramString = paramString.substring(1);
        }

        Map<String, String> map = new HashMap<String, String>();
        if (paramString.contains("&")) {
            String[] paramSplit = paramString.split("&");
            for (String param : paramSplit) {
                Map<String, String> tempMap = parseKeyValue(param);
                map.putAll(tempMap);
            }
        } else {
            Map<String, String> tempMap = parseKeyValue(paramString);
            map.putAll(tempMap);
        }

        return map;
    }
}

是不是意味着只需要写入一个ip即可呢?

ujjboy commented 3 years ago

@bwu41 写一个即可,集群内地址有自发现机制。

bwu41 commented 3 years ago

@ujjboy 明白~ 谢谢!