티스토리 뷰

spring boot 를 가지고 웹사이트를 개발해보려 한다. 오랜만에 java 환경을 경험하려 하니, 시행착오가 너무 많다. mac환경에서 docker 이미지를 가지고 로컬 개발환경을 셋팅해본다. 

OpenJDK 설치

아래 사이트로 가서 다운받고 로컬pc에 OpenJDK 설치를 한다. docker로 환경을 만들더라도 본 로컬환경에 설치는 필요하다. 여기서는 11 버전으로 설치했다.

 

Home

Eclipse Adoptium provides prebuilt OpenJDK binaries from a fully open source set of build scripts and infrastructure. Supported platforms include Linux, macOS, Windows, ARM, Solaris, and AIX.

adoptium.net

 

SpringToolSuite4 설치

vscode 를 가지고 개발을 할까 했는데, 단축키든 뭐든 다 익숙하지가 않아 결국 이클립스를 설치하기로 했다. spring 개발이니, 아래 사이트에 가서 spring tool suite 를 설치했다.

 

Spring Tools 4 is the next generation of Spring tooling

Largely rebuilt from scratch, Spring Tools 4 provides world-class support for developing Spring-based enterprise applications, whether you prefer Eclipse, Visual Studio Code, or Theia IDE.

spring.io

 

그리고, code completion 의 경우, 이클립스 설정을 좀 바꿔줘야 한다. content assist 단축키를 나같은 경우는, 커멘트키+스페이스로 변경했다. 

 

그리고 데모로 프로젝트를 하나 생성했다. maven, jdk11, spring-web 등의 선택하는 설정이 끝나고 나면, maven dependency 를 받고 빌드하냐고 시간이 꽤 걸린다. 이클립스 상태바가 완료될때까지 기다린다. 

반응형

docker 이미지 다운로드 및 빌드

openjdk docker 이미지를 기준으로 maven 설치를 추가했다.

FROM openjdk:11.0-jdk-bullseye
RUN apt-get update; \
		apt-get install -y apt-utils curl git maven;

 

그리고, spring boot 프로젝트가 있는 폴더와 maven 폴더를 별도로 로컬 폴더와 바인딩을 했다.  

$ docker run -it --rm --name my-openjdk \
	-p 8080:8080 \
	-v ~/github/sample/java/spring:/usr/src/app \
	-v ~/github/sample/java/mymaven:/root/.m2 \
	-w /usr/src/app openjdk-1 zsh

여기서 maven 파일을 바인딩 하지 않으면, 계속 docker run 할때마다 maven dependency 를 다운받게 되버린다. 아니면 이미지 빌드할때, 프로젝트 소스를 copy해서 올려놓고 maven 실행까지 docker build를 하면 되지만, 그렇게 하면 매번 docker image 빌드 시간이 너무 많이 걸리게 된다. 차라리 로컬에 별도로 폴더를 마련해주고, 연결해서 사용하는게 나을거 같았다.

이렇게 되면, 이클립스에서 개발할때의 로컬pc는 $HOME/.m2 에 maven dependency 가 저장되고, docker image에 소스를 빌드해서 WAS를 실행하면 위의 경우 ~/github/sample/java/mymaven 폴더에 maven dependency 가 저장되는 구조가 된다. 

 

docker 로 프로그램을 실행한다.

아래 사이트에 있는 소스만 좀 가져와서 데모 프로젝트를 수정한다.

 

Spring Quickstart Guide

You will build a classic “Hello World!” endpoint which any browser can connect to. You can even tell it your name, and it will respond in a more friendly way.

spring.io

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class App01Application {

	public static void main(String[] args) {
		SpringApplication.run(App01Application.class, args);
	}

	@GetMapping("/hello")
	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return String.format("Hello %s!", name);
	}

}

빌드 후 실행해본다. 여기서 프로젝트 폴더내에 있는 mvnw 명령어를 사용할 수는 있지만, maven 을 이미 docker image에 install 했으니, mvn 명령어로 실행했다.

$ mvn spring-boot:run

최초 처음에만 maven 다운로드 때문에 시간이 많이 걸리긴하지만, 로컬에 바인딩 했기때문에 2번째 이후부터는 아주 빠르게 실행됨을 볼 수 있다. 설치 끝.

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함