Springcloud sentinel 설치 및 사용 방법 분석

아 리 버 전의 Hstrix 로 서 sentinel 은 설정 방식 을 간소화 하고 시각 화 된 인터페이스 사이트 와 편리 한 설정 방식 을 제공 하여 실제 적 인 사용 방식 에 더욱 부합 되 고 각종 장점 으로 인해 sentinel 은 서비스 강등 용 단 흐름 제어 등 가장 좋 은 선택 이 되 었 다.
1.설치 활성화
https://github.com/alibaba/Sentinel/releases
홈 페이지 에서 적합 한 버 전 을 선택 하여 다운로드 합 니 다.본 체 는 jar 파일 입 니 다.
java -jar sentinel-dashboard-1.7.2.jar
다운로드 한 패 키 지 는 jar 이기 때문에 cmd 에서 직접 시작 하면 됩 니 다.
로그 인 주 소 는 기본적으로 localhost:8080 입 니 다.계 정 비밀 번 호 는 모두 sentinel 입 니 다.처음으로 인터페이스 에 들 어가 면 공백 이 되 고 응용 프로그램 에 한 번 방문 해 야 인터페이스 에 해당 하 는 응용 프로그램의 정보 가 나타 납 니 다.

2.응용 프로그램 연결 sentinel 설정.
1)pom 파일 설정

<dependencies>
    <!-- SpringCloud ailibaba nacos-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- SpringCloud ailibaba sentinel-datasource-nacos        -->
    <dependency>
      <groupId>com.alibaba.csp</groupId>
      <artifactId>sentinel-datasource-nacos</artifactId>
    </dependency>
    <!-- SpringCloud ailibaba sentinel-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <dependency>
      <groupId>com.bai</groupId>
      <artifactId>cloud-api-common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--  -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--   -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
2)yml 설정

server:
 port: 8401

spring:
 application:
  name: cloudalibaba-sentinal-service
 cloud:
  nacos:
   discovery:
    #Nacos        
    server-addr: localhost:8848
  sentinel:
   transport:
    #  Sentin dashboard  
    dashboard: localhost:8080
    #   8719  ,          8719  +1    ,            
    port: 8719
   datasource:  #         
    ds1:
     nacos:
      server-addr: localhost:8848
      dataId: cloudalibaba-sentinel-service
      groupId: DEFAULT_GROUP
      data-type: json
      rule-type: flow
management:
 endpoints:
  web:
   exposure:
    include: '*'
3)시작 클래스 일반 설정

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {
  public static void main(String[] args) {
    SpringApplication.run(SentinelMain8401.class,args);
  }
}
4)컨트롤 러 테스트

@RestController
@Slf4j
public class FlowLimitController {

  @GetMapping(value = "/testA")
  public String testA(){
    return "******this is A";
  }
  @GetMapping(value = "/testB")
  public String testB(){
    log.info(Thread.currentThread().getName()+"\t"+"***testB");
    return "******this is B";
  }
  @GetMapping(value = "/testD")
  public String testD(){
    /* try {
      TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    log.info("this is testD RT");*/
    int a=10/0;
    log.info("this is testD     ");
    return "!!!!!TESTD";
  }@GetMapping(value = "/testE")
  public String testE(){
    int a=10/0;
    log.info("this is testE    ");
    return "!!!!!TESTE";
  }

  @GetMapping(value = "/hotkey")
  @SentinelResource(value = "hotkey",blockHandler = "hotkeyhandler")
  public String hotkey(@RequestParam(value = "p1",required = false)String p1,
             @RequestParam(value = "p2",required = false)String p2){
    return ">>>>>>hotkey is ready";
  }
  public String hotkeyhandler(String p1, String p2, BlockException exception){
    return ".....hotkey is failed o(ini)o";
  }

}
3.일반적인 사용
1)흐름 제어 규칙
홈 페이지 의 흐름 제어 규칙 을 클릭 하여 추가 규칙 을 선택 하 십시오.

