国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springcloud3 hystrix實現(xiàn)服務降級的案例配置2

這篇具有很好參考價值的文章主要介紹了springcloud3 hystrix實現(xiàn)服務降級的案例配置2。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一 服務降級的說明

1.1 服務降級說明

"服務器忙,請稍后在試"不讓客戶達等待,立即返回一個友好的提示。

1.2 服務降級的觸發(fā)情況

1.程序運行異常;

2.超時;

3.服務熔斷觸發(fā)服務降級;

4.線程池/信號量打滿也會導致服務降級

1.3 通用注解

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

二 案例:對每一個方法實行降級處理

2.1 消費端

2.1.1 pom文件

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

2.1.2 設置降級規(guī)則

代碼

 @GetMapping(value = "/consumer/payment/nacos/{id}")
    @HystrixCommand(fallbackMethod = "dealFallBackInfo",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
    public String paymentInfo(@PathVariable("id") Long id)
    {
       System.out.println("獲取服務器配置信息serverUrl:"+serverURL);
       System.out.println("9008的controller獲取id:"+id);
       String str=orderConsumerService.getPaymentByIdLjf22222(id);
       return "ok:"+serverURL+""+str;
    }
    @GetMapping(value = "/consumer/getinfo/{id}")
    public Object getUserInfo(@PathVariable("id") Long id)
    {
        User u=new User(id.intValue(),"beijing"+id);
       return  orderConsumerService.postQueryParams(u);
    }
    public String dealFallBackInfo(@PathVariable("id") Long id)
    {
        return "我是消費者9008,服務出錯了,進行服務降級"+id;
    }

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?2.1.3 開啟hystrix熔斷

添加:@EnableHystrix 注解

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?2.2?服務端

2.2.1 pom文件

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

2.2.2?設置降級規(guī)則

1.代碼

    @GetMapping(value = "/ljf/getinfo/{id}")
    @HystrixCommand(fallbackMethod = "dealFallBackInfo",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
    })
    public String getPayment(@PathVariable("id") Integer id)
    {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int k=10/0;
        System.out.println("================服務9009 獲取到的參數(shù)id:"+id);
        return "服務9009 獲取到的參數(shù)id:"+id;
    }

    @PostMapping("/path")
   public  String postQueryParams(@RequestBody User user) throws JsonProcessingException {

       String str=  new JsonMapper().writeValueAsString(user);
       System.out.println("post提交....");
       return str;
    }
    public String dealFallBackInfo(@PathVariable("id") Long id)
    {
        return "我是消費者9009,服務出錯了,進行服務降級"+id;
    }

2.截圖

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?2.2.3 開啟hystrix熔斷

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

??2.3?啟動服務測試

1.啟動nacos,啟動sleuth

2.啟動consumer9008? ?provider9009

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?3.測試

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

三 案例:設置全局降級處理辦法

3.1 消費端設置

1.代碼

package com.ljf.mscloud.controller;

import com.ljf.mscloud.model.User;
import com.ljf.mscloud.service.OrderConsumerService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: OrderConsumerController
 * @Description: TODO
 * @Author: admin
 * @Date: 2023/08/14?18:09:14?
 * @Version: V1.0
 **/
@RestController
@Slf4j
@RefreshScope //支持Nacos的動態(tài)刷新功能。
//


@DefaultProperties(defaultFallback = "globalFallBackInfo")
public class OrderConsumerController {
    @Value("${service-url.nacos-user-service}")
   private String serverURL;
    @Autowired
    private OrderConsumerService orderConsumerService;


    @GetMapping(value = "/consumer/payment/nacos/{id}")
    @HystrixCommand(fallbackMethod = "dealFallBackInfo",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })
    public String paymentInfo(@PathVariable("id") Long id)
    {
       System.out.println("獲取服務器配置信息serverUrl:"+serverURL);
       System.out.println("9008的controller獲取id:"+id);
       String str=orderConsumerService.getPaymentByIdLjf22222(id);
       return "ok:"+serverURL+""+str;
    }
    @GetMapping(value = "/consumer/getinfo/{id}")
    public Object getUserInfo(@PathVariable("id") Long id)
    {
        User u=new User(id.intValue(),"beijing"+id);
       return  orderConsumerService.postQueryParams(u);
    }
    public String dealFallBackInfo(@PathVariable("id") Long id)
    {
        return "我是消費者9008,服務出錯了,進行服務降級"+id;
    }

    public String globalFallBackInfo()
    {

        return "Global異常處理信息,請稍后再試,客戶端9008";
    }
}

2.截圖

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?原因在沒有在制定方法加:@HystrixCommand? 那么加上此注解后:

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?再次訪問:http://localhost:9008/consumer/getinfo/666

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

四 案例:給Feginclient注解的接口上添加降級規(guī)則

4.1 controller

package com.ljf.mscloud.controller;

import com.ljf.mscloud.model.User;
import com.ljf.mscloud.service.OrderConsumerService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: OrderConsumerController
 * @Description: TODO
 * @Author: admin
 * @Date: 2023/08/14?18:09:14?
 * @Version: V1.0
 **/
