成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么理解SpringCloud的負(fù)載均衡策略+重試機(jī)制+Hystrix熔斷器

本篇內(nèi)容介紹了“怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

專注于為中小企業(yè)提供網(wǎng)站制作、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)朝陽免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

負(fù)載均衡策略  初體驗(yàn):

  • 步驟一:修改pom文件,修改服務(wù)調(diào)用方的pom文件,添加test依賴

<!--測(cè)試-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器

  • 步驟二:編寫測(cè)試類,在服務(wù)調(diào)用方編寫Junit測(cè)試類

package com.czxy;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;


@SpringBootTest(classes=Client4Application.class)       //Junit和Spring boot整合,將所有實(shí)例加載到spring容器。
@RunWith(SpringRunner.class)        //JUnit和Spring整合,將spring容器中的數(shù)據(jù)注入當(dāng)前類
public class TestRibbon {
    @Resource
    private RibbonLoadBalancerClient client;

    @Test
    public void testDemo(){
        // 通過“服務(wù)名”獲得對(duì)應(yīng)服務(wù)實(shí)例
        for (int i = 0; i < 10; i++) {
            ServiceInstance instance = client.choose("service4");
            System.out.println(instance.getHost() + ":">
  • 步驟三:修改提供方y(tǒng)ml文件,支持顯示IP地址,并重啟8081和8082

#服務(wù)名
spring:
  application:
    name: service4
#注冊(cè)地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    prefer-ip-address: true   #顯示IP地址

修改策略

給指定的“服務(wù)”設(shè)置策略

服務(wù)名.ribbon.NFLoadBalancerRuleClassName=策略實(shí)現(xiàn)類
service4:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule    #隨機(jī)
    #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.BestAvailableRule           #并發(fā)最少
    #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.WeightedResponseTimeRule    #請(qǐng)求時(shí)間權(quán)重

重試機(jī)制

  • 重試機(jī)制:服務(wù)B訪問集群環(huán)境下的服務(wù)A,某一個(gè)服務(wù)A宕機(jī),服務(wù)B將嘗試訪問其他可以使用的服務(wù)A。

    • 9090訪問 8081和8082

    • 如果8082宕機(jī)了

    • 9090將嘗試訪問8081

  • 步驟一:修改pom文件,添加重試retry依賴

<!--重試機(jī)制-->
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
  • 步驟二:修改yml文件,開啟cloud重試機(jī)制

spring:
  cloud:
    loadbalancer:
      retry:
        enabled: true   #開啟重試機(jī)制
  • 步驟三:修改yml文件,配置當(dāng)前服務(wù)的重試參數(shù)

service4:
  ribbon:
    ConnectTimeout: 250               # Ribbon的連接超時(shí)時(shí)間
    ReadTimeout: 1000                 # Ribbon的數(shù)據(jù)讀取超時(shí)時(shí)間
    OkToRetryOnAllOperations: true    # 是否對(duì)所有操作都進(jìn)行重試
    MaxAutoRetriesNextServer: 1       # 切換實(shí)例的重試次數(shù)
    MaxAutoRetries: 1                 # 對(duì)當(dāng)前實(shí)例的重試次數(shù)

Hystix熔斷器

Hystrix是Netflix開源的一個(gè)延遲和容錯(cuò)庫,用于隔離訪問遠(yuǎn)程服務(wù)、第三方庫,防止出現(xiàn)級(jí)聯(lián)失敗。

怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器

Hystrix 入門

  • 步驟一:修改pom,添加熔斷器依賴

  • 步驟二:修改啟動(dòng)類,添加開啟熔斷器注解 @EnableHystrix

  • 步驟三:改造dao,遠(yuǎn)程調(diào)用添加 熔斷器的備選方案,添加注解 + 備用方法

  • 步驟四:改變服務(wù)提供方法,添加線程sleep,0~2000隨機(jī) (測(cè)試方便)

  • 步驟一:修改pom,添加熔斷器依賴

<!--熔斷器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器

  • 步驟二:修改啟動(dòng)類,添加開啟熔斷器注解 @EnableHystrix

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;


@SpringBootApplication
@EnableEurekaClient
@EnableHystrix      //開啟熔斷器
public class Client4Application {
    public static void main(String[] args) {
        SpringApplication.run(Client4Application.class,args);
    }
}

怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器

  • 步驟三:改造dao,遠(yuǎn)程調(diào)用添加 熔斷器的備選方案,添加注解 + 備用方法

package com.czxy.dao;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Component
public class DataDao {
    @Resource
    private RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "dataFallback")
    public ResponseEntity<String> data(){
        String url = "http://service4/test";
        return restTemplate.getForEntity(url,String.class);
    }

    /**
     * 熔斷器超時(shí)處理方法
     * @return
     */
    public ResponseEntity<String> dataFallback(){
        return ResponseEntity.ok("臨時(shí)數(shù)據(jù)");
    }
}
  • 步驟四:改變服務(wù)提供方法,添加線程sleep,0~~2000隨機(jī) (測(cè)試方便)

package com.czxy.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;


@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping
    public ResponseEntity<String> test(HttpServletRequest request) throws Exception {
        //模擬延遲
        Thread.sleep(new Random().nextInt(2000));
        return ResponseEntity.ok("測(cè)試數(shù)據(jù)" + request.getServerPort());
    }
}

怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器

  • 步驟五:優(yōu)化dao,打印耗時(shí)時(shí)間

package com.czxy.dao;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@Component
public class DataDao {
    @Resource
    private RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "dataFallback")
    public ResponseEntity<String> data(){
        //1 記錄開始時(shí)間
        long start = System.currentTimeMillis();
        //2 調(diào)用
        String url = "http://service4/test";
        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        //3 記錄結(jié)束時(shí)間
        long end = System.currentTimeMillis();
        //4 統(tǒng)計(jì)耗時(shí)
        System.out.println("耗時(shí)時(shí)間:" + (end - start));
        return entity;
    }

    /**
     * 熔斷器超時(shí)處理方法
     * @return
     */
    public ResponseEntity<String> dataFallback(){
        return ResponseEntity.ok("臨時(shí)數(shù)據(jù)");
    }
}

(面試題)如果項(xiàng)目中同時(shí)使用熔斷器和Ribbon重試機(jī)制,誰先執(zhí)行?

  • 如果時(shí)間不相同,超時(shí)時(shí)間小的,先執(zhí)行。

  • 如果時(shí)間相同,只執(zhí)行熔斷器

  • 結(jié)論:如果兩個(gè)都需要配置,重試機(jī)制的超時(shí)時(shí)間 小于 熔斷器

“怎么理解Spring Cloud的負(fù)載均衡策略+重試機(jī)制+Hystrix 熔斷器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)頁題目:怎么理解SpringCloud的負(fù)載均衡策略+重試機(jī)制+Hystrix熔斷器
標(biāo)題路徑:http://jinyejixie.com/article36/pocdsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、云服務(wù)器、Google、用戶體驗(yàn)、ChatGPT、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)
凤凰县| 上高县| 文安县| 鄱阳县| 龙州县| 德令哈市| 贡山| 华池县| 古丈县| 东辽县| 凤山县| 绍兴县| 班戈县| 昭通市| 依兰县| 水城县| 交口县| 武陟县| 苏尼特右旗| 永仁县| 通化县| 黄梅县| 镇雄县| 乃东县| 阿坝| 光泽县| 宜州市| 屏边| 西城区| 历史| 霍邱县| 大关县| 西乡县| 绥江县| 张家界市| 府谷县| 平南县| 修武县| 佳木斯市| 长治市| 宕昌县|