[Spring] Spring Boot

[Spring] Spring Boot

[Spring] Spring Boot

Spring Boot

Spring Boot

- Spring framework의 프로젝트 중 하나

- 특별한 설정 작업 없이 기본 설정만으로 다양한 기능을 구현 가능

- Tomcat 내장 서버를 가지고 있어 따로 Tomcat 설정이 불필요

Project 구성(gradle)

- Spring boot 생성 시, 다음과 같은 구조가 생성

1. BoottestApplication

1 2 3 4 5 6 7 8 @SpringBootApplication public class BoottestApplication { public static void main( String [] args) { SpringApplication.run(BoottestApplication. class , args); } } Colored by Color Scripter cs

- 일반 Spring과 달리, main을 가지고 있음

- @SpringBootApplication을 지정하여 Boot를 사용한다고 명시하고 해당 annotation이 기본 설정을 해줌

1) @SpringBootApplication

1 2 3 4 5 6 7 8 9 10 11 12 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter. class ), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter. class ) }) public @ interface SpringBootApplication { } Colored by Color Scripter cs

- SpringBootApplication은 여러가지 annotation을 조합하여 만들어진 annotation

- 기본적으로 @SpringBootConfiguration, @EnableAutoConfiguration이 사용되어 기본 설정이 이루어짐

- @ComponentScan을 통해 bean들을 scan

2. application.properties

- Boot의 설정 파일로 필요한 설정들을 명시할 수 있음

1 server.port = 8080 cs

- 위와 같이 명시하면 server의 port number를 바꿀 수 있음(default=8080)

1) properties 값 읽기

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RestController @RequestMapping(value = "/port" ) public class PortController { @Value( "${server.port}" ) private String port; @Value( "#{systemProperties['test']}" ) private String test; @RequestMapping(value = "/" , method = RequestMethod.GET) public String getPort(){ return port + " " + test; } } Colored by Color Scripter cs

- @Value를 사용하여 application.properties에 설정한 값을 읽어올 수 있음

- jvm 실행 옵션에 "-Dtest=sample"과 같이 system property 값을 설정하여 사용할 수 있음

- 결과

3. build.gradle

- maven의 pom.xml과 같이 gradle을 사용할 때 dependency를 추가해주는 파일

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 31 32 33 34 35 36 37 38 buildscript { ext { springBootVersion = '2.1.4.RELEASE' } repositories { mavenCentral() jcenter() } dependencies { classpath( "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" ) } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' targetCompatibility = '11' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' runtimeOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } wrapper { gradleVersion = '5.2.1' } Colored by Color Scripter cs

- dependencies에 'org.springframework.boot:spring-boot-starter', 'org.springframework.boot:spring-boot-starter-web' dependency를 추가하여 기본적으로 제공되는 Boot의 기능을 사용할 수 있음

Spring Boot Data

- Spring에 비해 JDBC, JPA, MyBatis를 사용하는데 있어서 설정해야할 부분이 적어짐

Config

1. dependency 추가

1 2 3 implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:1.4.191' cs

- h2 DB를 사용하기 위해 dependency 추가

- jdbc와 jpa를 사용하기 위해 dependency 추가

2. Database 설정

1) DataSource 설정(application.properties)

1 2 spring.datasource.url = jdbc:h2:mem:test spring.datasource.driver - class - name = org.h2.Driver cs

- datasource의 url을 application.properties에 설정(h2, in memory, test db)

- driver-class를 org.h2.Driver로 지정

2) Schema.sql

1 2 3 4 CREATE TABLE user ( id varchar( 100 ) PRIMARY KEY, name varchar( 30 ) ); cs

- "/resources"에 "schema.sql"을 생성하면 boot에서 읽어와 table을 생성

Spring JDBC

- 적은 설정으로 Spring에서 사용했던 방식대로 사용할 수 있음

1. Model

1 2 3 4 5 @Data public class User { private String id; private String name; } cs

- User Model 생성

- lombok 사용(@Data)

2. Controller

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @RestController @RequestMapping(value = "/jdbc" ) public class JdbcController { @Autowired private JdbcTemplate jdbcTemplate; @PostMapping(value = "/" ) public void createUser(@RequestBody User user) { jdbcTemplate.update( "INSERT INTO user(id, name) VALUES(?, ?)" , user.getId(), user.getName()); } @GetMapping(value = "/" ) public List < User > getUserAll() { return jdbcTemplate.query( "SELECT * FROM user" ,(rs, i) - > { User u = new User(); u.setId(rs.getString( "id" )); u.setName(rs.getString( "name" )); return u; }); } } Colored by Color Scripter cs

- JdbcTemplate을 추가적으로 설정할 필요없이 바로 Autowired로 주입받아 사용