자원 이름,자신 이 방문 할 방법의 주소
한도 값 형식,QPS 는 1 초 동안 통 과 된 방 문 량 을 말 하 며,스 레 드 수 는 같은 자원 에 접근 하기 위해 몇 개의 스 레 드 를 사용 합 니까?
단기 한도 값,예 를 들 어 1 을 쓰 고 QPS 를 선택 하면 1 초 에 1 번 이상 testa 를 방문 하면 데이터 통 제 를 하고 오 류 를 강제 적 으로 보고 하 며 스 레 드 가 같 습 니 다.
흐름 제어 모드 는 문턱 값 을 만나면 바로 실패 하고 기본 적 인 것 이다.관련 은 한 자원 에서 다른 자원 과 연결 할 수 있 고 자원 의 방문 량 이 한도 값 을 초과 하면 다른 자원 의 통 제 를 강제 적 으로 중단 할 수 있다.
흐름 제어 효과,빠 른 실 패 는 기본 입 니 다.warm up 은 시스템 이 갑자기 한 동안 높 은 병발 을 하 는 상황 을 피하 기 위해 한도 값 을 제한 하고 한 단락 의 성장 으로 나 누 어 최종 적 으로 한도 값 에 도달 합 니 다.줄 을 서서 기다 리 면 방 문 량 이 오고 규칙 에 따라 하나씩 통과 합 니 다.
2)강등 규칙
Hstrix 와 달리 sentinel 은 웹 페이지 를 통 해 세 가지 강등 정책 을 설정 할 수 있 습 니 다.

RT:홈 페이지 를 살 펴 보 는 것 을 권장 합 니 다.명확 한 제한 규칙 이 있 습 니 다.즉,1 초 에 5 번 이상 방문 하고 평균 방문 시간 은 4900 을 초과 해 서 는 안 됩 니 다.만약 에 상기 제한 을 초과 하면 강등 처 리 를 할 것 입 니 다.
이상 비율:일정 시간 내 에 일정 비율 에 오류 가 발생 하면 강등 이 발생 합 니 다.
이상 수:일정 시간 내 에 일정 수 에 오류 가 발생 하면 강등 이 발생 합 니 다.(위 설정 은 홈 페이지 기준 을 엄 격 히 준수 해 야 합 니 다)
3.핫 이 슈 규칙
핫 이 슈 규칙 은 방법 중의 특정한 매개 변 수 를 제한 하고 특정한 매개 변 수 를 정확하게 제어 할 수 있 으 며 지정 한 변 수 를 추가 로 제어 할 수 있다.

핫 이 슈 규칙 은 QPS 만 지원 합 니 다.sentinelresource 주석 에 맞 춰 야 합 니 다.
@SentinelResource(value = "hotkey",blockHandler = "hotkeyhandler")
Blockhandler 가 핫 이 슈 를 한정 하 는 방법 이 잘못 되 었 을 때 바닥 을 파 헤 치 는 또 다른 방법 입 니 다.오류 가 발생 했 을 때 사용 하 는 시스템 자체 페이지 를 피하 고 원 하 는 정 보 를 제어 할 수 있 습 니 다.

public String hotkeyhandler(String p1, String p2, BlockException exception){
    return ".....hotkey is failed o(ini)o";
  }
4.시스템 규칙

시스템 규칙 은 전체 응용 에 상부 제한 을 가 할 수 있 고 가장 먼저 판단 할 수 있 으 며 규정된 한도 값 을 초과 하면 강등 규칙 이 발생 하여 오류 페이지 알림 이 발생 할 수 있다.
5.인터페이스 형식 으로 바닥 을 파 헤 치 는 방법.

@GetMapping(value = "/byhander")
  @SentinelResource(value = "byhander",blockHandlerClass = Myhander.class,blockHandler = "byhander1")
  public CommonResult byhander(){
    return new CommonResult(200,"     URL",new PayMent(21L,"999"));
  }
hander 인터페이스

public class Myhander {
  public static CommonResult byhander1(BlockException exception){
    return new CommonResult(400,"     "+exception.getClass().getCanonicalName()+"   hander1");
  }
  public static CommonResult byhander2(BlockException exception){
    return new CommonResult(400,"     "+exception.getClass().getCanonicalName()+"   hander2");
  }
}
어떤 폭로 방법 을 선택 하고 잘못 보고 하면 해당 하 는 방법의 이름 을 선택한다.
이 모든 코드 는 GitHub 에 있 습 니 다.
https://github.com/MaTsukun/springcloud2020
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기