diamond源码阅读-获取服务器列表
serverAddressProcessor
public synchronized void start() {
if (isRun) {
return;
}
isRun = true;
initHttpClient();//初始化HttpClient
if (this.diamondConfigure.isLocalFirst()) {
acquireServerAddressFromLocal();//如果是本地,从本地获取服务器列表
}
else {
synAcquireServerAddress();
//如果不再异步每隔一段通过域名时间去取diamondIpList,注释掉下面这行
//asynAcquireServerAddress();
asynAcquireServerAddress();
}
1 initHttpClinet()
private void initHttpClient() {
HostConfiguration hostConfiguration = new HostConfiguration();
SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
connectionManager.closeIdleConnections(5000L);//空闲时间
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
connectionManager.setParams(params);
configHttpClient = new HttpClient(connectionManager);
configHttpClient.setHostConfiguration(hostConfiguration);
}
2 acquireServerAddressFromLocal
protected void acquireServerAddressFromLocal() {
if (!isRun) {
throw new RuntimeException("ServerAddressProcessor不在运行状态,无法同步获取服务器地址列表");
}
if (MockServer.isTestMode()) {
diamondConfigure.addDomainName("测试模式,没有使用的真实服务器");
return;
}
int acquireCount = 0;
if (diamondConfigure.getDomainNameList().size() == 0) {
reloadServerAddresses();
if (diamondConfigure.getDomainNameList().size() == 0) {
if (!acquireServerAddressOnce(acquireCount)) {//从本地获取 发送http请求
acquireCount++;
if (acquireServerAddressOnce(acquireCount)) {//从服务器获取
// 存入本地文件
storeServerAddressesToLocal();
log.info("在同步获取服务器列表时,向日常ConfigServer服务器获取到了服务器列表");
}
else {
throw new RuntimeException("当前没有可用的服务器列表");
}
}
else {
log.info("在同步获取服务器列表时,向线上ConfigServer服务器获取到了服务器列表");
// 存入本地文件
storeServerAddressesToLocal();
}
}
else {
log.info("在同步获取服务器列表时,由于本地指定了服务器列表,不向ConfigServer服务器同步获取服务器列表");
}
}
}
3 类似第2步
protected void synAcquireServerAddress() {
if (!isRun) {
throw new RuntimeException("ServerAddressProcessor不在运行状态,无法同步获取服务器地址列表");
}
if (MockServer.isTestMode()) {
diamondConfigure.addDomainName("测试模式,没有使用的真实服务器");
return;
}
if (reloadServerAddresses()) {
log.info("成功从本地获取Diamond地址列表");
return ;
}
int acquireCount = 0;
if (diamondConfigure.getDomainNameList().size() == 0) {
if (!acquireServerAddressOnce(acquireCount)) {
acquireCount++;
if (acquireServerAddressOnce(acquireCount)) {
// 存入本地文件
storeServerAddressesToLocal();
log.info("在同步获取服务器列表时,向日常ConfigServer服务器获取到了服务器列表");
}
else {
log.info("从本地获取Diamond地址列表");
reloadServerAddresses();
if (diamondConfigure.getDomainNameList().size() == 0)
throw new RuntimeException("当前没有可用的服务器列表");
}
}
else {
log.info("在同步获取服务器列表时,向线上ConfigServer服务器获取到了服务器列表");
// 存入本地文件
storeServerAddressesToLocal();
}
}
if (diamondConfigure.getDomainNameList().size() == 0) {
throw new RuntimeException("当前没有可用的服务器列表");
}
}
4 开线程获取服务器地址列表
protected void asynAcquireServerAddress() {
if (MockServer.isTestMode()) {
return;
}
this.scheduledExecutor.schedule(new Runnable() {
public void run() {
if (!isRun) {
log.warn("ServerAddressProcessor不在运行状态,无法异步获取服务器地址列表");
return;
}
int acquireCount = 0;
if (!acquireServerAddressOnce(acquireCount)) {
acquireCount++;
if (acquireServerAddressOnce(acquireCount)) {
// 存入本地文件
storeServerAddressesToLocal();
}
}
else {
// 存入本地文件
storeServerAddressesToLocal();
}
asynAcquireServerAddress();
}
}, asynAcquireIntervalInSec, TimeUnit.SECONDS);
}
5 获取到服务器列表后
this.diamondConfigure.setDomainNameList(newDomainNameList);