Commit 83b04df5 by George.Z.Lin Committed by nobodyiam

.meta 支持多个host配置(逗号分割)

parent 08720578
package com.ctrip.framework.apollo.core;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.NetUtil;
import com.ctrip.framework.apollo.core.utils.ResourceUtils;
import java.util.HashMap;
......@@ -38,6 +39,8 @@ public class MetaDomainConsts {
}
public static String getDomain(Env env) {
return String.valueOf(domains.get(env));
String metaAddress = String.valueOf(domains.get(env));
String validAddress = NetUtil.getValidAddress( metaAddress );
return validAddress;
}
}
package com.ctrip.framework.apollo.core.utils;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by gl49 on 2018/6/8.
*/
public class NetUtil {
public static int getUrlStatus(String address){
int statusCode = 0;
try {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
oc.setUseCaches(false);
oc.setConnectTimeout(5000);
statusCode = oc.getResponseCode();
if (200 == statusCode) {
return statusCode;
}
} catch (Exception ignore) {
}
return statusCode;
}
public static boolean checkUrl(String address){
int status = getUrlStatus(address);
if( 0 == status ){ //异常状态重试一次
status = getUrlStatus(address);
}
if( 200 == status ){
return true;
}
return false;
}
public static String getValidAddress(String metaAddress) {
String validAddress = null;
String[] addressArr = changeAddressArr(metaAddress);
for(String address : addressArr){
if(NetUtil.checkUrl(address)){
validAddress = address;
break;
}
}
if(null == validAddress){
throw new RuntimeException("invalid meta address, please check $env_meta config!");
}
return validAddress;
}
private static String[] changeAddressArr(String address) {
String[] addressArr = address.split(",");
return addressArr;
}
}
package com.ctrip.framework.apollo.core.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* Created by gl49 on 2018/6/8.
*/
public class NetUtilTest {
@Test
public void testCheckUrl(){
String apolloUrl = "http://www.ctrip.com";
Assert.assertEquals( NetUtil.checkUrl(apolloUrl), true);
}
@Test
public void testGetValidAddress(){
String addressStr = "http://www.ctrip.com,http://www.ctrip.com2";
String address = NetUtil.getValidAddress(addressStr);
Assert.assertEquals( address, "http://www.ctrip.com");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment