Spring Cloud實戰(五)-Spring Cloud Netflix Hystrix

Spring Cloud實戰(五)-Spring Cloud Netflix Hystrix

概要

  • 什麼是Spring Cloud Netflix Hystrix?react

  • 怎麼用 Spring Cloud Netflix Hystrix?spring

  • Hystrix常見配置介紹服務器

什麼是Spring Cloud Netflix Hystrix?

Spring Cloud Netflix Hystrix是分佈式系統處理超時和錯誤的機制,以下圖所示,分佈式系統中某個用戶請求依賴A,H,I,P服務.
圖片描述
當此請求併發超過50的時候,服務I處理速度變慢,可是服務I仍是被調用.
圖片描述
圖片描述
大量請求會阻塞在Tomcat服務器上,影響其它整個服務.在複雜的分佈式架構的應用程序有不少的依賴,都會不可避免地在某些時候失敗.高併發的依賴失敗時若是沒有隔離措施,當前應用服務就有被拖垮的風險.
Spring Cloud Netflix Hystrix就是隔離措施的一種實現,能夠設置在某種超時或者失敗情形下斷開依賴調用或者返回指定邏輯,從而提升分佈式系統的穩定性.架構

怎麼用Spring Cloud Netflix Hystrix?

百說不如一run,構造一個例子來實現,例子要實現使用Hystrix的Fallback(失敗後)邏輯來處理錯誤情景
1.Eureka Server,和Eureka Client跟以前同樣,只需改造Eureka Sentence
2.Eureka Sentence 添加pom依賴併發

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

3.Eureka Sentence的Application添加hystrix註解dom

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class ApplicationSentence {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationSentence.class, args);
    }
}

4.Eureka Sentence WordServiceImpl在subject,adjective,noun上添加HystrixCommand註解並實現相應的fallback邏輯.異步

@Service
public class WordServiceImpl implements WordService {

    @Autowired
    VerbClient verbClient;
    @Autowired
    SubjectClient subjectClient;
    @Autowired
    ArticleClient articleClient;
    @Autowired
    AdjectiveClient adjectiveClient;
    @Autowired
    NounClient nounClient;
   

    @Override
    @HystrixCommand(fallbackMethod="getFallbackSubject")
    public String getSubject() {
        return subjectClient.getWord();
    }



    @Override
    public String getVerb() {
        return verbClient.getWord();
    }

    @Override
    public String getArticle() {
        return articleClient.getWord();
    }

    @Override
    @HystrixCommand(fallbackMethod="getFallbackAdjective")
    public String getAdjective() {
        return adjectiveClient.getWord();
    }

 
    @HystrixCommand(fallbackMethod="getFallbackNoun")
    public String getNoun() {
        return nounClient.getWord();
    }

    public String getFallbackSubject() {
        return "某人";
    }

    public String getFallbackAdjective() {
        return "";
    }

    public String getFallbackNoun() {
        return "某物";
    }
}

5.依次啓動Eureka Server,Eureka Client,Eureka Sentence,這個跟以前同樣造句,停掉subject,noun服務,能夠看到其能夠走fallback邏輯.
6.使用異步和reactive形式的Hystrix,修改getSubject()爲Future,修改getNoun()爲Observable.SentenceServiceImpl也作相應的修改.分佈式

@Service
public class WordServiceImpl implements WordService {

    @Autowired
    VerbClient verbClient;
    @Autowired
    SubjectClient subjectClient;
    @Autowired
    ArticleClient articleClient;
    @Autowired
    AdjectiveClient adjectiveClient;
    @Autowired
    NounClient nounClient;
    @Override
    @HystrixCommand(fallbackMethod = "getFallbackSubject")
    public Future<String> getSubject() {
        return new AsyncResult<String>() {
            @Override
            public String invoke() {
                return subjectClient.getWord();
            }
        };
    }

//    @Override
//    @HystrixCommand(fallbackMethod="getFallbackSubject")
//    public String getSubject() {
//        return subjectClient.getWord();
//    }



    @Override
    public String getVerb() {
        return verbClient.getWord();
    }

    @Override
    public String getArticle() {
        return articleClient.getWord();
    }

    @Override
    @HystrixCommand(fallbackMethod="getFallbackAdjective")
    public String getAdjective() {
        return adjectiveClient.getWord();
    }

    @Override
    @HystrixCommand(fallbackMethod="getFallbackNoun")
    public Observable<String> getNoun() {
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> observer) {
                try {
                    if (!observer.isUnsubscribed()) {
                        observer.onNext(nounClient.getWord());
                        observer.onCompleted();
                    }
                } catch (Exception e) {
                    observer.onError(e);
                }
            }
        });
    }
//    @HystrixCommand(fallbackMethod="getFallbackNoun")
//    public String getNoun() {
//        return nounClient.getWord();
//    }

    public String getFallbackSubject() {
        return "某人";
    }

    public String getFallbackAdjective() {
        return "";
    }

    public String getFallbackNoun() {
        return "某物";
    }
}

SentenceServiceImpl修改爲下面ide

@Service
public class SentenceServiceImpl implements SentenceService {

    @Autowired WordService wordService;


    /**
     * Assemble a sentence by gathering random words of each part of speech:
     */
    public String buildSentence() {

        final List<String> nounList = Lists.newArrayList();
        wordService.getNoun().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                nounList.add(s);
            }
        });

        try {
            return String.format("%s %s %s %s %s.", wordService.getSubject().get(), wordService.getVerb(),
                    wordService.getArticle(), wordService.getAdjective(), nounList.get(0));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return "error";
    }

}

7.查看Hystrix dashboard能夠看到方法調用的統計信息
訪問http://127.0.0.1:8020/hystrix並輸入http://127.0.0.1:8020/hystrix...能夠查看Hystrix接口調用信息
圖片描述高併發

相關文章
相關標籤/搜索