mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-11 05:19:00 +08:00
✨ 添加新特性。gateway 动态路由
This commit is contained in:
1 parent
5dc2482a2a
commit
d7b4cbfa60
9 files changed
+175
-103
No files matched your search
+14
-26
@@ -17,6 +17,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.gateway.support;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -49,37 +50,24 @@ public class DynamicRouteEndpoint {
|
||||
return dynamicRouteHandler.routes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增路由
|
||||
*
|
||||
* @param route 路由定义
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
public Mono<R> save(@RequestBody Mono<RouteDefinition> route) {
|
||||
return Mono.just(new R(dynamicRouteHandler.addRoute(route)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除路由
|
||||
*
|
||||
* @param id 路由定义ID
|
||||
* @return
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public Mono<R> delete(@PathVariable String id) {
|
||||
return Mono.just(new R(dynamicRouteHandler.deleteRoute(id)));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改路由
|
||||
*
|
||||
* @param routeDefinition 路由定义
|
||||
* @param routes 路由定义
|
||||
* @return
|
||||
*/
|
||||
@PutMapping
|
||||
public R edit(@RequestBody RouteDefinition routeDefinition) {
|
||||
return new R();
|
||||
public R edit(@RequestBody JSONObject routes) {
|
||||
return new R(dynamicRouteHandler.editRoutes(routes));
|
||||
}
|
||||
|
||||
/**
|
||||
* 回滚初始化配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/fallback")
|
||||
public Mono<R> fallback() {
|
||||
return Mono.just(new R(dynamicRouteHandler.fallback()));
|
||||
}
|
||||
}
|
||||
+68
-11
@@ -17,16 +17,25 @@
|
||||
|
||||
package com.pig4cloud.pigx.gateway.support;
|
||||
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
||||
import com.pig4cloud.pigx.gateway.support.vo.RouteDefinitionVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
||||
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -39,13 +48,13 @@ import java.util.Map;
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DynamicRouteHandler implements ApplicationEventPublisherAware {
|
||||
private final RouteDefinitionWriter routeDefinitionWriter;
|
||||
private final RouteDefinitionLocator routeDefinitionLocator;
|
||||
private final RedisTemplate redisTemplate;
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
public DynamicRouteHandler(RouteDefinitionWriter routeDefinitionWriter
|
||||
public DynamicRouteHandler(RedisTemplate redisTemplate
|
||||
, RouteDefinitionLocator routeDefinitionLocator) {
|
||||
this.routeDefinitionWriter = routeDefinitionWriter;
|
||||
this.redisTemplate = redisTemplate;
|
||||
this.routeDefinitionLocator = routeDefinitionLocator;
|
||||
}
|
||||
|
||||
@@ -63,19 +72,67 @@ public class DynamicRouteHandler implements ApplicationEventPublisherAware {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加路由
|
||||
* 更新路由信息
|
||||
*
|
||||
* @param route 路由定义
|
||||
* @param routes 路由信息
|
||||
* @return
|
||||
*/
|
||||
public Mono<Void> addRoute(Mono<RouteDefinition> route) {
|
||||
public Mono<Void> editRoutes(JSONObject routes) {
|
||||
Boolean result = redisTemplate.delete(CommonConstant.ROUTE_KEY);
|
||||
log.info("清空网关路由 {} ", result);
|
||||
|
||||
routes.forEach((key, value) -> {
|
||||
log.info("更新路由 -> {} ->{}", key, value);
|
||||
RouteDefinitionVo vo = new RouteDefinitionVo();
|
||||
Map<String, Object> map = (Map) value;
|
||||
|
||||
Object id = map.get("id");
|
||||
if (id != null) {
|
||||
vo.setId(String.valueOf(id));
|
||||
}
|
||||
|
||||
Object predicates = map.get("predicates");
|
||||
if (predicates != null) {
|
||||
JSONArray predicatesArray = (JSONArray) predicates;
|
||||
List<PredicateDefinition> predicateDefinitionList =
|
||||
predicatesArray.toList(PredicateDefinition.class);
|
||||
vo.setPredicates(predicateDefinitionList);
|
||||
}
|
||||
|
||||
Object filters = map.get("filters");
|
||||
if (filters != null) {
|
||||
JSONArray filtersArray = (JSONArray) filters;
|
||||
List<FilterDefinition> filterDefinitionList
|
||||
= filtersArray.toList(FilterDefinition.class);
|
||||
vo.setFilters(filterDefinitionList);
|
||||
}
|
||||
|
||||
Object uri = map.get("uri");
|
||||
if (uri != null) {
|
||||
vo.setUri(URI.create(String.valueOf(uri)));
|
||||
}
|
||||
|
||||
Object order = map.get("order");
|
||||
if (order != null) {
|
||||
vo.setOrder(Integer.parseInt(String.valueOf(order)));
|
||||
}
|
||||
|
||||
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
|
||||
redisTemplate.opsForHash().put(CommonConstant.ROUTE_KEY, key, vo);
|
||||
});
|
||||
log.debug("更新网关路由结束 ");
|
||||
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
return routeDefinitionWriter.save(route);
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
public Mono<Void> deleteRoute(String id) {
|
||||
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
return routeDefinitionWriter.delete(Mono.just(id));
|
||||
/**
|
||||
* 回滚路由
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Mono<Void> fallback() {
|
||||
this.applicationEventPublisher.publishEvent(new DynamicRouteInitEvent(this));
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.gateway.support;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/11/5
|
||||
* <p>
|
||||
* 路由初始化事件
|
||||
*/
|
||||
public class DynamicRouteInitEvent extends ApplicationEvent {
|
||||
public DynamicRouteInitEvent(Object source) {
|
||||
super(source);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -48,7 +48,7 @@ public class DynamicRouteInitRunner {
|
||||
|
||||
@Async
|
||||
@Order
|
||||
@EventListener(WebServerInitializedEvent.class)
|
||||
@EventListener({WebServerInitializedEvent.class, DynamicRouteInitEvent.class})
|
||||
public void initRoute() {
|
||||
Boolean result = redisTemplate.delete(CommonConstant.ROUTE_KEY);
|
||||
log.info("初始化网关路由 {} ", result);
|
||||
|
||||
Reference in new issue
Block a user