IPing of Ribbon
IPing is in the Ribbon framework, responsible for checking whether the service instance is alive (UP).
IPing
Public interface IPing {/ / Interface public boolean isAlive (Server server) to check for survival;}
DummyPing
A dummy IPing implementation that always returns true.
Public class DummyPing extends AbstractLoadBalancerPing {public DummyPing () {} / / directly returns true public boolean isAlive (Server server) {return true;} @ Override public void initWithNiwsConfig (IClientConfig clientConfig) {}} NoOpPing
Do nothing and go straight back to true.
Public class NoOpPing implements IPing {@ Override public boolean isAlive (Server server) {return true;}} PingConstant
The IPing implementation of a tool class, as long as the constant parameter is true, indicates that the service is alive, otherwise it is all invalid service instances.
Public class PingConstant implements IPing {boolean constant = true; public void setConstant (String constantStr) {constant = (constantStr! = null) & & (constantStr.toLowerCase (). Equals ("true"));} public void setConstant (boolean constant) {this.constant = constant;} public boolean getConstant () {return constant } public boolean isAlive (Server server) {return constant;}} PingUrl
The survival of the service is determined by the status code returned by the request access service.
Public class PingUrl implements IPing {public boolean isAlive (Server server) {String urlStr = ""; if (isSecure) {urlStr = "https://";} else {urlStr =" http://";} urlStr + = server.getId (); urlStr + = getPingAppendString (); boolean isAlive = false HttpClient httpClient = new DefaultHttpClient (); HttpUriRequest getRequest = new HttpGet (urlStr); String content=null; try {HttpResponse response = httpClient.execute (getRequest); content= EntityUtils.toString (response.getEntity ()); isAlive = (response.getStatusLine (). GetStatusCode () = 200) / / determine whether the service instance is valid according to the status code and the content returned. If (getExpectedContent ()! = null) {LOGGER.debug ("content:" + content); if (content = = null) {isAlive = false } else {if (content.equals (getExpectedContent () {isAlive = true;} else {isAlive = false;} catch (IOException e) {e.printStackTrace () } finally {/ / Release the connection. GetRequest.abort ();} return isAlive;}} NIWSDiscoveryPing
Eureka is used to determine whether the service instance is alive.
Public class NIWSDiscoveryPing extends AbstractLoadBalancerPing {public boolean isAlive (Server server) {boolean isAlive = true; if (serverables invalid null & & server instanceof DiscoveryEnabledServer) {DiscoveryEnabledServer dServer = (DiscoveryEnabledServer) server; / / determine whether the service is alive or not by Eureka's service mechanism InstanceInfo instanceInfo = dServer.getInstanceInfo () If (instanceInfooted null) {InstanceStatus status = instanceInfo.getStatus (); if (statusinstalled null) {isAlive = status.equals (InstanceStatus.UP);} return isAlive;}}