亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringCloud 2020-Ribbon負(fù)載均衡服務(wù)調(diào)用的實(shí)現(xiàn)

 更新時(shí)間:2021年03月23日 10:27:03   作者:Cool劉某人  
這篇文章主要介紹了SpringCloud 2020-Ribbon負(fù)載均衡服務(wù)調(diào)用的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、概述

在這里插入圖片描述

官網(wǎng):https://github.com/Netflix/ribbon/wiki/Getting-Started

Ribbon目前也進(jìn)入維護(hù)模式,未來(lái)替換方案:

在這里插入圖片描述

LB(負(fù)載均衡)

在這里插入圖片描述

集中式LB

在這里插入圖片描述

進(jìn)程內(nèi)LB

在這里插入圖片描述

Ribbon就是負(fù)載均衡+RestTemplate調(diào)用

2、Ribbon負(fù)載均衡演示

1、架構(gòu)說(shuō)明

在這里插入圖片描述

總結(jié):Ribbon其實(shí)就是一個(gè)軟負(fù)載均衡的客戶(hù)端組件,他可以和其他所需請(qǐng)求的客戶(hù)端結(jié)合使用,和eureka結(jié)合只是其中的一個(gè)實(shí)例。
2、

在這里插入圖片描述
在這里插入圖片描述

3、二說(shuō)RestTemplate的使用

官網(wǎng)
修改cloud-consumer-order80

getForObject方法/getForEntity方法

在這里插入圖片描述

postForObject/postForEntity

在這里插入圖片描述

  • GET請(qǐng)求方法
  • POST請(qǐng)求方法

4、依次2啟動(dòng)7001,7002,8001,8002,80。訪問(wèn):http://localhost/consumer/payment/getForEntity/31

在這里插入圖片描述 

3、Ribbon核心組件IRule

IRule:根據(jù)特定算法從服務(wù)列表中選取一個(gè)要訪問(wèn)的服務(wù)

在這里插入圖片描述

Ribbon自帶負(fù)載均衡算法:

在這里插入圖片描述

如何替換負(fù)載均衡算法:修改cloud-consumer-order80
1、注意配置細(xì)節(jié)

在這里插入圖片描述

2、新建package

在這里插入圖片描述

3、在myrule下面新建配置類(lèi)MySelfRule

package com.liukai.myrule;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName MySelfRule.java
 * @Description TODO
 * @createTime 2021年03月21日 11:50:00
 */
@Configuration
public class MySelfRule {

 @Bean(name = "myRandomRule")
 public IRule myRule(){
  return new RandomRule();//定義為隨機(jī)
 }
}

4、主啟動(dòng)類(lèi)添加@RibbonClient

package com.liukai.springcloud;

import com.liukai.myrule.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName OrderMain80.java
 * @Description TODO
 * @createTime 2021年03月19日 18:27:00
 */
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class OrderMain80 {

 public static void main(String[] args) {
   SpringApplication.run(OrderMain80.class);
 }

}

5、測(cè)試:依次啟動(dòng)7001,7002,8001,8002,cloud-consumer-order80
訪問(wèn):http://localhost/consumer/payment/get/31
多方問(wèn)幾次,可以發(fā)現(xiàn)查詢(xún)的端口號(hào)是隨機(jī)的,而不是交替出現(xiàn)了

在這里插入圖片描述

4、Ribbon負(fù)載均衡算法

4.1 原理 + 源碼

1、注釋掉cloud-consumer-order80主啟動(dòng)類(lèi)的@RibbonClient
2、原理

在這里插入圖片描述

