ํฐ์คํ ๋ฆฌ ๋ทฐ
Spring boot์์ Http request ๋น๋๊ธฐ ์์ฒญํ๊ธฐ
๋๋์ด๋ฐ๐พ 2023. 10. 26. 17:04Spring boot๋ก http request ์ฝ๋๋ฅผ ๊ฐ๋ฐํ ๋ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ก ์์ฒญ ๋ฐ ์ฒ๋ฆฌํ๊ธฐ์ํ ๋ด์ฉ์ ๋๋ค.
๋๊ธฐ๋ฐฉ์์ผ๋ก ํธ์ถํ๊ฒ๋๋ฉด ์์ ํธ์ถ๋ request์ ์๋ต์ด ์๋ฃ๋ ๋๊น์ง ๋ค์์์ฒญ์ด ์คํ๋์ง ๋ชปํ๊ฒ๋๋ฏ๋ก
์ฌ๋ฌ๊ฐ์ request๋ฅผ ๋ณด๋ด๋ ์ํฉ์์๋ ๋ ๋์ ํจ์จ์ ๋ณด์ฌ์ค๋๋ค.
์ด ๊ธ์์ http ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ okhttp3 ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
build.gradle์ ์์กด์ฑ ์ถ๊ฐ
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
์ฝ๋์์ฑ (๋น๋๊ธฐ ์์ฒญ)
public class Helloworld {
private void asyncOkHttp() {
// http ์์ฒญํ๋ ค๋ URL ๋ฆฌ์คํธ๊ฐ ์๋ค๊ณ ๊ฐ์
List<String> requestUrls = new ArrayList<>();
requestUrls.add("https://111...");
requestUrls.add("https://222...");
requestUrls.add("https://333...");
OkHttpClient client = new OkHttpClient().newBuilder().build();
for (String url : requestUrls) {
Request request = new Request.Builder()
.url(url)
.method("GET", null)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// request ์์ฒญ ์คํจ ์๋ต์ ๋ํ ์ฒ๋ฆฌ
}
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
// request ์์ฒญ ์ฑ๊ณต ์๋ต์ ๋ํ ์ฒ๋ฆฌ
}
}
});
}
}
}
(์ฐธ๊ณ ๋ก ๋๊ธฐ ์์ฒญ์ enqueue ๋ฉ์๋ ๋์ execute ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.)
์ถ๊ฐ๋ก Request_Limit_Exceeded ๋ผ๊ณ ํด์ ๋์ผํ IP์์ ๋๋ฌด ์งง์ ๊ฐ๊ฒฉ์ผ๋ก Request๋ฅผ ์์ฒญํ ์์ ์์ฒญ์์ฒด๋ฅผ ๊ฑฐ๋ถํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ ํด๋น ์ํฉ์ ํด๊ฒฐํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ถ๋ถ๋ ๊ณต์ ํฉ๋๋ค.
์ฝ๋์์ฑ (๋น๋๊ธฐ ์์ฒญ + Dispatcher)
public class Helloworld {
private void asyncOkHttp() {
// http ์์ฒญํ๋ ค๋ URL ๋ฆฌ์คํธ๊ฐ ์๋ค๊ณ ๊ฐ์
List<String> requestUrls = new ArrayList<>();
requestUrls.add("https://111...");
requestUrls.add("https://222...");
requestUrls.add("https://333...");
// dispatcher๋ฅผ ์์ฑํ์ฌ ์ต๋ ํธ์ถ ์๋ฅผ ์ ์ด
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(50);
OkHttpClient client = new OkHttpClient().newBuilder().dispatcher(dispatcher).build();
for (String url : requestUrls) {
Request request = new Request.Builder()
.url(url)
.method("GET", null)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// request ์์ฒญ ์คํจ ์๋ต์ ๋ํ ์ฒ๋ฆฌ
}
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
// request ์์ฒญ ์ฑ๊ณต ์๋ต์ ๋ํ ์ฒ๋ฆฌ
}
}
});
}
}
}
์ฌ์ฉ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ค๋ฅด์ง ์๊ณ client ๊ฐ์ฒด build์์ dispatcher๋ฅผ ์ถ๊ฐํด์ค ์ฑ๋ก ์์ฑํด์ฃผ๋ฉด ๋ฉ๋๋ค. ๊ฐ ๋์คํจ์ฒ๋ ExecutorService๋ฅผ ์ฌ์ฉํ์ฌ ๋ด๋ถ์ ์ผ๋ก ํธ์ถ์ ์คํํฉ๋๋ค.
'spring๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC์ ๋น๋๊ธฐ์ฒ๋ฆฌ (ThreadPoolTaskExecutor ์ฌ์ฉ) (0) | 2023.08.22 |
---|
- Total
- Today
- Yesterday
- ์๊ณ ๋ฆฌ์ฆ
- nestjs typeorm
- Promise bulk
- @nestjs/config
- backend-framework
- nestjs project
- nestjs module
- nestjs config
- nestjs/cli
- node.js
- foreignkey
- Request_Limit_Exceeded
- node.js backend
- NestJS
- DeferredResult
- Promise error
- docker mysql
- ํ๋ก๊ทธ๋๋จธ์ค
- android
- typeorm
- nestjs directory
- ๊ธฐ์์ฒญAPI
- Spring Async
- ๋น๋๊ธฐ ์์ฒญ
- typeorm ์ฐ๊ฒฐ
- Spring
- sequelize
- JavaScript
- nestjs configService
- nestjs doc
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |