JSCODE Logo
프로그래밍 과외블로그후기멘토진
회사명 : JSCODE대표 : 박재성사업자 등록번호 : 244-22-01557통신판매업 : 제 2023-인천미추홀-0381 호
학원 명칭 : 제이에스코드(JSCODE)원격학원학원설립ㆍ운영 등록번호 : 제6063호

서울특별시 구로구 경인로 20가길 11(오류동, 아델리아)

Copyright ⓒ 2025 JSCODE - 최상위 현업 개발자들의 프로그래밍 교육 All rights reserved.

이용약관개인정보처리방침
← 블로그 목록으로 돌아가기

[실습] 게시글 조회 로직 최적화하기 (

JSCODE 박재성
JSCODE 박재성
2025-12-06
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
practice-optimize-post-query
type
post
updatedAt
Dec 6, 2025 05:47 AM

✅ 게시글 조회 로직 최적화하기

board-service에서 아래 코드 작성하기
  1. Board 엔티티에 연관 관계 매핑하기
    1. domain/Board
      @Entity @Table(name = "boards") public class Board { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long boardId; private String title; private String content; @ManyToOne @JoinColumn(name = "user_id", insertable = false, updatable = false) // 조회용 private User user; @Column(name = "user_id") private Long userId; public Board() { } public Board(String title, String content, Long userId) { this.title = title; this.content = content; this.userId = userId; } ... public User getUser() { return user; } }
       
  1. 게시글 조회 로직 수정하기
    1. service/BoardService
      @Service public class BoardService { ... // 연관관계를 활용한 게시글 조회 public BoardResponseDto getBoard2(Long boardId) { Board board = boardRepository.findById(boardId) .orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); // BoardResponseDto 생성 BoardResponseDto boardResponseDto = new BoardResponseDto( board.getBoardId(), board.getTitle(), board.getContent(), new UserDto( board.getUser().getUserId(), board.getUser().getName() ) ); return boardResponseDto; } // 연관관계를 활용한 게시글 전체 조회 public List<BoardResponseDto> getBoards2() { List<Board> boards = boardRepository.findAll(); return boards.stream() .map(board -> new BoardResponseDto( board.getBoardId(), board.getTitle(), board.getContent(), new UserDto( board.getUser().getUserId(), board.getUser().getName() ) )) .toList(); } }
       
  1. 컨트롤러 로직 바꾸기
    1. controller/BoardController
      @RestController @RequestMapping("/boards") public class BoardController { ... @GetMapping("/{boardId}") public ResponseEntity<BoardResponseDto> getBoard(@PathVariable Long boardId) { // BoardResponseDto boardResponseDto = boardService.getBoard(boardId); BoardResponseDto boardResponseDto = boardService.getBoard2(boardId); return ResponseEntity.ok(boardResponseDto); } @GetMapping() public ResponseEntity<List<BoardResponseDto>> getBoards() { // List<BoardResponseDto> boardResponseDtos = boardService.getBoards(); List<BoardResponseDto> boardResponseDtos = boardService.getBoards2(); return ResponseEntity.ok(boardResponseDtos); } }
       
  1. 서버 다시 실행시키기
    1. notion image
       
  1. 테스트를 위해 게시글 데이터 2개 넣기
    1. notion image
      notion image
       
  1. API 테스트 해보기
    1. 특정 게시글 조회하기
      1. notion image
         
    2. 전체 게시글 조회하기
      1. notion image
마이크로서비스 간 통신 방식에 비해 조회 로직이 훨씬 단순해졌다. 또한 게시글 서비스가 사용자 정보를 가져오기 위해 사용자 서비스에 API 요청을 보내지 않아, 사용자 서비스나 사용자 DB에 불필요한 부하가 발생하지 않는다.
 
author
JSCODE 박재성
category
MSA
createdAt
Dec 6, 2025
series
비전공자도 이해할 수 있는 MSA 입문/실전
slug
type
series-footer
updatedAt
Dec 6, 2025 05:45 AM
📎
이 글은 비전공자도 이해할 수 있는 MSA 입문/실전 강의의 수업 자료 중 일부입니다.