-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client side retry and logging to CAT
- Loading branch information
Showing
18 changed files
with
343 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 52 additions & 17 deletions
69
apollo-client/src/main/java/com/ctrip/apollo/internals/ConfigServiceLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,87 @@ | ||
package com.ctrip.apollo.internals; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import com.ctrip.apollo.core.dto.ServiceDTO; | ||
import com.ctrip.apollo.util.ConfigUtil; | ||
import com.ctrip.apollo.util.http.HttpRequest; | ||
import com.ctrip.apollo.util.http.HttpResponse; | ||
import com.ctrip.apollo.util.http.HttpUtil; | ||
import com.dianping.cat.Cat; | ||
import com.dianping.cat.message.Message; | ||
import com.dianping.cat.message.Transaction; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.unidal.lookup.annotation.Inject; | ||
import org.unidal.lookup.annotation.Named; | ||
|
||
import java.util.ArrayList; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
@Named(type = ConfigServiceLocator.class) | ||
public class ConfigServiceLocator { | ||
private static final Logger logger = LoggerFactory.getLogger(ConfigServiceLocator.class); | ||
@Inject | ||
private HttpUtil m_httpUtil; | ||
@Inject | ||
private ConfigUtil m_configUtil; | ||
private List<ServiceDTO> serviceCaches = new ArrayList<>(); | ||
private AtomicReference<List<ServiceDTO>> m_configServices; | ||
private Type m_responseType; | ||
|
||
public ConfigServiceLocator() { | ||
List<ServiceDTO> initial = Lists.newArrayList(); | ||
m_configServices = new AtomicReference<>(initial); | ||
m_responseType = new TypeToken<List<ServiceDTO>>() { | ||
}.getType(); | ||
} | ||
|
||
/** | ||
* Get the config service info from remote meta server. | ||
* | ||
* @return the services dto | ||
*/ | ||
public List<ServiceDTO> getConfigServices() { | ||
if (m_configServices.get().isEmpty()) { | ||
updateConfigServices(); | ||
} | ||
|
||
return m_configServices.get(); | ||
} | ||
|
||
//TODO periodically update config services | ||
private void updateConfigServices() { | ||
String domainName = m_configUtil.getMetaServerDomainName(); | ||
String url = domainName + "/services/config"; | ||
|
||
HttpRequest request = new HttpRequest(url); | ||
int maxRetries = 5; | ||
Throwable exception = null; | ||
|
||
for (int i = 0; i < maxRetries; i++) { | ||
Transaction transaction = Cat.newTransaction("Apollo.MetaService", "getConfigService"); | ||
transaction.addData("Url", url); | ||
try { | ||
HttpResponse<List<ServiceDTO>> response = m_httpUtil.doGet(request, m_responseType); | ||
m_configServices.set(response.getBody()); | ||
Cat.logEvent("Apollo.Config.Services", response.getBody().toString()); | ||
transaction.setStatus(Message.SUCCESS); | ||
return; | ||
} catch (Throwable ex) { | ||
Cat.logError(ex); | ||
transaction.setStatus(ex); | ||
exception = ex; | ||
} finally { | ||
transaction.complete(); | ||
} | ||
|
||
try { | ||
HttpResponse<ServiceDTO[]> response = m_httpUtil.doGet(request, ServiceDTO[].class); | ||
ServiceDTO[] services = response.getBody(); | ||
if (services != null && services.length > 0) { | ||
serviceCaches.clear(); | ||
for (ServiceDTO service : services) { | ||
serviceCaches.add(service); | ||
} | ||
try { | ||
TimeUnit.SECONDS.sleep(1); | ||
} catch (InterruptedException e) { | ||
//ignore | ||
} | ||
} catch (Throwable ex) { | ||
logger.error("Get config services failed", ex); | ||
throw new RuntimeException("Get config services failed", ex); | ||
} | ||
|
||
return serviceCaches; | ||
throw new RuntimeException("Get config services failed", exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.