2 minute read

๐Ÿ“– SpringBoot Server

์—ฐ์Šต์„ ์œ„ํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ demo/hello API์™€ getํ˜•์‹์˜ demo/demo๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Demo Server๋ฅผ ์˜ฌ๋ ธ๋‹ค.
์Šคํ”„๋ง๋ถ€ํŠธ๋Š” IntelliJ์—์„œ ์ง„ํ–‰ํ•  ์˜ˆ์ •์ด๋ฉฐ, ์ž๋ฐ”๋Š” JDK-17/ ์Šคํ”„๋ง๋ถ€ํŠธ ๋ฒ„์ „์€ 3.2.2์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

image

image

์œ„์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.
์•„๋ž˜๋Š” MVC๋ชจ๋ธ๋กœ ๊ตฌํ˜„ํ•œ ์†Œ์Šค์ฝ”๋“œ์ด๋‹ค.


๐Ÿ„ ์†Œ์Šค ์ฝ”๋“œ

Controller
package com.example.demo.Controller;

import com.example.demo.Service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("demo")
public class DemoController {

    private final DemoService demoService;

    @Autowired
    public DemoController(DemoService demoService) {
        this.demoService = demoService;
    }

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping()
    public String getName(int index) {
        System.out.println(index);
        return demoService.DemoDTO(index);
    }
}


DTO
package com.example.demo.DTO;

public class DemoDTO {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


Entity
package com.example.demo.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Demo")
public class Demo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int index;

    @Column(nullable = false)
    private String name;
}


Repository
package com.example.demo.Repository;

import com.example.demo.DTO.DemoDTO;
import com.example.demo.entity.Demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DemoRepository extends JpaRepository<Demo, Integer> {
}


Service
package com.example.demo.Service;

import com.example.demo.DTO.DemoDTO;
import com.example.demo.Repository.DemoRepository;
import com.example.demo.entity.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class DemoService {
    private final DemoRepository demoRepository;

    @Autowired
    public DemoService(DemoRepository demoRepository) {
        this.demoRepository = demoRepository;
    }

    public String DemoDTO(int index) {
        Demo demo = demoRepository.findById(index).get();

        return demo.getName();
    }
}


Repository
package com.example.demo.Repository;

import com.example.demo.DTO.DemoDTO;
import com.example.demo.entity.Demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DemoRepository extends JpaRepository<Demo, Integer> {
}


application.yml

๊ธฐ์กด์— ์žˆ๋˜ application.properties ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ณ , application.yml๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์—ˆ๋‹ค.
์ด๋•Œ local db๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ, ์ด๋ฅผ ์œ„ํ•˜์—ฌ docker์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š” host network์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ host.docker.internal๋ฅผ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

image1

spring:
  datasource:
    url: jdbc:mariadb://host.docker.internal:3306/demo
    driver-class-name: org.mariadb.jdbc.Driver
    username: 'root'
    password: '1234'
  jpa:
    open-in-view: false
    generate-ddl: true
    show-sql: true
    hibernate:
      ddl-auto: update
  application:
    name: demo
  profiles:
    active: dev

server:
  port: 8080


์˜์กด์„ฑ ์ฃผ์ž…

build.gradle์— ์žˆ๋Š” dependencies

//swagger
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
    //Database
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client' // MariaDB


๐Ÿ“– docker ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ

์•„๋ž˜์™€ ๊ฐ™์ด ์˜ค๋ฅธ์ชฝ์˜ Gradle ํ•ญ๋ชฉ์—์„œ build/bootJar์„ ํด๋ฆญํ•˜์—ฌ ํŒŒ์ผ ๋นŒ๋“œ๋ฅผ ํ•ด์ค€๋‹ค.
๊ทธ๋Ÿฌ๋ฉด build/libs ์•„๋ž˜์— .jarํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๊ฒŒ ๋œ๋‹ค.

image

dockerfile์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํŒŒ์ผ์„ ํ”„๋กœ์ ํŠธ rootํด๋” ์•„๋ž˜์— ๋งŒ๋“ค์–ด์ค€๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด์šฉ์„ ์ ์–ด์ค€๋‹ค.

  • dockerfile
    FROM openjdk:11
    ARG JAR_FILE=*.jar
    COPY ${JAR_FILE} app.jar
    ENTRYPOINT ["java","-jar","/app.jar"]
    


๐Ÿ„ ์‹ค์Šต

์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•ด์ฃผ๊ณ , Docker Hub์— ์ด๋ฅผ ์˜ฌ๋ฆฐ๋‹ค.

docker build -t [๋„์ปคํ—ˆ๋ธŒ ID]/[Repository ์ด๋ฆ„] .
docker push [๋„์ปคํ—ˆ๋ธŒ ID]/[Repository ์ด๋ฆ„]

์•„๋ž˜์™€ ๊ฐ™์ด docker Hub์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋ฐ›์•„์„œ ์‹คํ–‰์‹œ์ผฐ๋‹ค.

docker run -d -i -p 8080:8080 [๋„์ปค ํ—ˆ๋ธŒ ID/์ด๋ฏธ์ง€ ์ด๋ฆ„]

์•„๋ž˜๋Š” ์‹คํ–‰ ํ™”๋ฉด๋“ค์ด๋‹ค.

demo

swagger

get


๐Ÿ“– ๋ฉ€ํ‹ฐ ์Šคํ…Œ์ด์ง€๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ

๊ธฐ์กด๊ณผ ๋™์ผํ•œ ์ฝ”๋“œ์—์„œ Dockerfile์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

# build
FROM gradle:8.5 AS builder
WORKDIR /build

COPY build.gradle settings.gradle /build/
RUN gradle build -x test --parallel --continue > /dev/null 2>&1 || true

COPY . /build
RUN gradle build -x test --parallel


# app
FROM openjdk:17

WORKDIR /app

COPY --from=builder /build/build/libs/*-SNAPSHOT.jar ./app.jar

ENTRYPOINT ["java","-jar","app.jar"]

EXPOSE 8080

์ด๋•Œ, builder์˜ ๋ฒ„์ „์„ ํ™•์ธํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋Š” gradle/wrapper/gradle-wrapper.properties ํŒŒ์ผ์— ์žˆ๋Š” distributionUrl์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ„ ์‹ค์Šต

docker build -t [๋„์ปคํ—ˆ๋ธŒ ID]/[Repository ์ด๋ฆ„] .
docker push [๋„์ปคํ—ˆ๋ธŒ ID]/[Repository ์ด๋ฆ„]

๊ธฐ์กด๊ณผ ๋˜‘๊ฐ™์ด build ํ›„ pushํ•ด์ฃผ์—ˆ๋‹ค.
๊ธฐ์กด๊ณผ ๋‹ฌ๋ผ์ง„ ์ ์€ Dockerfile์—์„œ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์—, bootJar๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค„ ํ•„์š”๋Š” ์—†๋‹ค.

docker run -d -i -p 8080:8080 [๋„์ปค ํ—ˆ๋ธŒ ID/์ด๋ฏธ์ง€ ์ด๋ฆ„]

์ด์ „๊ณผ ๊ฐ™์ด ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