@RestController
@Slf4j
@RefreshScope //支持Nacos的動態(tài)刷新功能。
//
//@DefaultProperties(defaultFallback = "globalFallBackInfo")
public class OrderConsumerController {
    @Value("${service-url.nacos-user-service}")
   private String serverURL;
    @Autowired
    private OrderConsumerService orderConsumerService;


    @GetMapping(value = "/consumer/payment/nacos/{id}")
 //   @HystrixCommand(fallbackMethod = "dealFallBackInfo",commandProperties = {
    //        @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
   // })
    public String paymentInfo(@PathVariable("id") Long id)
    {
       System.out.println("獲取服務器配置信息serverUrl:"+serverURL);
       System.out.println("9008的controller獲取id:"+id);
       String str=orderConsumerService.getPaymentByIdLjf22222(id);
       return "ok:"+serverURL+""+str;
    }
    @GetMapping(value = "/consumer/getinfo/{id}")
   // @HystrixCommand
    public Object getUserInfo(@PathVariable("id") Long id)
    {
        User u=new User(id.intValue(),"beijing"+id);
      //  int age = 10/0;
       return  orderConsumerService.postQueryParams(u);
    }
    public String dealFallBackInfo(@PathVariable("id") Long id)
    {

        return "我是消費者9008,服務出錯了,進行服務降級"+id;
    }

    public String globalFallBackInfo()
    {

        return "Global異常處理信息,請稍后再試,客戶端9008";
    }
}

4.2 service

package com.ljf.mscloud.service;


import com.ljf.mscloud.model.User;
import feign.Headers;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@Component
@FeignClient(value = "mscloud-fegin-nacos-hystrix-provider9009",fallback = PaymentFallbackService.class)
public interface OrderConsumerService {
    @GetMapping(value = "/ljf/getinfo/{yyid}") //相當于:
    public String getPaymentByIdLjf22222(@PathVariable("yyid") Long ssid);
   // @Headers({"Content-Type: application/json"})
   // @PostMapping("/path")
    @PostMapping(value = "/path",consumes ="application/json")
    String postQueryParams(@RequestBody User user);
}

4.3 fallback實現(xiàn)類

@Component
public class PaymentFallbackService  implements OrderConsumerService{
    @Override
    public String getPaymentByIdLjf22222(Long ssid) {
        return "getPaymentByIdLjf22222方法出現(xiàn)問題開始降級";
    }

    @Override
    public String postQueryParams(User user) {
        return "postQueryParams方法出現(xiàn)問題開始降級";
    }
}

?4.4 配置文件

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

?4.5 測試

1.故意只啟動 consuemr9008,不啟動provider9009 模擬宕機的情況

2.測試

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud

springcloud3 hystrix實現(xiàn)服務降級的案例配置2,springcloud3,spring cloud文章來源地址http://www.zghlxwxcb.cn/news/detail-653820.html

