日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringCloud Ribbon(三)之IPing机制

發布時間:2023/12/3 javascript 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringCloud Ribbon(三)之IPing机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、IPing機制

IPing是一個主動探測服務節點存活的機制,通過判斷服務節點的當前狀態,設置節點的可用狀態。只有當節點為可用時候才會作為負載均衡器的選取節點。

IPing有以下幾種模式:

  • DummyPing:默認返回true,即認為所有節點都可用,這也是單獨使用Ribbon時的默認模式
  • NIWSDiscoveryPing:借助Eureka服務發現機制獲取節點狀態。節點狀態是UP,則認為是可用狀態
  • PingUrl:主動向服務節點發起一次http調用,對方有響應則認為節點是可用狀態
  • NoOpPing:返回true
  • PingConstant:返回設置的常量值

?

二、IPing配置

(1)application.yaml配置

#單個服務設置 [service-name]: ribbon: NFLoadBalancerPingClassName: com.netflix.loadbalancer.DummyPing

(2)代碼配置

public class MicroRibbonConfig {@Beanpublic IPing microIPing(){return new DummyPing();} }@RibbonClient(name = "micro-service", configuration = MicroRibbonConfig.class) public class RibbonClientConfig {}

?

三、IPing模式實現

(1)DummyPing

public class DummyPing extends AbstractLoadBalancerPing {public DummyPing() {}public boolean isAlive(Server server) {return true;}@Overridepublic void initWithNiwsConfig(IClientConfig clientConfig) {} }public abstract class AbstractLoadBalancerPing implements IPing, IClientConfigAware{AbstractLoadBalancer lb;@Overridepublic boolean isAlive(Server server) {return true;}public void setLoadBalancer(AbstractLoadBalancer lb){this.lb = lb;}public AbstractLoadBalancer getLoadBalancer(){return lb;}}

(2)PingUrl

public class PingUrl implements IPing {private static final Logger LOGGER = LoggerFactory.getLogger(PingUrl.class);String pingAppendString = "";boolean isSecure = false;String expectedContent = null;/*** Send one ping only.** Well, send what you need to determine whether or not the* server is still alive. Should return within a "reasonable"* time.*/public PingUrl() {}public PingUrl(boolean isSecure, String pingAppendString) {this.isSecure = isSecure;this.pingAppendString = (pingAppendString != null) ? pingAppendString : "";}public void setPingAppendString(String pingAppendString) {this.pingAppendString = (pingAppendString != null) ? pingAppendString : "";}public String getPingAppendString() {return pingAppendString;}public boolean isSecure() {return isSecure;}/*** Should the Secure protocol be used to Ping* @param isSecure*/public void setSecure(boolean isSecure) {this.isSecure = isSecure;}public String getExpectedContent() {return expectedContent;}/*** Is there a particular content you are hoping to see?* If so -set this here.* for e.g. the WCS server sets the content body to be 'true'* Please be advised that this content should match the actual * content exactly for this to work. Else yo may get false status.* @param expectedContent*/public void setExpectedContent(String expectedContent) {this.expectedContent = expectedContent;}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);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;}public static void main(String[] args){PingUrl p = new PingUrl(false,"/cs/hostRunning");p.setExpectedContent("true");Server s = new Server("ec2-75-101-231-85.compute-1.amazonaws.com", 7101);boolean isAlive = p.isAlive(s);System.out.println("isAlive:" + isAlive);} }

(3)PingConstant?

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

(4)NoOpPing

public class NoOpPing implements IPing {@Overridepublic boolean isAlive(Server server) {return true;}}

?

總結

以上是生活随笔為你收集整理的SpringCloud Ribbon(三)之IPing机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。