풀소스는 여기 에서 확인할 수 있다. circuit open 실습은 전 소스 + 아주 조금 수정으로 간단히 해볼 수 있다.
1) 우선 @EnableHystrix -> @EnableCircuitBreaker 로 바꾼다.
1
2
3
@EnableCircuitBreaker //circuit breaker
@Service
public class searchService {
2) circuit breaker 지정할 메소드에 에러통계를 낼 묶음인 commandkey , 각종 세팅 적는다.
1
2
3
4
5
@HystrixCommand(fallbackMethod = "fallback" , commandKey = "askLastTm",commandProperties = {
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000") })
public String askIsOver(String apiAddr, String strsch) throws Exception {
여기서는 솔직히 한 메소드 가지고 실패내고 오픈되는것만 볼꺼니까 커맨드키 지정없어도 잘되지만, 그냥 지정했다.
그리고 HystrixProperty 는 굉장히 여러개 있는데, 여기서 정의한 것들은 requestVolumeThreshold, sleepWindowInMilliseconds이다.
requestVolumeThreshold : circuitbreaker 작동위한 최소 요청 갯수. 10초에 20개가 기본이라 5개로 줄였다.
sleepWindowInMilliseconds : circuit open 지속시간이다. 기본이 5초 여서 10초로 늘렸다.
여기서 @HystrixProperty 어노테이션이 안 먹히는 문제가 있었는데, 이건 autoimport 가 안되고 링크 에서 본대로 수동으로 import 시켜줘야했다.
이렇게 기본 세팅을 하고 간단하게 일부러 오류를 발생시켜서 circuit 을 오픈시키기 위해 소스 수정을 조금 했다.
1
2
3
4
5
@GetMapping("/start/{apiAddr}/{strsch}")
@ResponseBody
public String searchDest(@PathVariable String apiAddr, @PathVariable String strsch) throws Exception {
return searchService.askIsOver(apiAddr,strsch); //getBusRouteList/153 이런꼴
}
api 명을 틀리게 한다던지, 검색 값을 없앤다던지 해서 오류를 유발하고자 get 으로 요청할때 uri 에 값을 명시하도록 했다.
1
2
3
4
5
@GetMapping("/start/{apiAddr}/{strsch}")
@ResponseBody
public String searchDest(@PathVariable String apiAddr, @PathVariable String strsch) throws Exception {
return searchService.askIsOver(apiAddr,strsch); //getBusRouteList/153 이런꼴
}
이걸 통해 postman으로 계속 틀리게 쏜다음에 제대로 쏴도 fallback으로 바로 빠지는지 확인해보았다. 정상적으로 쏴도 circuit open 후 이기에 실행도 되지않고 바로 fallback 실행하는 모습이다.
여기까지는 아주 간단히 확인할 수 있다 :)