Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

IPing of Ribbon

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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;}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report