这篇“SpringCloud Gateway动态路由怎么配置”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringCloud Gateway动态路由怎么配置”文章吧。
路由
gateway最主要的作用是,提供统一的入口,路由,鉴权,限流,熔断;这里的路由就是请求的转发,根据设定好的某些条件,比如断言,进行转发。
动态
动态的目的是让程序更加可以在运行的过程中兼容更多的业务场景。
涉及到两个服务,一个是门户服务(作用是提供给运营人员管理入口--包括:管理路由、绑定路由),一个是网关服务(gateway组件,为门户服务提供:查询路由信息、添加路由、删除路由、编辑路由接口)。
路由模型实体类
@Datapublic class RouteDefinitionObject { private String id; private List<PredicateDefinition> predicates; private List<FilterDefinition> filters; private String uri; private Integer order = 0; @Data public static class PredicateDefinition { private String name; private Map<String, String> args; } @Data public static class FilterDefinition { private String name; private Map<String, String> args; }}
动态路径配置
@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; } @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(); } @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()); }}
路由模型JSON数据
{ "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动态路由怎么配置”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网行业资讯频道。