3、源碼:

 public Server choose(ILoadBalancer lb, Object key) {
  if (lb == null) {
   log.warn("no load balancer");
   return null;
  }

  Server server = null;
  int count = 0;
  while (server == null && count++ < 10) {
   List<Server> reachableServers = lb.getReachableServers();
   List<Server> allServers = lb.getAllServers();
   int upCount = reachableServers.size();
   int serverCount = allServers.size();

   if ((upCount == 0) || (serverCount == 0)) {
    log.warn("No up servers available from load balancer: " + lb);
    return null;
   }

   int nextServerIndex = incrementAndGetModulo(serverCount);
   server = allServers.get(nextServerIndex);

   if (server == null) {
    /* Transient. */
    Thread.yield();
    continue;
   }

   if (server.isAlive() && (server.isReadyToServe())) {
    return (server);
   }

   // Next.
   server = null;
  }

  if (count >= 10) {
   log.warn("No available alive servers after 10 tries from load balancer: "
     + lb);
  }
  return server;
 }

 /**
  * Inspired by the implementation of {@link AtomicInteger#incrementAndGet()}.
  *
  * @param modulo The modulo to bound the value of the counter.
  * @return The next value.
  */
 private int incrementAndGetModulo(int modulo) {
  for (;;) {
   int current = nextServerCyclicCounter.get();
   int next = (current + 1) % modulo;
   if (nextServerCyclicCounter.compareAndSet(current, next))
    return next;
  }
 }

4.2 手寫(xiě)負(fù)載均衡算法

1、修改8001,8002的controller

// 手寫(xiě)負(fù)載均衡需要用到
 @GetMapping(value = "/payment/lb")
 public String getPaymentLB(){
  return serverPort;
 }

2、cloud-consumer-order80的ApplicationContextBean去掉@LoadBalanced
3、新建接口LoadBalancer

package com.liukai.springcloud.lb;

import org.springframework.cloud.client.ServiceInstance;

import java.util.List;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName LoadBalancer.java
 * @Description TODO
 * @createTime 2021年03月21日 12:24:00
 */
public interface LoadBalancer {
 //收集服務(wù)器總共有多少臺(tái)能夠提供服務(wù)的機(jī)器,并放到list里面
 ServiceInstance instances(List<ServiceInstance> serviceInstances);
}

4、新建實(shí)現(xiàn)類(lèi)MyLB

package com.liukai.springcloud.lb;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName MyLB.java
 * @Description TODO
 * @createTime 2021年03月21日 12:27:00
 */
@Component
public class MyLB implements LoadBalancer {

 private AtomicInteger atomicInteger = new AtomicInteger(0);

 //坐標(biāo)
 private final int getAndIncrement() {
  int current;
  int next;
  do {
   current = this.atomicInteger.get();
   next = current >= 2147483647 ? 0 : current + 1;
  } while (!this.atomicInteger.compareAndSet(current, next)); //第一個(gè)參數(shù)是期望值,第二個(gè)參數(shù)是修改值是
  System.out.print("*******第幾次訪問(wèn),次數(shù)next: " + next);
  return next;
 }



 @Override
 public ServiceInstance instances(List<ServiceInstance> serviceInstances) { //得到機(jī)器的列表
  int index = getAndIncrement() % serviceInstances.size(); //得到服務(wù)器的下標(biāo)位置
  System.out.println(" ====>端口:" + serviceInstances.get(index).getPort());
  return serviceInstances.get(index);
 }
}

5、修改OrderController

@Resource
 private LoadBalancer loadBalancer;

 @Resource
 private DiscoveryClient discoveryClient;

 @GetMapping(value = "/consumer/payment/lb")
 public String getPaymentLB(){
  List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
  if (instances == null || instances.size() <= 0){
   return null;
  }
//  instances.forEach(System.out::println);
  // 使用手寫(xiě)的負(fù)載均衡算法獲取服務(wù)
  ServiceInstance serviceInstance = loadBalancer.instances(instances);
  // 獲取服務(wù)的地址
  URI uri = serviceInstance.getUri();
  // 拼接地址訪問(wèn)
  return restTemplate.getForObject(uri+"/payment/lb",String.class);
 }

6、測(cè)試:訪問(wèn) http://localhost/consumer/payment/lb
發(fā)現(xiàn)訪問(wèn)的端口號(hào)開(kāi)始輪詢(xún)出現(xiàn),手寫(xiě)負(fù)載均衡輪詢(xún)算法成功

