springcloud(4 편)springcloud 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
모니터링 jarEnableCircuitBreaker
사용
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 관련 기능 에 대해 이 주해 속성 이 비교적 많다 는 것 을 나타 낸다.다음은 그 중의 몇 가 지 를 설명 한다.HystrixCommand
은 실패프로젝트 실행,접근 가능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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
마이크로 서비스 간에feign을 통해 서로 호출되며, 크로스 서버 호출 시 이상이 발생: No route to host해결: feign이 다른 서비스를 호출하는 것도 eureka 등록센터 서비스를 획득하여 서비스가 등록된 IP에 문제가 있는지 추측하고 지정한 appId의 실례를 조회한다. eureka url/eureka/apps/{...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.