到了這里,關(guān)于springcloud3 hystrix實現(xiàn)服務降級的案例配置2的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 微服務:Springboot集成Hystrix實現(xiàn)熔斷、降級、隔離

    微服務:Springboot集成Hystrix實現(xiàn)熔斷、降級、隔離

    在分布式微服務的項目中,常常會有多個服務復用,產(chǎn)生多個服務調(diào)用的情況。比如A服務調(diào)用B服務,B服務調(diào)用C服務。服務調(diào)用鏈路長了必然會增加服務超時的概率,服務的超時阻塞會一直占用線程資源,大量的阻塞會直接消耗完服務線程,嚴重情況下會導致服務直接宕機從

    2024年02月12日
    瀏覽(21)
  • 【微服務筆記10】微服務組件之Hystrix實現(xiàn)服務降級和服務熔斷

    【微服務筆記10】微服務組件之Hystrix實現(xiàn)服務降級和服務熔斷

    這篇文章,主要介紹微服務組件之Hystrix實現(xiàn)服務降級和服務熔斷。 目錄 一、服務降級 1.1、什么是服務降級 1.2、實現(xiàn)服務降級 (1)引入依賴 (2)編寫Service層代碼 (3)編寫Controller層代碼 (4)運行測試 (5)fallbackMethod屬性 二、服務熔斷 2.1、什么是服務熔斷 2.2、實現(xiàn)服務

    2023年04月11日
    瀏覽(22)
  • OpenFegin+hystrix實現(xiàn)遠程HTTP服務調(diào)用、服務降級(深度解析~保姆級)

    OpenFegin+hystrix實現(xiàn)遠程HTTP服務調(diào)用、服務降級(深度解析~保姆級)

    OpenFeign 是 Spring Cloud 家族的一個成員, 它最核心的作用是為 HTTP 形式的 Rest API 提供了非常簡潔高效的 RPC 調(diào)用方式。支持Hystrix 、Ribbon和SpringMVC 注解。 Feign和OpenFeign的區(qū)別? 1、Feign: Feign是Netflix公司(第一代SpringCloud)研發(fā)的一個輕量級RESTful的偽HTTP服務客戶端。 Feign內(nèi)置了

    2023年04月08日
    瀏覽(23)
  • springboot整合feign實現(xiàn)RPC調(diào)用,并通過Hystrix實現(xiàn)服務降級

    springboot整合feign實現(xiàn)RPC調(diào)用,并通過Hystrix實現(xiàn)服務降級

    feign/openfeign和dubbo是常用的微服務RPC框架,由于feigin內(nèi)部已經(jīng)集成ribbon,自帶了負載均衡的功能,當有多個同名的服務注冊到注冊中心時,會根據(jù)ribbon默認的負載均衡算法將請求分配到不同的服務。這篇文章就簡單介紹一下怎么使用feign來調(diào)用遠程的服務。 首先,需要有一個

    2024年02月16日
    瀏覽(28)
  • springcloud3 bus+springconfig 實現(xiàn)配置文件的動態(tài)刷新(了解)

    springcloud3 bus+springconfig 實現(xiàn)配置文件的動態(tài)刷新(了解)

    spring cloud bus是用來將分布式系統(tǒng)的節(jié) 點與輕量級消息系統(tǒng)鏈接起來的框架。 它整合了java的事件處理機制和消息中間件的功能。其中目前支持RabbitMQ和kafka 簡介: bus實現(xiàn)多個服務的配置文件動態(tài)刷新。 1.2.1 rabbitmq的配置 ?1.2.2 工程結(jié)構(gòu) ?1.2.2 實現(xiàn)方式 1.利用消息縱向觸發(fā)一個

    2024年02月13日
    瀏覽(17)
  • springcloud3 GateWay動態(tài)路由的案例操作

    springcloud3 GateWay動態(tài)路由的案例操作

    gateway相當于所有服務的門戶,將客戶端請求與服務端應用相分離,客戶端請求通過gateway后由定義的路由和斷言進行轉(zhuǎn)發(fā),路由代表需要轉(zhuǎn)發(fā)請求的地址,斷言相當于請求這些地址時所滿足的條件,只有同時符合路由和斷言才給予轉(zhuǎn)發(fā) gateWay是微服務的API網(wǎng)關(guān),能夠?qū)崿F(xiàn)服務的

    2024年02月13日
    瀏覽(21)
  • Hystrix入門使用 服務熔斷 服務降級 服務雪崩

    Hystrix入門使用 服務熔斷 服務降級 服務雪崩

    hystrix停止更新,理念優(yōu)秀。 分布式系統(tǒng)面臨的問題: 對于復雜的分布式體系,有數(shù)十個依賴,依賴不可避免的錯誤。 服務會出現(xiàn)雪崩, 高可用受到破壞 。 Hystrix就是用于解決分布式系統(tǒng)延遲和容錯的開源庫。 保證在一個依賴出現(xiàn)問題,不會導致整體的服務失敗,避免級聯(lián)故

    2024年02月07日
    瀏覽(31)
  • Spring Cloud 容錯機試 Hystrix 服務降級 RestTemplate:

    Spring Cloud 容錯機試 Hystrix 服務降級 RestTemplate:

    雪崩效應: ? 如果短信服務炸了后面的所有服務就會起連鎖反應造成全部服務掛掉 , 這就是雪崩效應 , 那么其實短信服務又不是我們主要業(yè)務 , 這個時候我們可以采用服務降級 , 服務降級就是暫時的把短信服務停掉能用就返回不能用就返回個錯誤 , 但是它也不會影響

    2024年02月07日
    瀏覽(28)
  • 云原生微服務 Spring Cloud Hystrix 降級、熔斷實戰(zhàn)應用

    云原生微服務 Spring Cloud Hystrix 降級、熔斷實戰(zhàn)應用

    第一章 Java線程池技術(shù)應用 第二章 CountDownLatch和Semaphone的應用 第三章 Spring Cloud 簡介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon 第六章 Spring Cloud 之 OpenFeign 第七章 Spring Cloud 之 GateWay 第八章 Spring Cloud Netflix 之 Hystrix 多個微服務之間調(diào)用的時候,假如微服

    2024年02月08日
    瀏覽(29)
  • (一)Spring Cloud 直擊微服務作用、架構(gòu)應用、hystrix降級

    (一)Spring Cloud 直擊微服務作用、架構(gòu)應用、hystrix降級

    直擊微服務作用 ?? ?遇到了什么問題? ? ? ? ? 將單體架構(gòu)拆分成微服務架構(gòu)后,如果保證多個服務(項目)正常運行? ? ? 哪個技術(shù)可以解決這個問題? ? ? ? ? 微服務技術(shù) ? ? ? ? 服務治理: 服務管理,維護服務與服務之間的關(guān)系 ? ? 這個技術(shù)如何使用? ? ? ? ? netflix/網(wǎng)飛:

    2024年02月03日
    瀏覽(115)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包