Skip to content

Commit f101374

Browse files
authored
[Fix] build.test 오류 수정 (#41)
* [Fix]Test 코드 수정 * [Refactor]CafeteriaServiceV2 단일 책임 원칙 적용하여 클래스 분리 * Update Java CI-CD.yml dependency submission code removed * [Refactor]CampusServiceV2 클래스 분리 * [Refactor]DietServiceV2 서비스 클래스 분리 * [fix]Cafeteria Cacheable 주석처리 * [Refactor]캐시 적용 및 중복로직 최적화 * [fix]userService에서 발생한 hikariCP connection error 찾는 중 * [Test]HikariCP 멀티스레드 환경 테슽 * [Fix]파일명 수정 * [Feat]hikari, tomcat monitoring code * [Fix]redis db 수정 위해 cacheable 주석처리 * [Fix]code rabbit 반영 * [Fix] build.test 오류 수정
1 parent d2619a3 commit f101374

File tree

9 files changed

+114
-54
lines changed

9 files changed

+114
-54
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ dependencies {
4747

4848
// actuator
4949
implementation 'org.springframework.boot:spring-boot-starter-actuator'
50+
implementation 'io.micrometer:micrometer-core'
51+
implementation 'io.micrometer:micrometer-registry-prometheus'
5052

5153
implementation 'io.sentry:sentry-spring-boot-starter-jakarta:8.3.0'
5254
implementation 'javax.enterprise:cdi-api:2.0'

src/main/java/com/example/Jinus/controller/DietController.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RestController;
1010

11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
1114
@RestController
1215
@RequestMapping("/api/spring")
1316
@RequiredArgsConstructor
1417
public class DietController {
15-
private final DietService dietServiceV2;
18+
private final DietService dietService;
19+
private static final Logger logger = LoggerFactory.getLogger(DietController.class);
1620

1721
@PostMapping("/v2/dish")
1822
public String handleRequest(@RequestBody RequestDto requestDto) {
19-
return dietServiceV2.requestHandler(requestDto);
23+
String threadName = Thread.currentThread().getName();
24+
logger.info("[식단 요청] 스레드 이름: {}", threadName);
25+
26+
return dietService.requestHandler(requestDto);
2027
}
2128

2229
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.Jinus.monitor;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
import com.zaxxer.hikari.HikariPoolMXBean;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.scheduling.annotation.Scheduled;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
@Slf4j
12+
public class HikariCPMonitor {
13+
14+
private final HikariDataSource dataSource;
15+
16+
@Autowired
17+
public HikariCPMonitor(HikariDataSource dataSource) {
18+
this.dataSource = dataSource;
19+
}
20+
21+
@Scheduled(fixedRateString = "${monitor.pool.status.rate.ms:60000}") // 상태 출력 주기 (기본 1분)
22+
public void logHikariStatus() {
23+
if (dataSource != null) {
24+
HikariPoolMXBean poolMXBean = dataSource.getHikariPoolMXBean();
25+
26+
int total = poolMXBean.getTotalConnections();
27+
int active = poolMXBean.getActiveConnections();
28+
int idle = poolMXBean.getIdleConnections();
29+
int waiting = poolMXBean.getThreadsAwaitingConnection();
30+
31+
log.info("[HikariCP 상태] 전체: {} / 사용 중: {} / 유휴: {} / 대기 중: {}",
32+
+ total, active, idle, waiting);
33+
}
34+
}
35+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example.Jinus.monitor;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.apache.catalina.connector.Connector;
5+
import org.apache.coyote.ProtocolHandler;
6+
import org.apache.coyote.AbstractProtocol;
7+
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
8+
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
9+
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
10+
import org.springframework.scheduling.annotation.Scheduled;
11+
import org.springframework.stereotype.Component;
12+
13+
import java.util.concurrent.Executor;
14+
15+
@Component
16+
@Slf4j
17+
public class TomcatThreadMonitor {
18+
19+
private final ServletWebServerApplicationContext context;
20+
21+
public TomcatThreadMonitor(ServletWebServerApplicationContext context) {
22+
this.context = context;
23+
}
24+
25+
@Scheduled(fixedRateString = "${monitor.pool.status.rate.ms:60000}") // 상태 출력 주기 (기본 1분)
26+
public void logTomcatThreadPoolStatus() {
27+
if (context.getWebServer() instanceof TomcatWebServer tomcatWebServer) {
28+
Connector connector = tomcatWebServer.getTomcat().getConnector();
29+
ProtocolHandler handler = connector.getProtocolHandler();
30+
31+
if (handler instanceof AbstractProtocol<?> protocol) {
32+
Executor executor = protocol.getExecutor();
33+
34+
if (executor instanceof ThreadPoolExecutor threadPoolExecutor) {
35+
int max = threadPoolExecutor.getMaximumPoolSize();
36+
int poolSize = threadPoolExecutor.getPoolSize();
37+
int active = threadPoolExecutor.getActiveCount();
38+
long taskCount = threadPoolExecutor.getTaskCount();
39+
long completedTaskCount = threadPoolExecutor.getCompletedTaskCount();
40+
41+
log.info("[Tomcat 스레드] MaxPoolSize: {}, PoolSize: {}, 활성: {}, TaskCount: {}, 완료: {}",
42+
+ max, poolSize, active, taskCount, completedTaskCount);
43+
}
44+
}
45+
}
46+
}
47+
}

src/main/java/com/example/Jinus/service/cafeteria/CacheService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ private long calculateTtlUntilNextMidnight(Date dietDate) {
5555

5656

5757
// Redis에서 조회 (없으면 DB에서 가져옴)
58-
@Cacheable(
59-
value = "cafeteriaList",
60-
key = "#p0",
61-
cacheManager = "contentCacheManager")
58+
// @Cacheable(
59+
// value = "cafeteriaList",
60+
// key = "#p0",
61+
// cacheManager = "contentCacheManager")
6262
public List<CafeteriaDto> getCafeteriaList(int campusId) {
6363
return cafeteriaRepositoryV2.findCafeteriaListByCampusId(campusId);
6464
}

src/main/java/com/example/Jinus/service/cafeteria/CafeteriaQueryService.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
public class CafeteriaQueryService {
1313
private final CafeteriaRepository cafeteriaRepositoryV2;
1414

15-
@Cacheable(
16-
value = "cafeteriaId",
17-
key = "#p0 + '::' + #p1",
18-
unless = "#result == -1",
19-
cacheManager = "contentCacheManager")
15+
// @Cacheable(
16+
// value = "cafeteriaId",
17+
// key = "#p0 + '::' + #p1",
18+
// unless = "#result == -1",
19+
// cacheManager = "contentCacheManager")
2020
public int getCafeteriaId(String cafeteriaName, int campusId) {
2121
return cafeteriaRepositoryV2.findCafeteriaId(cafeteriaName, campusId).orElse(-1);
2222
}
2323

24-
@Cacheable(
25-
value = "cafeteriaUrl",
26-
key = "#p0",
27-
cacheManager = "contentCacheManager")
24+
// @Cacheable(
25+
// value = "cafeteriaUrl",
26+
// key = "#p0",
27+
// cacheManager = "contentCacheManager")
2828
public String getImgUrl(int cafeteriaId) {
2929
return cafeteriaRepositoryV2.findImgUrlByCafeteriaId(cafeteriaId);
3030
}

src/main/java/com/example/Jinus/service/cafeteria/CampusService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
public class CampusService {
1616
private final CampusRepository campusRepositoryV2;
1717

18-
@Cacheable(
19-
value = "campusId",
20-
key = "#p0",
21-
cacheManager = "contentCacheManager")
18+
// @Cacheable(
19+
// value = "campusId",
20+
// key = "#p0",
21+
// cacheManager = "contentCacheManager")
2222
// 사용자의 campusId 받아 캠퍼스 이름 찾기
2323
public String getUserCampusName(int campusId) {
2424
return campusRepositoryV2.findCampusNameByCampusId(campusId);
@@ -29,10 +29,10 @@ public List<CampusEntity> getCampusList() {
2929
return campusRepositoryV2.findCampusList();
3030
}
3131

32-
@Cacheable(
33-
value = "campusName",
34-
key = "#p0",
35-
cacheManager = "contentCacheManager")
32+
// @Cacheable(
33+
// value = "campusName",
34+
// key = "#p0",
35+
// cacheManager = "contentCacheManager")
3636
// 캠퍼스 이름으로 id 찾기
3737
public int getCampusId(String campusName) {
3838
return campusRepositoryV2.findCampusIdByName(campusName);

src/main/java/com/example/Jinus/service/diet/DietQueryService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
public class DietQueryService {
2121

2222
private final CacheService cacheServiceV2;
23-
private final DietRepository dietRepositoryV2;
2423

2524
// 메뉴 존재 여부에 따른 반환값 처리 로직
2625
public String getDietResponse(HandleRequestDto parameters, int cafeteriaId) {
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,16 @@
11
package com.example.Jinus.service.userInfo;
22

3-
import com.example.Jinus.entity.userInfo.CollegeEntity;
43
import com.example.Jinus.repository.userInfo.CollegeRepository;
54
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
76
import org.springframework.stereotype.Service;
87

98
@Service
109
public class CollegeService {
11-
private static final Logger logger = LoggerFactory.getLogger(CollegeService.class);
1210
private final CollegeRepository collegeRepository;
1311

1412
public CollegeService(CollegeRepository collegeRepository) {
1513
this.collegeRepository = collegeRepository;
16-
// logger.info("CollegeService 실행");
1714
}
1815

19-
public String getCollegeName(int collegeId) {
20-
// logger.info("getCollegeName 실행");
21-
CollegeEntity collegeEntity = collegeRepository.findById(collegeId).orElse(null);
22-
// logger.info("collegeEntity: {}", collegeEntity);
23-
24-
if (collegeEntity != null) {
25-
// logger.info("collegeEng: {}", collegeEntity.getCollegeEng());
26-
return collegeEntity.getCollegeEng(); // 학과 영어 이름 리턴(테이블 찾기 위함)
27-
} else {
28-
logger.debug("CollegeService: collge를 찾을 수 없습니다.");
29-
return null;
30-
}
31-
}
32-
33-
public boolean checkEtcValue(int collegeId) {
34-
CollegeEntity collegeEntity = collegeRepository.findById(collegeId).orElse(null);
35-
return collegeEntity.isEtcValue();
36-
}
37-
38-
// collegeId에 해당하는 campusId 찾기
39-
public int getCampusId(int collegeId) {
40-
// logger.info("getCampusId 실행");
41-
int campusId = collegeRepository.findCampusId(collegeId);
42-
// logger.info("campusId: {}", campusId);
43-
44-
return campusId;
45-
}
4616
}

0 commit comments

Comments
 (0)