springcloud(4 편)springcloud hystrix

spring cloud hystrix
간단 한 소개
hystrix      、  (       )、                 。

netflix hystrix
이 편 에 서 는 주로 spring cloud 가 Hstrix 에 대한 통합 을 설명 하 는데,어떻게 Hstrix 를 단독으로 사용 하 는 지 에 대해 서 는 제 가 공유 한 pdf 를 참고 할 수 있 습 니 다.
spring cloud hystrix
도입 의존
  <!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

        <!--hystrix-dashboard   -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
spring-cloud-starter-hystrix핵심 jarspring-cloud-starter-hystrix-dashboard모니터링 jar
EnableCircuitBreaker
사용EnableCircuitBreaker또는EnableHystrix공정 이 Hstrix 를 사용 하 는 것 을 나타 내 고 두 주 해 는 등가 입 니 다.Spring boot
package com.lkl.springcloud.hystrix;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/** * Created by liaokailin on 16/5/1. */
@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrixDashboard
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}

그 중에서Application.java주 해 는 Hstrix 에 대한 감 시 를 시작 하고 나중에 사용 할 것 임 을 나타 낸다.
그 다음 에 3 자 의존 서 비 스 를 호출 하 는 것 을 모 의 했다.EnableHystrixDashboard -> controller -> service dependency service
package com.lkl.springcloud.hystrix.controller;

import com.lkl.springcloud.hystrix.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/** *           * Created by liaokailin on 16/5/1. */
@RestController
public class HystrixController {

    @Autowired
    private HystrixService service;
    /** *         */
    @RequestMapping("/call")
    public String callDependencyService(){
        return service.callDependencyService();
    }
}

controller
package com.lkl.springcloud.hystrix.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** *      * Created by liaokailin on 16/5/1. */
@Service
public class HystrixService {

    @Autowired
    private CallDependencyService dependencyService;
    public String callDependencyService() {
        return dependencyService.mockGetUserInfo();
    }
}
service
package com.lkl.springcloud.hystrix.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Component;

import java.util.Random;

/** *       ,  hystrix       * Created by liaokailin on 16/5/1. */
@Component
public class CallDependencyService {

    private Random random = new Random();
    /** *         (      ) * @return */
    @HystrixCommand(fallbackMethod = "fallback")
    public String mockGetUserInfo(){
        int randomInt= random.nextInt(10) ;
        if(randomInt<8){  //        
            throw new RuntimeException("call dependency service fail.");
        }else{
            return "UserName:liaokailin;number:"+randomInt;
        }
    }

    public String fallback(){
        return "some exception occur call fallback method.";
    }
}
dependency service이 방법 은 hystrix 패키지 로 의존 서비스 에 대해 격 리,강등,빠 른 실패,빠 른 재 시도 등 hystrix 관련 기능 에 대해 이 주해 속성 이 비교적 많다 는 것 을 나타 낸다.다음은 그 중의 몇 가 지 를 설명 한다.
  • fallback Method 강등 방법
  • commandProperties 일반 설정 속성,Hystrixcand 대응 속성 을 설정 할 수 있 습 니 다.예 를 들 어 스 레 드 탱크 또는 신 호 량 격 리,퓨즈 퓨즈 퓨즈 규칙 등
  • ignoreExceptions 가 무시 한 이상,기본 값HystrixCommand은 실패
  • 를 계산 하지 않 습 니 다.
  • groupkey()그룹 이름,기본 사용 클래스 이름
  • commandKey 명령 이름,기본 사용 방법 이름
  • Dashboard
    프로젝트 실행,접근 가능http://localhost:9090/hystrix.stream dashboard 정 보 를 가 져 옵 니 다.기본 값 으로 최대HystrixBadRequestException개의 단말 기 를 열 어 모니터링 정 보 를 가 져 옵 니 다.5매개 변 수 를 추가 하여 모니터링 데 이 터 를 가 져 오 는 간격 을 지정 할 수 있 습 니 다.
    Hstrix.stream 에 직접 방문 하 는 것 은 현명 하지 못 할 것 입 니 다.공식 적 으로 Hstrix-dashboard-\#.\#.\#.war 가방 을 모니터링 하고 다운로드 한 후에 tomcat 에 넣 으 면 다음 과 같은 화면 을 얻 을 수 있 습 니 다.
    입력http://localhost:9090/hystrix.stream 클릭delay대시 보드 인터페이스 진입
    방문 하 다.http://localhost:/9090/call Dashboard 진입 변화 관찰
    ok ~ it’s work ! more about is here

    좋은 웹페이지 즐겨찾기