-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
## λ°μ΄ν°λ² μ΄μ€ 컀λ₯μ ν | ||
- μ ν리μΌμ΄μ λ‘λ© μμ μ Connection κ°μ²΄λ₯Ό 미리 μμ±νκ³ , μ ν리μΌμ΄μ μμ λ°μ΄ν°λ² μ΄μ€μ μ°κ²°μ΄ νμν κ²½μ° λ―Έλ¦¬ μ€λΉλ Connection κ°μ²΄λ₯Ό μ¬μ©νλ€ | ||
- DB μ°κ²° μ TCP/IP νΈλμ °μ΄ν¬μ κ°μ μ¬λ¬ λ¨κ³λ₯Ό κ±°μ³μΌ νλ€. λ°μ΄ν°λ² μ΄μ€ 컀λ₯μ νμ μ¬μ©νμ¬ DB μ°κ²°μ μμ μκ°μ μ μ½νκ³ μ±λ₯μ ν₯μμν¬ μ μλ€ | ||
|
||
### λ°μ΄ν°λ² μ΄μ€ 컀λ₯μ νμ λμ λ°©μ | ||
1. μ ν리μΌμ΄μ μλ²κ° μμλ λ μΌμ μμ 컀λ₯μ μ 미리 μμ±νλ€ | ||
2. μ ν리μΌμ΄μ μμ²μ λ°λΌ μμ±λ 컀λ₯μ κ°μ²΄λ₯Ό μ λ¬νλ€ | ||
3. μΌμ μ μ΄μμ 컀λ₯μ μ΄ μ¬μ©λλ©΄ μλ‘μ΄ μ»€λ₯μ μ λ§λ λ€ | ||
4. μ¬μ©νμ§ μλ 컀λ₯μ μ μ’ λ£νκ³ μ΅μνμ κΈ°λ³Έ 컀λ₯μ μ μ μ§νλ€ | ||
|
||
![image](https://github.com/user-attachments/assets/96b03f43-c047-42d8-babd-09c25d81adc6) | ||
|
||
### 컀λ₯μ νμ μ₯μ | ||
- 컀λ₯μ κ°μ²΄λ₯Ό 미리 λ§λ€μ΄ μ°κ²°νμ¬ λ©λͺ¨λ¦¬ μμ λ±λ‘ν΄λκΈ° λλ¬Έμ ν΄λΌμ΄μΈνΈκ° λΉ λ₯΄κ² DBμ μ μν μ μλ€ | ||
- DB 컀λ₯μ μλ₯Ό μ νν¨μΌλ‘μ¨ κ³Όλν μ μμ λ°©μ§ν μ μλ€ | ||
- DB μ μ λͺ¨λμ 곡ν΅ννμ¬ DB μλ² νκ²½μ΄ λ³ν κ²½μ° λΉκ΅μ μ¬μ΄ μ μ§λ³΄μκ° κ°λ₯νλ€ | ||
- μ°κ²°μ΄ λλ 컀λ₯μ μ μ¬μ¬μ©ν¨μΌλ‘μ¨ μλ‘ κ°μ²΄λ₯Ό μμ±νλ λΉμ©μ μ€μΌ μ μλ€ | ||
|
||
## Hikari CP | ||
- λ°μ΄ν°λ² μ΄μ€ μ°κ²°(Connection)μ κ΄λ¦¬ν΄ μ£Όλ λꡬ(λΌμ΄λΈλ¬λ¦¬) | ||
- 컀λ₯μ ν(Connection Pool)μ΄Β μ€μ λ 컀λ₯μ μ μ¬μ΄μ¦λ§νΌμ μ°κ²°μ νμ©νλ©° HTTP μμ²μ λν΄ μμ°¨μ μΌλ‘ DB 컀λ₯μ μ μ²λ¦¬ν΄ μ£Όλ κΈ°λ₯μ μννλ€ | ||
|
||
- μλ°: κΈ°λ³Έμ μΌλ‘Β `DataSource`Β μΈν°νμ΄μ€λ₯Ό μ¬μ©νμ¬ μ»€λ₯μ νμ κ΄λ¦¬νλ€ | ||
- μ€νλ§: SpringBoot 2.0 μ΄μ μλΒ tomcat-jdbcλ₯Ό μ¬μ©νλ€κ° 2.0 μ΄ν λΆν°λΒ Hikari CPλ₯Ό κΈ°λ³Έ μ΅μ μΌλ‘ μ±ννλ€ | ||
- Hikari CP μ±λ₯μ΄ μλ±ν μ’λ€ | ||
|
||
![image](https://github.com/user-attachments/assets/02ea8f4a-20e4-4b9c-966c-b49df0e028bf) | ||
|
||
### Hikari CPμ λμ λ°©μ | ||
1. Threadκ° Connectionμ μμ²νλ©΄ Connection Poolμ κ°μμ λ°©μμ λ°λΌ μ ν΄ Connectionμ μ°Ύμμ λ°ννλ€ | ||
- Hikari CPμ κ²½μ°, μ΄μ μ μ¬μ©νλ Connectionμ΄ μ‘΄μ¬νλμ§ νμΈνκ³ , μ΄λ₯Ό μ°μ μ μΌλ‘ λ°ννλ€ | ||
|
||
![image](https://github.com/user-attachments/assets/3ccc54a0-de34-4bb1-a375-bd8bc8508972) | ||
|
||
2. κ°λ₯ν Connectionμ΄ μ‘΄μ¬νμ§ μμΌλ©΄, HandOffQueueλ₯Ό Pollingνλ©΄μ λ€λ₯Έ Threadκ° Connectionμ λ°λ©νκΈ°λ₯Ό κΈ°λ€λ¦°λ€. λ§μ½ TimeOut μκ°μ΄ λ§λ£λλ©΄ μμΈλ₯Ό λμ§λ€ | ||
|
||
![image](https://github.com/user-attachments/assets/53123ad7-5d56-426b-9a3b-0678af76e9f2) | ||
|
||
3. μ΅μ’ μ μΌλ‘ μ¬μ©ν Connectionμ λ°λ©νλ©΄ Connection Poolμ΄ Connection μ¬μ© λ΄μμ κΈ°λ‘νκ³ , HandOffQueueμ λ°λ©λ Connectionμ μ½μ νλ€. HandOffQueueλ₯Ό Pollingνλ Threadλ Connectionμ νλνκ³ μμ μ μ΄μ΄λκ°λ€ | ||
|
||
![image](https://github.com/user-attachments/assets/51b8f94a-bb0c-416c-b837-a8657dc14168) | ||
|
||
## λ°λλ½ νΌνκΈ° | ||
- 컀λ₯μ νμ ν¬κ² μ€μ νλ©΄? β λ©λͺ¨λ¦¬ μλͺ¨κ° ν° λμ λ§μ μ¬λμ λκΈ°μκ°μ΄ μ€μ΄ λ λ€ | ||
- 컀λ₯μ νμ μκ² μ€μ νλ©΄? β μ¬μ©μμ λκΈ°μκ°μ΄ κΈΈμ΄μ§λ€ | ||
|
||
- 컀λ₯μ ν μ¬μ΄μ¦λ₯Ό μ§μ ν λλ νμ λ°λλ½μ κ³ λ €ν΄μΌ νλ€ | ||
- [μ°μν νμ λ€](https://techblog.woowahan.com/2664/)μμ μ μν μ΄λ‘ μ μΌλ‘ νμν μ΅μνμ 컀λ₯μ ν μ¬μ΄μ¦λ λ€μκ³Ό κ°λ€ | ||
``` | ||
PoolSize = Tn Γ ( Cm -1 ) + 1 | ||
- Tn : μ 체 Thread κ°―μ | ||
- Cm : νλμ Taskμμ λμμ νμν Connection μ | ||
``` | ||
|
||
- μμ κ²½μ°λ λ°λλ½μ νΌν μ μμ§λ§, μ¬μ 컀λ₯μ μ΄ +1 κ° μ΄λ―λ‘ μ±λ₯μ μ’μ§ λͺ»ν μ μλ€. 컀λ₯μ νμ λμ± μ¬μ λ₯Ό μ£Όκ³ μΆλ€λ©΄ μλμ κ°μ΄ ν΄λ³΄μ | ||
``` | ||
PoolSize = Tn Γ ( Cm - 1 ) + ( Tn / 2 ) | ||
- thread count : 16 | ||
- simultaneous connection count : 2 | ||
- pool size : 16 * ( 2 β 1 ) + (16 / 2) = 24 | ||
``` | ||
|
||
## νκ²½ μ€μ | ||
1. μμ‘΄μ± μΆκ° | ||
```java | ||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' | ||
} | ||
``` | ||
|
||
2. μ€μ μΆκ° | ||
```java | ||
@Configuration | ||
public class DataSourceConfiguration { | ||
|
||
@Bean | ||
@Primary | ||
@ConfigurationProperties("spring.datasource") | ||
public DataSourceProperties dataSourceProperties() { | ||
return new DataSourceProperties(); | ||
} | ||
|
||
@Bean | ||
@ConfigurationProperties("spring.datasource.hikari") | ||
public HikariDataSource dataSource(DataSourceProperties properties) { | ||
return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); | ||
} | ||
} | ||
``` | ||
|
||
3. property μ μ | ||
```yaml | ||
spring: | ||
datasource: | ||
url: jdbc:mysql://${MYSQL_URL}:${MYSQL_PORT}/${MYSQL_SCHEMA} | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
username: ${MYSQL_USERNAME} | ||
password: ${MYSQL_PASSWORD} | ||
hikari: | ||
connection-timeout: 3000 | ||
validation-timeout: 3000 | ||
minimum-idle: 5 | ||
max-lifetime: 240000 | ||
maximum-pool-size: 20 | ||
``` | ||
- `maximum-pool-size`: μ΅λ pool size (defailt 10) | ||
- `connection-timeout`: 컀λ₯μ μ νμμμ | ||
- `validation-timeout`: μ ν¨ν νμμμ | ||
- `minimum-idle`: μ°κ²° νμμ HikariCPκ° μ μ§ κ΄λ¦¬νλ μ΅μ μ ν΄ μ°κ²° μ | ||
- `idle-timeout`: μ°κ²°μ μν μ΅λ μ ν΄ μκ° | ||
- `max-lifetime`: λ«ν ν pool μμλ connectionμ μ΅λ μλͺ (ms)μ λλ€. | ||
- `auto-commit`: auto commit μ¬λΆ (default true) | ||
|
||
4. Hikari Poolμ΄ μ λλ‘ λμνκ³ μλμ§ λ‘κΉ λ 벨 μ€μ | ||
```yaml | ||
logging: | ||
level: | ||
org.hibernate.SQL: info | ||
root: info | ||
com.zaxxer.hikari.pool.HikariPool: debug | ||
``` | ||
|
||
5. μ€ν κ²°κ³Ό | ||
![image](https://github.com/user-attachments/assets/ab18fd8c-f5f6-4b7e-9964-0afd0111d2fc) |