- DB에 값을 넣는 createUser와 User정보를 가져오는 getUserAll 구현

3. 결과

1) Insert(POST)

2) Select(GET)

Spring Data JPA

- 적은 설정으로 Spring에서 사용했던 방식대로 사용할 수 있음

1. Model

1 2 3 4 5 6 7 8 @Data @Entity @Table(name = "user" ) public class User { @Id private String id; private String name; } cs

- JPA를 사용하기 위해 @Entity, @Table을 지정

- primary key인 id에 @Id를 지정

2. Repository

1 2 3 public interface UserRepository extends JpaRepository < User, String > { } Colored by Color Scripter cs

- JpaRepository를 상속받는 repository interface 생성

3. Controller

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @RestController @RequestMapping( "/jpa" ) public class JpaController { @Autowired private UserRepository userRepository; @RequestMapping(value = "/" , method = RequestMethod.POST) public void createUser(@RequestBody User user) { userRepository.save(user); } @RequestMapping(value = "/" , method = RequestMethod.GET) public List < User > getUserAll() { return userRepository.findAll(); } } Colored by Color Scripter cs

- 생성한 repository를 autowired로 주입

4. 결과

1) Insert(POST)

2) Select(GET)

Spring Boot Security

- Spring에 비해 적은 설정으로 Spring Security를 사용할 수 있음

- 기본 인증은 Basic auth로 설정

Config

1. dependency 추가

1 implementation 'org.springframework.boot:spring-boot-starter-security' cs

2. application.properties

1 2 3 spring.security.user.name = tester spring.security.user.password = tester spring.security.user.roles = ADMIN cs

- application.properties에 인증에 필요한 id, password, role을 설정할 수 있음

Spring Security

- Spring Boot에서는 dependency에 security를 등록할 경우, 자동으로 basic auth로 인증이 필요하게 설정

- application.properties에 등록한 id,password로 인증

Spring Boot Actuator

- Applicatino 운영을 위해 모니터링 기능과 관리를 위한 데이터를 제공

- Application의 상태를 확인할 수 있는 endpoint 제공

Endpoint

1. GET /beans

- DI Container의 bean 목록 확인

2. GET /env

- 환경 변수와 시스템 프로퍼티 확인

3. GET /configprops

- @ConfigurationProperties 목록 확인

4. GET /threaddump

- thread dump 확인

5. GET /health

- health check 결과 확인

6. GET /info

- info.*의 프로퍼티 값 목록과 같은 application 정보 확인

7. GET /logger

- logging 정보 확인

9. GET /metrics

- metric 확인

10. GET /mappings

- mapping 정보 확인

11. POST /shutdown

- application 정지 요청

12. GET /httptrace

- HTTP Request 로그 확인

Config

- actuator 사용 시, default로는 shutdown을 뺀 나머지는 모두 활성화

1. exposure

- 활성화는 되어있지만 접근하기 위해 exposure 설정을 해야함

- 기본적으로 보이는 endpoint는 health, info 등이 있음

- application.properties에 management.endpoints.web.exposure.include=* 추가

- 사용하고 싶은 endpoint를 * 대신 명시할 수 있음

2. disable

- 불필요할 시, disable을 통해 비활성화할 수 있음

- application.properties에 management.endpoint.{Http Endpoint}.enabled = false 추가

ex) management.endpoint.beans.enabled = false

Health Check

- Actuator에서 제공

- 현재 Server의 상태를 확인할 수 있음

1. State

- UP : 정상 작동

- DOWN : 비정상

- UNKNOWN : 알 수 없음

- OUT_OF_SERVICE : 이용 불가

2. show detail

- 기본적으로 /health로 요청 시, state만 확인할 수 있음

- application.properties에 management.endpoint.health.show-details=always 추가

1) 결과

- no show-details

- show-details

3. Httpstatus 적용

- 각 state별로 원하는 http 응답 코드를 설정할 수 있음

- application.properties에 management.health.status.http-mapping.{state}={error_code} 추가

ex) management.health.status.http-mapping.UP=201

1) 결과

Metric

- Application의 특성들을 볼 수 있게 해주는 Spring actuator 기능

1. GET /metrics

- 조회할 수 있는 metric들의 name을 json 형태로 반환

- 해당 name을 path variable로 넘겨 자세한 내용 조회

2. GET /metrics/{metric_name}

- metrics로 조회한 metric name을 조회

- jvm.threads.states 정보를 조회

from http://dongdd.tistory.com/179 by ccl(A) rewrite - 2020-03-06 07:54:54

댓글

이 블로그의 인기 게시물

데이터 바인딩 추상화 - propertyEditor

[샤니마스 SPRING PARTY2020] THE IDOLM @ STER SHINY COLORS SPRING...

Spring Web Form