-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: circuit breaker로 날씨 API 이중화 (#213)
- Loading branch information
1 parent
e5c59fc
commit 08c1576
Showing
8 changed files
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/dnd/runus/infrastructure/weather/WeatherClientProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.dnd.runus.infrastructure.weather; | ||
|
||
import com.dnd.runus.global.constant.WeatherType; | ||
import com.dnd.runus.global.exception.BusinessException; | ||
import io.github.resilience4j.circuitbreaker.CallNotPermittedException; | ||
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Primary | ||
@Component | ||
public class WeatherClientProxy implements WeatherClient { | ||
|
||
private final WeatherClient mainWeatherClient; | ||
private final WeatherClient fallbackWeatherClient; | ||
|
||
public WeatherClientProxy( | ||
@Qualifier("openweathermapWeatherClient") WeatherClient openweathermapWeatherClient, | ||
@Qualifier("weatherApiComWeatherClient") WeatherClient weatherApiComWeatherClient) { | ||
this.mainWeatherClient = openweathermapWeatherClient; | ||
this.fallbackWeatherClient = weatherApiComWeatherClient; | ||
} | ||
|
||
@Override | ||
@CircuitBreaker(name = "weatherClient", fallbackMethod = "fallback") | ||
public WeatherInfo getWeatherInfo(double longitude, double latitude) { | ||
return mainWeatherClient.getWeatherInfo(longitude, latitude); | ||
} | ||
|
||
public WeatherInfo fallback(double longitude, double latitude, BusinessException e) { | ||
log.error("Business exception occurred. type: {}, message: {}", e.getType(), e.getMessage()); | ||
return fallbackWeatherClient.getWeatherInfo(longitude, latitude); | ||
} | ||
|
||
public WeatherInfo fallback(double longitude, double latitude, CallNotPermittedException e) { | ||
log.error("Circuit breaker is open. {}", e.getMessage()); | ||
return fallbackWeatherClient.getWeatherInfo(longitude, latitude); | ||
} | ||
|
||
public WeatherInfo fallback(Exception e) { | ||
log.error("Fallback occurred. {}", e.getMessage()); | ||
return new WeatherInfo(WeatherType.CLOUDY, 0, 0, 0, 0, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters