board-service에서 아래 코드 작성하기
public class BoardResponseDto { private Long boardId; private String title; private String content; private UserDto user; public BoardResponseDto(Long boardId, String title, String content, UserDto user) { this.boardId = boardId; this.title = title; this.content = content; this.user = user; } public Long getBoardId() { return boardId; } public String getTitle() { return title; } public String getContent() { return content; } public UserDto getUser() { return user; } }
public class UserDto { private Long userId; private String name; public UserDto(Long userId, String name) { this.userId = userId; this.name = name; } public Long getUserId() { return userId; } public String getName() { return name; } }
userId와 name만 필요하기 때문에 위와 같이 코드를 작성했다. 이 프로젝트에서는 외부 API 통신을 할 때RestClient라는 라이브러리를 사용할 것이다. ‘MSA니까 반드시WebClient를 써야돼’라는 건 없다.RestTemplate를 써도 되고WebClient를 써도 되고FeignClient를 써도 된다. API 통신을 할 수 있는 라이브러리라면 어떤 라이브러리를 사용해도 상관없다.
server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3307/board-db # DB 주소 username: root # DB 계정 password: password # DB 비밀번호 driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true client: user-service: url: http://localhost:8080
public class UserResponseDto { private Long userId; private String email; private String name; public Long getUserId() { return userId; } public String getEmail() { return email; } public String getName() { return name; } }
@Component public class UserClient { private final RestClient restClient; public UserClient( @Value("${client.user-service.url}") String userServiceUrl ) { this.restClient = RestClient.builder() .baseUrl(userServiceUrl) .build(); } public UserResponseDto fetchUser(Long userId) { return this.restClient.get() .uri("/users/{userId}", userId) .retrieve() .body(UserResponseDto.class); } }
@Service public class BoardService { private final BoardRepository boardRepository; private final UserClient userClient; public BoardService(BoardRepository boardRepository, UserClient userClient) { this.boardRepository = boardRepository; this.userClient = userClient; } ... public BoardResponseDto getBoard(Long boardId) { // 게시글 불러오기 Board board = boardRepository.findById(boardId) .orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); // user-service로부터 사용자 정보 불러오기 UserResponseDto userResponseDto = userClient.fetchUser(board.getUserId()); // 응답값 조합하기 UserDto userDto = new UserDto( userResponseDto.getUserId(), userResponseDto.getName() ); BoardResponseDto boardResponseDto = new BoardResponseDto( board.getBoardId(), board.getTitle(), board.getContent(), userDto ); return boardResponseDto; } }
@RestController @RequestMapping("/boards") public class BoardController { private final BoardService boardService; public BoardController(BoardService boardService) { this.boardService = boardService; } ... @GetMapping("/{boardId}") public ResponseEntity<BoardResponseDto> getBoard(@PathVariable Long boardId) { BoardResponseDto boardResponseDto = boardService.getBoard(boardId); return ResponseEntity.ok(boardResponseDto); } }


GET localhost:8081/boards/1)로 요청을 보낸다. GET localhost:8080/users/1)로부터 데이터를 조회해온다.