<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                2021-12-19 周天 ## Hystrix介紹 Hystrix是由Netflix開源的一個延遲和容錯庫,用于隔離訪問遠程系統、服務或者第三方庫,防止級聯失敗,從而提升系統的可用性與容錯性。 ### Hystrix特性 Hystrix主要通過以下幾點實現延遲和容錯。 * 包裹請求 - `@HystrixCommand`注解(命令模式) * 跳閘機制 - 5秒內20次失敗,一段時間內會停止訪問服務 * 資源隔離 - 給每個服務提供小型線程池,線程池滿立即拒絕請求 * 監控 * 回退機制 - 開發人員可提供默認回退邏輯 * 自我修復 - 一段時間內,會嘗試調用一次服務,請求成功則關閉斷路器 ## Hystrix 集成 ### 添加依賴 ``` xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` ### 開啟斷路器 在啟動類上增加`@EnableHystrix`注解或者`@EnableCircuitBreaker`注解。 ### 直接使用`@HystrixCommand` ``` java @FeignClient(name = "mic-card",path = "/card/admin") public interface CardApi { @PostMapping("/count/home") @HystrixCommand ResultObject<CountVo> getHome(); } ``` ### 結合Feign使用 Feign默認是不啟用Hystrix的,添加如下配置啟用。 ``` yml feign: hystrix: enabled: true ``` ``` java /** * @Description card api服務 * @Date 2021-12-04 10:12 * @Created by mango */ @FeignClient(name = "mic-card",path = "/card/admin", configuration = {FeignConfiguration.class}, fallback = CardApiFallback.class) public interface CardApi { @GetMapping("/card/rand") ResultObject<CardVo> getRand(); @PostMapping("/count/home") ResultObject<CountVo> getHome(); } ## 回退機制,自定義實現回退邏輯 @Component @Slf4j class CardApiFallback implements CardApi { @Autowired private ResultUtil resultUtil; @Override public ResultObject<CardVo> getRand() { log.info("enter card api fallback"); CardVo cardVo = new CardVo(); cardVo.setName("enter fallback"); cardVo.setIsOpen("1"); cardVo.setCreateTime(DateUtil.now()); return resultUtil.success2obj(cardVo,"success"); } @Override public ResultObject<CountVo> getHome() { log.info("enter card api get home fallback"); CountVo countVo = new CountVo(); countVo.setCardCount(1); countVo.setFileCount(1); countVo.setPointCount(1); return resultUtil.success2obj(countVo,"success"); } } ``` ## 配置相關 ### 全局啟用 ~~~ feign.hystrix.enabled: true ~~~ #### 全局禁用 ~~~ feign.hystrix.enabled: false ~~~ ### 局部啟用 ~~~ public class FeignEnableHystrixConfiguration { @Bean @Scope("prototype") public HystrixFeign.Builder feignBuilder() { return HystrixFeign.builder(); } } ~~~ ~~~ @FeignClient(name = "mic-card",path = "/card/admin", configuration = {FeignEnableHystrixConfiguration.class}, fallback = CardApiFallback.class) ~~~ ## 局部禁用 ~~~ public class FeignDisableHystrixConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } } ~~~ ## @HystrixCommand注解源碼 ``` java /** * This annotation used to specify some methods which should be processes as hystrix commands. */ @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface HystrixCommand { /** * The command group key is used for grouping together commands such as for reporting, * alerting, dashboards or team/library ownership. * <p/> * default => the runtime class name of annotated method * * @return group key */ String groupKey() default ""; /** * Hystrix command key. * <p/> * default => the name of annotated method. for example: * <code> * ... * @HystrixCommand * public User getUserById(...) * ... * the command name will be: 'getUserById' * </code> * * @return command key */ String commandKey() default ""; /** * The thread-pool key is used to represent a * HystrixThreadPool for monitoring, metrics publishing, caching and other such uses. * * @return thread pool key */ String threadPoolKey() default ""; /** * Specifies a method to process fallback logic. * A fallback method should be defined in the same class where is HystrixCommand. * Also a fallback method should have same signature to a method which was invoked as hystrix command. * for example: * <code> * @HystrixCommand(fallbackMethod = "getByIdFallback") * public String getById(String id) {...} * * private String getByIdFallback(String id) {...} * </code> * Also a fallback method can be annotated with {@link HystrixCommand} * <p/> * default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()} * * @return method name */ String fallbackMethod() default ""; /** * Specifies command properties. * * @return command properties */ HystrixProperty[] commandProperties() default {}; /** * Specifies thread pool properties. * * @return thread pool properties */ HystrixProperty[] threadPoolProperties() default {}; /** * Defines exceptions which should be ignored. * Optionally these can be wrapped in HystrixRuntimeException if raiseHystrixExceptions contains RUNTIME_EXCEPTION. * * @return exceptions to ignore */ Class<? extends Throwable>[] ignoreExceptions() default {}; /** * Specifies the mode that should be used to execute hystrix observable command. * For more information see {@link ObservableExecutionMode}. * * @return observable execution mode */ ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER; /** * When includes RUNTIME_EXCEPTION, any exceptions that are not ignored are wrapped in HystrixRuntimeException. * * @return exceptions to wrap */ HystrixException[] raiseHystrixExceptions() default {}; /** * Specifies default fallback method for the command. If both {@link #fallbackMethod} and {@link #defaultFallback} * methods are specified then specific one is used. * note: default fallback method cannot have parameters, return type should be compatible with command return type. * * @return the name of default fallback method */ String defaultFallback() default ""; } ``` ## 參考資料 * https://www.itmuch.com/spring-cloud/finchley-13/
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看