SofaRegistryHelper { public static String convertProviderToUrls(ProviderConfig providerConfig, ServerConfig server) { StringBuilder sb = new StringBuilder(200); String appName = providerConfig.getAppName(); String host = server.getVirtualHost(); if (host == null) { host = server.getHost(); if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) { host = SystemInfo.getLocalHost(); } } else { if (LOGGER.isWarnEnabled(appName)) { LOGGER.warnWithApp(appName, "Virtual host is specified, host will be change from {} to {} when register", server.getHost(), host); } } Integer port = server.getVirtualPort(); if (port == null) { port = server.getPort(); } else { if (LOGGER.isWarnEnabled(appName)) { LOGGER.warnWithApp(appName, "Virtual port is specified, host will be change from {} to {} when register", server.getPort(), port); } } String protocol = server.getProtocol(); sb.append(host).append(":").append(port).append(server.getContextPath()); sb.append("?").append(ATTR_RPC_VERSION).append("=").append(Version.RPC_VERSION); sb.append(getKeyPairs(ATTR_SERIALIZATION, providerConfig.getSerialization())); sb.append(getKeyPairs(ATTR_WEIGHT, providerConfig.getWeight())); if (providerConfig.getTimeout() > 0) { sb.append(getKeyPairs(ATTR_TIMEOUT, providerConfig.getTimeout())); } sb.append(getKeyPairs(ATTR_APP_NAME, appName)); if (PROTOCOL_TYPE_BOLT.equals(protocol)) { sb.append(getKeyPairs(RPC_REMOTING_PROTOCOL, RemotingConstants.PROTOCOL_BOLT)); } else if (PROTOCOL_TYPE_TR.equals(protocol)) { sb.append(getKeyPairs(RPC_REMOTING_PROTOCOL, RemotingConstants.PROTOCOL_TR)); } sb.append(getKeyPairs(RPC_SERVICE_VERSION, SOFA4_RPC_SERVICE_VERSION)); sb.append(getKeyPairs(SERIALIZE_TYPE_KEY, providerConfig.getSerialization())); sb.append(getKeyPairs(WEIGHT_KEY, providerConfig.getWeight())); if (providerConfig.getTimeout() > 0) { sb.append(getKeyPairs(TIMEOUT, providerConfig.getTimeout())); } sb.append(getKeyPairs(APP_NAME, appName)); if (StringUtils.isNotBlank(SystemInfo.getHostMachine())) { sb.append(getKeyPairs(HOST_MACHINE_KEY, SystemInfo.getHostMachine())); } Map<String, MethodConfig> methodConfigs = providerConfig.getMethods(); if (CommonUtils.isNotEmpty(methodConfigs)) { for (Map.Entry<String, MethodConfig> entry : methodConfigs.entrySet()) { String methodName = entry.getKey(); MethodConfig methodConfig = entry.getValue(); sb.append(getKeyPairs("." + methodName + "." + ATTR_TIMEOUT, methodConfig.getTimeout())); String key = "[" + methodName + "]"; String value = "[" + KEY_TIMEOUT + "#" + methodConfig.getTimeout() + "]"; sb.append(getKeyPairs(key, value)); } } sb.append(convertMap2Pair(providerConfig.getParameters())); addCommonAttrs(sb); return sb.toString(); }  static String buildListDataId(AbstractInterfaceConfig config, String protocol); static String convertProviderToUrls(ProviderConfig providerConfig, ServerConfig server); static List<ProviderInfo> parseProviderInfos(List<String> urls); static ProviderInfo parseProviderInfo(String originUrl); static final String PUBLISHER_PREFIX; static final String SUBSCRIBER_PREFIX; static final String SUBSCRIBER_LIST_GROUP_ID; static final String SUBSCRIBER_CONFIG_GROUP_ID; static final String PROTOCOL_TYPE_OVERRIDE; }
@Test public void convertProviderToUrls() throws Exception { ServerConfig serverConfig = new ServerConfig() .setHost("0.0.0.0") .setPort(22000) .setProtocol("bolt"); ServerConfig serverConfig2 = new ServerConfig() .setHost("127.0.0.1") .setPort(12200) .setProtocol("tr"); ServerConfig serverConfig3 = new ServerConfig() .setHost("192.1.1.1") .setPort(8080) .setProtocol("xfire"); ProviderConfig<?> providerConfig = new ProviderConfig(); providerConfig .setInterfaceId("com.alipay.sofa.rpc.test.TestService") .setUniqueId("qqqq") .setApplication(new ApplicationConfig().setAppName("xxxx")) .setTimeout(4444) .setWeight(250) .setServer(Arrays.asList(serverConfig, serverConfig2)); MethodConfig methodConfig = new MethodConfig().setName("echo").setTimeout(3333); MethodConfig methodConfig2 = new MethodConfig().setName("xx").setTimeout(2222); providerConfig.setMethods(Arrays.asList(methodConfig, methodConfig2)); String s1 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig); Assert.assertNotNull(s1); ProviderInfo providerInfo = SofaRegistryHelper.parseProviderInfo(s1); Assert.assertEquals(SystemInfo.getLocalHost(), providerInfo.getHost()); Assert.assertEquals(serverConfig.getPort(), providerInfo.getPort()); Assert.assertEquals(providerConfig.getAppName(), providerInfo.getAttr(ProviderInfoAttrs.ATTR_APP_NAME)); Assert.assertEquals(providerConfig.getTimeout(), providerInfo.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT)); String s2 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig2); Assert.assertNotNull(s2); ProviderInfo providerInfo2 = SofaRegistryHelper.parseProviderInfo(s2); Assert.assertEquals(SystemInfo.getLocalHost(), providerInfo.getHost()); Assert.assertEquals(serverConfig2.getPort(), providerInfo2.getPort()); Assert.assertEquals(providerConfig.getAppName(), providerInfo2.getAttr(ProviderInfoAttrs.ATTR_APP_NAME)); Assert.assertEquals(providerConfig.getTimeout(), providerInfo2.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT)); String s3 = SofaRegistryHelper.convertProviderToUrls(providerConfig, serverConfig3); Assert.assertNotNull(s3); ProviderInfo providerInfo3 = SofaRegistryHelper.parseProviderInfo(s3); Assert.assertEquals(serverConfig3.getHost(), providerInfo3.getHost()); Assert.assertEquals(serverConfig3.getPort(), providerInfo3.getPort()); Assert.assertEquals(providerConfig.getAppName(), providerInfo3.getAttr(ProviderInfoAttrs.ATTR_APP_NAME)); Assert.assertEquals(providerConfig.getTimeout(), providerInfo3.getDynamicAttr(ProviderInfoAttrs.ATTR_TIMEOUT)); }