在這里插入圖片描述
在這里插入圖片描述

到此這篇關(guān)于SpringCloud 2020-Ribbon負(fù)載均衡服務(wù)調(diào)用的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCloud Ribbon負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis千萬(wàn)級(jí)數(shù)據(jù)查詢(xún)的解決方式,避免OOM問(wèn)題

    Mybatis千萬(wàn)級(jí)數(shù)據(jù)查詢(xún)的解決方式,避免OOM問(wèn)題

    這篇文章主要介紹了Mybatis千萬(wàn)級(jí)數(shù)據(jù)查詢(xún)的解決方式,避免OOM問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決

    RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決

    這篇文章主要介紹了RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringCloud整合Nacos實(shí)現(xiàn)流程詳解

    SpringCloud整合Nacos實(shí)現(xiàn)流程詳解

    這篇文章主要介紹了SpringCloud整合Nacos實(shí)現(xiàn)流程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot使用@Async注解實(shí)現(xiàn)異步調(diào)用

    SpringBoot使用@Async注解實(shí)現(xiàn)異步調(diào)用

    這篇文章主要介紹了SpringBoot使用@Async注解實(shí)現(xiàn)異步調(diào)用,異步調(diào)用是相對(duì)于同步調(diào)用而言的,同步調(diào)用是指程序按預(yù)定順序一步步執(zhí)行,每一步必須等到上一步執(zhí)行完后才能執(zhí)行,異步調(diào)用則無(wú)需等待,程序執(zhí)行完即可執(zhí)行,可以減少程序執(zhí)行時(shí)間,需要的朋友可以參考下
    2023-10-10
  • java?讀寫(xiě)鎖的使用及它的優(yōu)點(diǎn)

    java?讀寫(xiě)鎖的使用及它的優(yōu)點(diǎn)

    這篇文章主要介紹了java?讀寫(xiě)鎖的使用及它的優(yōu)點(diǎn),讀寫(xiě)鎖的特點(diǎn)就是是讀讀不互斥、讀寫(xiě)互斥、寫(xiě)寫(xiě)互斥,下面具體使用分享需要的小伙伴可以參考一下
    2022-05-05
  • java中Javers?比較兩個(gè)類(lèi)的差異

    java中Javers?比較兩個(gè)類(lèi)的差異

    本文主要介紹了Javers?比較兩個(gè)類(lèi)的差異,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • java設(shè)計(jì)模式之淺談適配器模式

    java設(shè)計(jì)模式之淺談適配器模式

    我們現(xiàn)實(shí)生活中的適配器不少.例如,我們使用存儲(chǔ)卡適配器連接存儲(chǔ)卡和一個(gè)計(jì)算機(jī),因?yàn)橛?jì)算機(jī)僅支持一種類(lèi)型的存儲(chǔ)卡和我們的卡不與計(jì)算機(jī)兼容,適配器是兩種不相容的實(shí)體之間的轉(zhuǎn)換器,適配器模式是一種結(jié)構(gòu)模式.本文就帶大家了解一下java適配器模式,需要的朋友可以參考下
    2021-06-06
  • Java中Collections.sort的使用

    Java中Collections.sort的使用

    本文主要介紹了Java中Collections.sort的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • SpringBoot項(xiàng)目啟動(dòng)時(shí)如何讀取配置以及初始化資源

    SpringBoot項(xiàng)目啟動(dòng)時(shí)如何讀取配置以及初始化資源

    這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目啟動(dòng)時(shí)如何讀取配置以及初始化資源的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 一文詳解Java Netty中的Constant類(lèi)

    一文詳解Java Netty中的Constant類(lèi)

    這篇文章主要介紹了Constants類(lèi)即常量類(lèi)是將一些常用的變量集合到一個(gè)地方的類(lèi),文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考一下
    2023-05-05

最新評(píng)論