ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Spring 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
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