{"id":22992,"date":"2023-04-20T23:17:29","date_gmt":"2023-04-20T14:17:29","guid":{"rendered":"https:\/\/m9js.shop\/blog\/?p=22992"},"modified":"2023-04-29T18:48:18","modified_gmt":"2023-04-29T09:48:18","slug":"how-to-use-filters-and-routing-in-spring-cloud-gateway","status":"publish","type":"post","link":"https:\/\/m9js.shop\/blog\/development\/how-to-use-filters-and-routing-in-spring-cloud-gateway","title":{"rendered":"Spring Cloud Gateway\uc758 \ud544\ud130\uc640 \ub77c\uc6b0\ud305 \uc0ac\uc6a9\ubc95"},"content":{"rendered":"

Spring Cloud Gateway\ub294 \ub9c8\uc774\ud06c\ub85c\uc11c\ube44\uc2a4 \uc544\ud0a4\ud14d\ucc98\ub97c \uc704\ud55c API \uac8c\uc774\ud2b8\uc6e8\uc774\ub85c, \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc744 \ud1b5\ud574 \uc694\uccad\uc744 \ucc98\ub9ac\ud569\ub2c8\ub2e4. \uc774\ubc88 \uae00\uc5d0\uc11c\ub294 Spring Cloud Gateway\uc758 \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc5d0 \ub300\ud574 \uc790\uc138\ud788 \uc54c\uc544\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.<\/p>\n

Spring Cloud Gateway \ud544\ud130<\/h2>\n

Spring Cloud Gateway \ud544\ud130\ub294 \uc694\uccad\uacfc \uc751\ub2f5\uc744 \ubcc0\ud658\ud558\uac70\ub098, \ubcf4\uc548, \ub85c\uae45, \ud2b8\ub798\ud53d \uc81c\ud55c \ub4f1\uc758 \ucd94\uac00 \uae30\ub2a5\uc744 \uc218\ud589\ud558\ub294 \uc5ed\ud560\uc744 \ud569\ub2c8\ub2e4. \ud544\ud130\ub294 \uc21c\uc11c\ub300\ub85c \uc801\uc6a9\ub418\uba70, \ud544\ud130 \uccb4\uc778\uc758 \ub2e4\uc74c \ud544\ud130\ub85c \uc694\uccad\uc744 \uc804\ub2ec\ud558\uac70\ub098, \uc694\uccad\uc744 \ucc98\ub9ac\ud558\uc9c0 \uc54a\uace0 \uc751\ub2f5\uc744 \ubc18\ud658\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

Spring Cloud Gateway\ub294 \ub2e4\uc591\ud55c \ud544\ud130\ub97c \uc81c\uacf5\ud558\uba70, \uac1c\ubc1c\uc790\uac00 \ud544\ud130\ub97c \uc9c1\uc811 \uad6c\ud604\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4, \uc544\ub798\uc758 \ucf54\ub4dc\ub294 \uc694\uccad URL\uc5d0 "hello"\uac00 \ud3ec\ud568\ub418\uc5b4 \uc788\uc744 \uacbd\uc6b0 "Hello, world!"\ub97c \ubc18\ud658\ud558\ub294 \ud544\ud130\ub97c \uad6c\ud604\ud55c \uc608\uc2dc\uc785\ub2c8\ub2e4.<\/p>\n

public class HelloFilter implements GatewayFilter {\n    @Override\n    public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {\n        if (exchange.getRequest().getURI().toString().contains(\"hello\")) {\n            ServerHttpResponse response = exchange.getResponse();\n            response.setStatusCode(HttpStatus.OK);\n            response.getHeaders().add(\"Content-Type\", \"text\/plain\");\n            return response.writeWith(Mono.just(response.bufferFactory().wrap(\"Hello, world!\".getBytes())));\n        }\n        return chain.filter(exchange);\n    }\n}<\/code><\/pre>\n

Spring Cloud Gateway \ub77c\uc6b0\ud305<\/h2>\n

Spring Cloud Gateway \ub77c\uc6b0\ud305\uc740 \uc694\uccad\uc744 \ucc98\ub9ac\ud560 \ub300\uc0c1 \uc11c\ube44\uc2a4\ub97c \uc120\ud0dd\ud558\uace0, \uc694\uccad\uc744 \ud574\ub2f9 \uc11c\ube44\uc2a4\ub85c \uc804\ub2ec\ud558\ub294 \uc5ed\ud560\uc744 \ud569\ub2c8\ub2e4. \ub77c\uc6b0\ud305\uc740 \uc694\uccad URI, HTTP \uba54\uc18c\ub4dc, \ud5e4\ub354 \ub4f1 \ub2e4\uc591\ud55c \uc870\uac74\uc744 \uae30\ubc18\uc73c\ub85c \uc218\ud589\ub429\ub2c8\ub2e4.<\/p>\n

Spring Cloud Gateway\ub294 \ub2e4\uc591\ud55c \ub77c\uc6b0\ud305 \uae30\ub2a5\uc744 \uc81c\uacf5\ud558\uba70, \uac1c\ubc1c\uc790\uac00 \ub77c\uc6b0\ud305\uc744 \uc9c1\uc811 \uc124\uc815\ud560 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4, \uc544\ub798\uc758 \ucf54\ub4dc\ub294 \uc694\uccad URI\uac00 "\/api"\ub85c \uc2dc\uc791\ud560 \uacbd\uc6b0 "http:\/\/localhost:8080″\uc73c\ub85c<\/a> \ub77c\uc6b0\ud305\ud558\ub294 \uc608\uc2dc\uc785\ub2c8\ub2e4.<\/p>\n

spring:\n  cloud:\n    gateway:\n      routes:\n        - id: api\n          uri: http:\/\/localhost:8080\n          predicates:\n            - Path=\/api\/**<\/code><\/pre>\n

Spring Cloud Gateway \ud544\ud130\uc640 \ub77c\uc6b0\ud305 \uc0ac\uc6a9\ubc95<\/h2>\n

Spring Cloud Gateway \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc740 \uac1c\ubc1c\uc790\uac00 \uc694\uccad\uc744 \ucc98\ub9ac\ud558\ub294\ub370 \uc911\uc694\ud55c \uc5ed\ud560\uc744 \ud569\ub2c8\ub2e4. \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc744 \uc870\ud569\ud558\uc5ec, \uc694\uccad\uc5d0 \ub300\ud55c \ubcf4\uc548, \ub85c\uae45, \ud2b8\ub798\ud53d \uc81c\ud55c \ub4f1 \ub2e4\uc591\ud55c \ucd94\uac00 \uae30\ub2a5\uc744 \uc218\ud589\ud558\uace0, \uc694\uccad\uc744 \uc801\uc808\ud55c \uc11c\ube44\uc2a4\ub85c \uc804\ub2ec\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

Spring Cloud Gateway\ub294 \ub2e4\uc591\ud55c \uae30\ub2a5\uc744 \uc81c\uacf5\ud558\uba70, \uc774\ub97c \uc870\ud569\ud558\uc5ec \uc720\uc5f0\ud558\uace0 \uc548\uc815\uc801\uc778 API \uac8c\uc774\ud2b8\uc6e8\uc774\ub97c \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uac1c\ubc1c\uc790\ub294 \ud544\uc694\uc5d0 \ub530\ub77c \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc744 \uad6c\ud604\ud558\uace0, API \uac8c\uc774\ud2b8\uc6e8\uc774\ub97c \ud6a8\uacfc\uc801\uc73c\ub85c \uad00\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n

Spring Cloud Gateway\uc758 \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc744 \ud1b5\ud574, \uac1c\ubc1c\uc790\ub294 API \uac8c\uc774\ud2b8\uc6e8\uc774\ub97c \ub354\uc6b1 \uc720\uc5f0\ud558\uace0 \uc548\uc815\uc801\uc73c\ub85c \uad6c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ud544\ud130\uc640 \ub77c\uc6b0\ud305\uc744 \uc801\uc808\ud788 \ud65c\uc6a9\ud558\uc5ec, \ubcf4\uc548, \ub85c\uae45, \ud2b8\ub798\ud53d \uc81c\ud55c \ub4f1 \ub2e4\uc591\ud55c \ucd94\uac00 \uae30\ub2a5\uc744 \uc218\ud589\ud558\uace0, \uc694\uccad\uc744 \uc801\uc808\ud55c \uc11c\ube44\uc2a4\ub85c \uc804\ub2ec\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. Spring Cloud Gateway\ub97c \uc0ac\uc6a9\ud558\uc5ec, \ub9c8\uc774\ud06c\ub85c\uc11c\ube44\uc2a4 \uc544\ud0a4\ud14d\ucc98\ub97c \ubcf4\ub2e4 \ud6a8\uacfc\uc801\uc73c\ub85c \uad00\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"

Spring Cloud Gateway\uc758 \ud544\ud130\uc640 \ub77c\uc6b0\ud305 \uc0ac\uc6a9\ubc95<\/p>\n","protected":false},"author":1,"featured_media":12633,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[2082,2468,2156],"class_list":["post-22992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-cloud","tag-gateway","tag-spring"],"acf":[],"_links":{"self":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/22992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/comments?post=22992"}],"version-history":[{"count":0,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/22992\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media\/12633"}],"wp:attachment":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media?parent=22992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/categories?post=22992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/tags?post=22992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}