ํฐ์คํ ๋ฆฌ ๋ทฐ
spring๐
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
๋งํฌ
TAG
- nestjs typeorm
- Spring Async
- JavaScript
- ๊ธฐ์์ฒญAPI
- nestjs config
- nestjs project
- android
- @nestjs/config
- ์๊ณ ๋ฆฌ์ฆ
- nestjs doc
- nestjs module
- DeferredResult
- sequelize
- typeorm
- Request_Limit_Exceeded
- Promise bulk
- foreignkey
- nestjs configService
- docker mysql
- NestJS
- typeorm ์ฐ๊ฒฐ
- backend-framework
- ํ๋ก๊ทธ๋๋จธ์ค
- Spring
- ๋น๋๊ธฐ ์์ฒญ
- nestjs directory
- node.js backend
- node.js
- Promise error
- nestjs/cli
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
๊ธ ๋ณด๊ดํจ