마이크로서비스
What is the micorservices
- An architectural style
- An application as a collection of services
- 높은 maintainable과 testable
- Losslely coupled
- 독립적으로 deployable
Monolithic vs Microservice
- Monolithic
- 다수의 서비스, 하나의 프로세스, 하나의 데이터베이스, 하나의 서버
- 서비스가 안되면 전체 다시 껏다 킨다.
- Micorservices
- 다수의 서비스, 다수의 프로세스, 다수의 데이터베이스, 다수의 서버(Well-defined protocol : HTTP, RPC, RMI, ...etc)
- Independently deployalbe
- Independently scalable
- Independent tech stack
- Faster development cycle
- Independence in case of failure-does not crash whole thing
- Servers to manage
- Performance(process vs network)
- Transactio is hard(DB scattered)
- Network security
- Logging
Spring boot
What is the Spring framework?
- Framework란
- skeletons or environment for development
- a collection of reusable, extendable libraries
- Spring framework란?
- open source application for java platform
- based on maven or gradle
- IoC(Inversion of Control)
- DI(Dependency Injection)
- a lot of annotations(@Bean @Service @Respository ...etc)
- suitable to realize micorservice architecture
JSON
- JavaScript Object Notation
- 경량의 DATA-교환 형식
- 사람이 읽고 쓰기에 용이하며, 기계가 분석하고 생성함에도 용이하다.
{
"name": "김개똥",
"age": 19,
"addr": "서울"
"courseId": 31242
}
Spring vs Spring boot
- Spring
- 수동으로 많은 종류의 dependecies를 추가해야함
- 수동으로 웹 서버를 setup해야 함
- 개발 환경에서 IDE도 웹서버랑 연동해야함
- 수동.. 수동.. 수동.. 환경설정
- Spring boot
- 최소한의 설정만으로! 서버로 즉시 활용 가능! 즉시 배포! 즉시 서비스!
- 웹 서버 내장
- Spring을 사용한다 = 자주 사용되는 라이브러리들 알아서 다 들어가 있다.
Structure of Spring boot application
- Maven project directory structure
- pom.xml & External Libraries
- spring configuration file(resources/application.yml or properties)
- package naming(follow project' group name)
- package convention(controller, service, repository, api, config)
강사분 팀 예시
- config : Spring boot project에 필요한 custom configuration들을 정의
- api : 외부 웹 서버와 통신하는 역할의 클래스들 정의
- controller : 외부에서 호출 가능한 rest api들을 제공하는 역할의 클래스들을 정의
- domain or dto : controller에서 resource로 사용될 클래스들 정의(그릇 정도로 이해하면 된다.)
- service : 핵심 비즈니스 로직을 처리하는 역할의 클래스들을 정의
- repository : DB에 접근하여 데이터를 넣거나 업데이트하거나 가져오는 역할의 클래스들을 정의
REST API with spring
HTTP(HyperText Transfer Protocol) Methods
같은 http uri에 대해서 다양한 종류의 method를 적용할 수 있다.
- POST : Create(생성)
- GET : Select(조회)
- PUT : Update(변경)
- PATCH : Update(변경)
- DELETE : Delete(삭제)
- HEAD : Select(조회)
- OPTIONS : proxy
- TRACE : for testing
- CONNECT : method 종류 확인
REST(Representational state transfer)ful
REST basic(Resource, Method, Message)
생성
이름이 Ian인 불독 종류의 강아지를 나이 정보와 함께 생성하고 싶다.
- Resource : dogs
- Method : create(POST)
- Message : Request Body
HTTP POST방식으로, http://localhost:8888/dogs
{
"name": "Ian",
"type": "bulldog",
"age": 11
}
조회
이름이 Ian인 강아지를 조회하고 싶다.
- Resource : dogs
- Method : select(GET)
- Message : RequestParam or PathVariable
HTTP GET방식으로, http://localhost:8888/dogs?name=Ian
HTTP GET방식으로, http://localhost:8888/dogs/Ian
변경
이름이 Ian인 강아지의 종을 변경하고 싶다.
- Resource : dogs
- Method : update(PATCH)
- Message : PathVariable or Request body
HTTP PATCH방식으로, http://localhost:8888/dogs/ian
변경
이름이 Ian인 강아지의 정보를 통짜로 덮어써서 변경하고 싶다.
- Resource : dogs
- Method : update(PUT)
- Message : PathVariable or Request body
HTTP PUT방식으로, http://localhost:8888/dogs/ian
{
"name": "Ian",
"type": "bulldog",
"age": 3
}
삭제
이름이 Ian인 강아지를 삭제하고 싶다.
- Resource : dogs
- Method : delete(DELETE)
- Message : PathVariable or Request body
HTTP DELETE방식으로, http://localhost:8888/dogs/ian
HTTP DELETE방식으로, http://localhost:8888/dogs?name=ian
IoC & DI
- Spring container(IoC, Inversion of Control)
- spring framework 안에서 사용하는 자바 객체를 어떻게 관리?
- Spring container가 객체를 singleton패턴으로 관리하고, 필요한 순간에 사용 가능토록 주입(Dependency Injection)
- IoC example
@Configuration @Service @Repository @Controller @Bean ...etc
- DI example
@Autowired : "spring container야, 내가 원하는 객체를 사용 가능하도록, 주입해줘"