SpringCloudGateway动态路由配置详解
路由
gateway最主要的作用是,提供统一的入口,路由,鉴权,限流,熔断;这里的路由就是请求的转发,根据设定好的某些条件,比如断言,进行转发。
动态
动态的目的是让程序更加可以在运行的过程中兼容更多的业务场景。
涉及到两个服务,一个是门户服务(作用是提供给运营人员管理入口--包括:管理路由、绑定路由),一个是网关服务(gateway组件,为门户服务提供:查询路由信息、添加路由、删除路由、编辑路由接口)。
路由模型实体类
@Data
public 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动态路由配置详解的详细内容,更多关于springCloud Gateway动态路由的资料请关注其它相关文章!
相关文章