[๋์์ธํจํด์ ์๋ฆ๋ค์] 3์ฅ. ์ค๊ณ ์์น : ๋จ์ผ์ฑ ์์์น, ๊ฐ๋ฐฉํ์ ์์น
ํด๋์ค์ ๋ชจ๋์ ํ๋์ ์ฑ ์ ๋๋ ๊ธฐ๋ฅ๋ง์ ๊ฐ์ง๊ณ ์์ด์ผ ํ๋ค
์ผ๋จ ์์ฑํ๊ณ , ์ฝ๋๊ฐ ๋ณต์กํด์ง์ ๋ฐ๋ผ ๊ฑฐ๋ํด์ง ํด๋์ค๋ฅผ ์ฌ๋ฌ ๊ฐ์ ์ธ๋ถํ๋ ํด๋์ค๋ก ๋๋์
1. ํด๋์ค์ ์ฝ๋, ํจ์ ๋๋ ์์ฑ์ด ๋๋ฌด ๋ง์ ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ ์ง ๋ณด์์ฑ์ ์ํฅ์ ๋ฏธ์น๋ ๊ฒฝ์ฐ ํด๋์ค ๋ถํ ์ ๊ณ ๋ คํ์
2. ํด๋์ค๊ฐ ๋๋ฌด ๊ณผํ๊ฒ ๋ค๋ฅธ ํด๋์ค์ ์์กดํ๋ค๋ฉด, ๋์ ์์ง๋์ ๋ฎ์ ๊ฒฐํฉ๋์ ์ฝ๋ ์ค๊ณ ์ฌ์์ ๋ถํฉํ์ง ์์ผ๋ฏ๋ก ํด๋์ค ๋ถํ ์ ๊ณ ๋ คํ์
3. ํด๋์ค์ private ๋ฉ์๋๊ฐ ๋๋ฌด ๋ง์ ๊ฒฝ์ฐ ์๋ก์ด ํด๋์ค๋ก ๋ถ๋ฆฌํ๊ณ ๋ ๋ง์ ํด๋์ค์์ ์ฌ์ฉํ ์ ์๋๋ก public ๋ฉ์๋๋ก ์ค์ ํ์ฌ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ํฅ์์ํค์
4. ํด๋์ค์ ์ด๋ฆ์ ๋น์ฆ๋์ค์ ์ผ๋ก ์ ํํ๊ฒ ์ง์ ํ๊ธฐ ์ด๋ ต๊ฑฐ๋ Manager, Context์ฒ๋ผ ์ผ๋ฐ์ ์ธ ๋จ์ด๊ฐ ์๋๋ฉด ํด๋์ค์ ์ด๋ฆ์ ์ ์ํ๊ธฐ ์ด๋ ค์ธ ๊ฒฝ์ฐ, ํด๋์ค ์ฑ ์ ์ ์๊ฐ ์ถฉ๋ถํ ๋ช ํํ์ง ์์์ ์๋ฏธํ๋ค
5. UserInfo ํด๋์ค์ ๋ง์ ๋ฉ์๋๊ฐ ์ฃผ์๋ฅผ ์ํด์๋ง ๊ตฌํ๋ ์์ ์์์ฒ๋ผ ํด๋์ค์ ๋ง์ ๋ฉ์๋๊ฐ ์ฌ๋ฌ ์์ฑ ์ค ์ผ๋ถ์์๋ง ์๋ํ๋ ๊ฒฝ์ฐ ์ด๋ฌํ ์์ฑ๊ณผ ํด๋น ๋ฉ์๋๋ฅผ ๋ถํ ํ๋ ๊ฒ์ ๊ณ ๋ คํ์
ํด๋์ค ๋ถํ ๋ก ์ธํด ์ฝ๋์ ์ ์ง ๋ณด์์ฑ์ด ๊ทนํ ๋ฎ์์ง ์ ์๋ค
ํ์ฅํ ๋๋ ๊ฐ๋ฐฉ, ์์ ํ ๋๋ ํ์
ํด๋น ๋ชจ๋๋ค์ด ์ด๋ป๊ฒ ์ฌ์ฉ๋ ์ง, ์ด๋ค ์๊ตฌ ์ฌํญ์ด ์์์ง ์ดํดํ๋ ๊ฒ์ด ํ์์
๋จ๊ธฐ๊ฐ ๋ด์ ์งํํ ์ ์๋ ํ์ฅ, ์ฝ๋ ๊ตฌ์กฐ ๋ณ๊ฒฝ์ ๋ฏธ์น๋ ์ํฅ์ด ๋น๊ต์ ํฐ ํ์ฅ, ๊ตฌํ ๋น์ฉ์ด ๋ง์ด ๋ค์ง ์๋ ํ์ฅ์ ๋ํด ํ์ฅ ํฌ์ธํธ๋ฅผ ์ค๋นํ์
๋ฐฉ๋ฒ1)
public class ApplicationContext {
private AlertRule alertRule;
private Notification notification;
private Alert alert;
public void initalizeBeans() {
alertRule = new AlertRule(/*๋งค๊ฐ๋ณ์ ์๋ต*/); // ์ด๊ธฐํ ์ฝ๋ ์๋ต
notification = new Notification(/* ๋งค๊ฐ๋ณ์ ์๋ต*/); // ์ด๊ธฐํ ์ฝ๋ ์๋ต
alert = new Alert();
alert.addAlertHandler(new TpsAlerthandler(alertRule, notification));
alert.addAlertHandler(new ErrorAlertHandler(alertRule, notification));
// api ํธ๋ค๋ฌ ๋ฑ๋ก
}
public Alert getAlert() { return alert; }
private static final ApplicationContext instance = new ApplicationContext();
private ApplicationContext() {
initalizeBeans();
}
public static ApplicationContext getInstance() {
return instance;
}
}
public class Demo {
public static void main(String[] args) {
ApiStatInfo apiStatInfo = new ApiStatInfo();
// ... apiStatInfo ๋ฐ์ดํฐ ๊ฐ ์ค์ ์ฝ๋ ์๋ต ..
ApplicationContext.getInstance().getAlert().check(apiStatInfo);
}
}
public class ApiStatInfo {
private String api;
private long requestCount;
private long errorCount;
private long duration;
private long timeoutCount; // ๋ณ๊ฒฝ1 : timeoutCount ์์ฑ ์ถ๊ฐ
}
public abstract class AlertHandler {// ์ฝ๋ ๋ณ๊ฒฝ ์์ }
public class TpsAlertHandler extends AlertHandler {//์ฝ๋ ๋ณ๊ฒฝ ์์ }
public class ErrorHandler extends AlertHandler {// ์ฝ๋ ๋ณ๊ฒฝ ์์ }
// ๋ณ๊ฒฝ2 : TimeoutAlertHandler ํด๋์ค ์ถ๊ฐ
public class TimeoutAlertHandler extends AlertHandler {//์ฝ๋ ์๋ต}
๋ฐฉ๋ฒ2)
์ถ์ ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ํด๋์ค๋ฅผ ์ฃผ์ ๋ฐ์์ ๋์ผํ ์ด๋ฆ์ด์ง๋ง ๋ค๋ฅธ ๋์์ ํ๋๋ก ํ๋ค
| ๋น๋ํจํด ์ง์ ๊ตฌํํด๋ณด๊ธฐ (0) | 2024.11.05 |
|---|---|
| [๋์์ธํจํด] ํฉํฐ๋ฆฌํจํด (0) | 2024.05.05 |
| [๋์์ธํจํด] ์ฑ๊ธํด ํจํด (0) | 2024.05.01 |
| ์ด๋ํฐ ํจํด (0) | 2024.03.05 |