您好,登錄后才能下訂單哦!
這篇“SpringCloud Gateway動態路由怎么配置”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“SpringCloud Gateway動態路由怎么配置”文章吧。
gateway最主要的作用是,提供統一的入口,路由,鑒權,限流,熔斷;這里的路由就是請求的轉發,根據設定好的某些條件,比如斷言,進行轉發。
動態的目的是讓程序更加可以在運行的過程中兼容更多的業務場景。
涉及到兩個服務,一個是門戶服務(作用是提供給運營人員管理入口--包括:管理路由、綁定路由),一個是網關服務(gateway組件,為門戶服務提供:查詢路由信息、添加路由、刪除路由、編輯路由接口)。
/** * 路由模型 * * @author 苦瓜不苦 * @date 2022/11/13 23:23 **/ @Data public class RouteDefinitionObject { /** * 路由的Id */ private String id; /** * 路由斷言集合配置 */ private List<PredicateDefinition> predicates; /** * 路由過濾器集合配置 */ private List<FilterDefinition> filters; /** * 路由規則轉發的目標uri */ private String uri; /** * 路由執行的順序 */ private Integer order = 0; @Data public static class PredicateDefinition { /** * 斷言對應的Name */ private String name; /** * 配置的斷言規則 */ private Map<String, String> args; } @Data public static class FilterDefinition { /** * 斷言對應的Name */ private String name; /** * 配置的斷言規則 */ private Map<String, String> args; } }
/** * 動態路由配置 * * @author 苦瓜不苦 * @date 2022/11/13 23:26 **/ @Slf4j @RestController @RequestMapping("/route") public class GatewayRouteConfig implements ApplicationEventPublisherAware { public final Map<String, RouteDefinitionObject> MAP = new ConcurrentHashMap<>(); private final RouteDefinitionWriter routeDefinitionWriter; private ApplicationEventPublisher applicationEventPublisher; public GatewayRouteConfig(RouteDefinitionWriter routeDefinitionWriter) { this.routeDefinitionWriter = routeDefinitionWriter; } @Override public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) { this.applicationEventPublisher = applicationEventPublisher; } /** * 新增路由 * {"filters":[{"name":"StripPrefix","args":{"_genkey_0":"2"}}],"id":"item-system","uri":"lb://item-system","order":0,"predicates":[{"name":"Path","args":{"_genkey_0":"/api/system/**"}}]} */ @PostMapping("/save") public R<String> save(@RequestBody RouteDefinitionObject routeDefinitionObject) { try { // 路由模型轉換成路由對象 RouteDefinition definition = BeanUtil.toBean(routeDefinitionObject, RouteDefinition.class); URI uri = routeDefinitionObject.getUri().startsWith("http") ? UriComponentsBuilder.fromHttpUrl(routeDefinitionObject.getUri()).build().toUri() : URI.create(routeDefinitionObject.getUri()); definition.setUri(uri); // 新增 routeDefinitionWriter.save(Mono.just(definition)).subscribe(); applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this)); MAP.put(routeDefinitionObject.getId(), routeDefinitionObject); return R.success(); } catch (Exception e) { log.error("\n", e); } return R.failed(); } /** * 刪除路由 */ @GetMapping("/delete") public R<Mono<ResponseEntity<Object>>> delete(@RequestParam("id") String id) { try { Mono<ResponseEntity<Object>> responseEntityMono = routeDefinitionWriter.delete(Mono.just(id)) .then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build()) )).onErrorResume((t) -> t instanceof NotFoundException , (t) -> Mono.just(ResponseEntity.notFound().build()) ); MAP.remove(id); return R.success(responseEntityMono); } catch (Exception e) { log.error("\n", e); } return R.failed(); } /** * 更新路由 * {"filters":[{"name":"StripPrefix","args":{"_genkey_0":"2"}}],"id":"item-system","uri":"lb://item-system","order":0,"predicates":[{"name":"Path","args":{"_genkey_0":"/api/system/**"}}]} */ @PostMapping("/update") public R<String> update(@RequestBody RouteDefinitionObject routeDefinitionObject) { try { // 刪除 delete(routeDefinitionObject.getId()); // 新增 save(routeDefinitionObject); return R.success(); } catch (Exception e) { log.error("\n", e); } return R.failed(); } /** * 查詢路由 */ @GetMapping("/list") public R<Collection<RouteDefinitionObject>> list() { return R.success(MAP.values()); } }
{ "filters":[ { "name":"StripPrefix", "args":{ "_genkey_0":"2" } } ], "id":"item-system", "uri":"lb://item-system", "order":0, "predicates":[ { "name":"Path", "args":{ "_genkey_0":"/api/system/**" } } ] }
以上就是關于“SpringCloud Gateway動態路由怎么配置”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。