mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-26 16:26:41 +08:00
后台目录结构大调整,让结构更清晰
This commit is contained in:
@ -0,0 +1,57 @@
|
||||
package org.jeecg;
|
||||
|
||||
import org.jeecg.loader.DynamicRouteLoader;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
||||
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
|
||||
|
||||
/**
|
||||
* @author jeecg
|
||||
*/
|
||||
@EnableFeignClients
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class JeecgGatewayApplication implements CommandLineRunner {
|
||||
@Resource
|
||||
private DynamicRouteLoader dynamicRouteLoader;
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext applicationContext = SpringApplication.run(JeecgGatewayApplication.class, args);
|
||||
//String userName = applicationContext.getEnvironment().getProperty("jeecg.test");
|
||||
//System.err.println("user name :" +userName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 容器初始化后加载路由
|
||||
* @param strings
|
||||
*/
|
||||
@Override
|
||||
public void run(String... strings) {
|
||||
dynamicRouteLoader.refresh(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口地址(通过9999端口直接访问)
|
||||
*
|
||||
* @param indexHtml
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> indexRouter(@Value("classpath:/META-INF/resources/doc.html") final org.springframework.core.io.Resource indexHtml) {
|
||||
return route(GET("/"), request -> ok().contentType(MediaType.TEXT_HTML).syncBody(indexHtml));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
package org.jeecg.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author scott
|
||||
* @date 2020/05/26
|
||||
* 路由配置信息
|
||||
*/
|
||||
@Configuration
|
||||
@RefreshScope
|
||||
public class GatewayRoutersConfig {
|
||||
/**
|
||||
* 路由配置方式:database,yml,nacos
|
||||
*/
|
||||
public String dataType;
|
||||
public String serverAddr;
|
||||
public String namespace;
|
||||
public String dataId;
|
||||
public String routeGroup;
|
||||
public String username;
|
||||
public String password;
|
||||
|
||||
@Value("${spring.cloud.nacos.discovery.server-addr}")
|
||||
public void setServerAddr(String serverAddr) {
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
@Value("${spring.cloud.nacos.discovery.namespace:#{null}}")
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.data-id:#{null}}")
|
||||
public void setRouteDataId(String dataId) {
|
||||
this.dataId = dataId + ".json";
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.group:DEFAULT_GROUP:#{null}}")
|
||||
public void setRouteGroup(String routeGroup) {
|
||||
this.routeGroup = routeGroup;
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.data-type:#{null}}")
|
||||
public void setDataType(String dataType) {
|
||||
this.dataType = dataType;
|
||||
}
|
||||
|
||||
@Value("${spring.cloud.nacos.config.username:#{null}}")
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Value("${spring.cloud.nacos.config.password:#{null}}")
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getDataType() {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public String getRouteGroup() {
|
||||
return routeGroup;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
package org.jeecg.config;
|
||||
|
||||
import org.jeecg.filter.GlobalAccessTokenFilter;
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author scott
|
||||
* @date 2020/5/26
|
||||
* 路由限流配置
|
||||
*/
|
||||
@Configuration
|
||||
public class RateLimiterConfiguration {
|
||||
/**
|
||||
* IP限流 (通过exchange对象可以获取到请求信息,这边用了HostName)
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public KeyResolver ipKeyResolver() {
|
||||
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户限流 (通过exchange对象可以获取到请求信息,获取当前请求的用户 TOKEN)
|
||||
*/
|
||||
@Bean
|
||||
public KeyResolver userKeyResolver() {
|
||||
//使用这种方式限流,请求Header中必须携带X-Access-Token参数
|
||||
return exchange -> Mono.just(exchange.getRequest().getHeaders().getFirst(GlobalAccessTokenFilter.X_ACCESS_TOKEN));
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口限流 (获取请求地址的uri作为限流key)
|
||||
*/
|
||||
@Bean
|
||||
public KeyResolver apiKeyResolver() {
|
||||
return exchange -> Mono.just(exchange.getRequest().getPath().value());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.config;
|
||||
|
||||
/**
|
||||
* nocos配置方式枚举
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
public enum RouterDataType {
|
||||
/**
|
||||
* 数据库加载路由配置
|
||||
*/
|
||||
database,
|
||||
/**
|
||||
* 本地yml加载路由配置
|
||||
*/
|
||||
yml,
|
||||
/**
|
||||
* nacos加载路由配置
|
||||
*/
|
||||
nacos
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
//package org.jeecg.fallback;
|
||||
//
|
||||
//import org.springframework.web.bind.annotation.RequestMapping;
|
||||
//import org.springframework.web.bind.annotation.RestController;
|
||||
//import reactor.core.publisher.Mono;
|
||||
//
|
||||
///**
|
||||
// * 响应超时熔断处理器【升级springboot2.6.6后,此类作废】
|
||||
// *
|
||||
// * @author zyf
|
||||
// */
|
||||
//@RestController
|
||||
//public class FallbackController {
|
||||
//
|
||||
// /**
|
||||
// * 全局熔断处理
|
||||
// * @return
|
||||
// */
|
||||
// @RequestMapping("/fallback")
|
||||
// public Mono<String> fallback() {
|
||||
// return Mono.just("访问超时,请稍后再试!");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * demo熔断处理
|
||||
// * @return
|
||||
// */
|
||||
// @RequestMapping("/demo/fallback")
|
||||
// public Mono<String> fallback2() {
|
||||
// return Mono.just("访问超时,请稍后再试!");
|
||||
// }
|
||||
//}
|
||||
@ -0,0 +1,33 @@
|
||||
//package org.jeecg.fallback;
|
||||
//
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.http.HttpStatus;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//import org.springframework.web.reactive.function.BodyInserters;
|
||||
//import org.springframework.web.reactive.function.server.HandlerFunction;
|
||||
//import org.springframework.web.reactive.function.server.ServerRequest;
|
||||
//import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
//import reactor.core.publisher.Mono;
|
||||
//
|
||||
//import java.util.Optional;
|
||||
//
|
||||
//import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR;
|
||||
//
|
||||
///**
|
||||
// * @author scott
|
||||
// * @date 2020/05/26
|
||||
// * Hystrix 降级处理
|
||||
// */
|
||||
//@Slf4j
|
||||
//@Component
|
||||
//public class HystrixFallbackHandler implements HandlerFunction<ServerResponse> {
|
||||
// @Override
|
||||
// public Mono<ServerResponse> handle(ServerRequest serverRequest) {
|
||||
// Optional<Object> originalUris = serverRequest.attribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
|
||||
//
|
||||
// originalUris.ifPresent(originalUri -> log.error("网关执行请求:{}失败,hystrix服务降级处理", originalUri));
|
||||
//
|
||||
// return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
|
||||
// .header("Content-Type","text/plain; charset=utf-8").body(BodyInserters.fromObject("访问超时,请稍后再试"));
|
||||
// }
|
||||
//}
|
||||
@ -0,0 +1,45 @@
|
||||
package org.jeecg.fallback.sentinel;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
|
||||
import org.jeecg.common.enums.SentinelErrorInfoEnum;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @Description: 自定义Sentinel全局异常(需要启动Sentinel客户端)
|
||||
* @author: zyf
|
||||
* @date: 2022/02/18
|
||||
* @version: V1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class GatewaySentinelExceptionConfig {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
BlockRequestHandler blockRequestHandler = (serverWebExchange, ex) -> {
|
||||
String msg;
|
||||
SentinelErrorInfoEnum errorInfoEnum = SentinelErrorInfoEnum.getErrorByException(ex);
|
||||
if (ObjectUtil.isNotEmpty(errorInfoEnum)) {
|
||||
msg = errorInfoEnum.getError();
|
||||
} else {
|
||||
msg = "未知限流降级";
|
||||
}
|
||||
HashMap<String, String> map = new HashMap(5);
|
||||
map.put("code", HttpStatus.TOO_MANY_REQUESTS.toString());
|
||||
map.put("message", msg);
|
||||
//自定义异常处理
|
||||
return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(map));
|
||||
};
|
||||
|
||||
GatewayCallbackManager.setBlockHandler(blockRequestHandler);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
//package org.jeecg.fallback.sentinel;
|
||||
//import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
|
||||
//import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.cloud.commons.util.InetUtils;
|
||||
//import org.springframework.http.HttpStatus;
|
||||
//import org.springframework.http.MediaType;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//import org.springframework.web.reactive.function.BodyInserters;
|
||||
//import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
//import org.springframework.web.server.ServerWebExchange;
|
||||
//import reactor.core.publisher.Mono;
|
||||
//
|
||||
//import javax.annotation.PostConstruct;
|
||||
//
|
||||
///**
|
||||
// * 自定义限流返回信息
|
||||
// * @author scott
|
||||
// */
|
||||
//@Slf4j
|
||||
//@Component
|
||||
//public class SentinelBlockRequestHandler implements BlockRequestHandler {
|
||||
// @Autowired
|
||||
// private InetUtils inetUtils;
|
||||
//
|
||||
// @PostConstruct
|
||||
// public void doInit() {
|
||||
// System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, inetUtils.findFirstNonLoopbackAddress().getHostAddress());
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable ex) {
|
||||
// String resultString = "{\"code\":403,\"message\":\"服务开启限流保护,请稍后再试!\"}";
|
||||
// return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS).contentType(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(resultString));
|
||||
// }
|
||||
//
|
||||
//
|
||||
//}
|
||||
@ -0,0 +1,55 @@
|
||||
package org.jeecg.filter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
|
||||
|
||||
/**
|
||||
* 全局拦截器,作用所有的微服务
|
||||
*
|
||||
* 1.重写StripPrefix(获取真实的URL)
|
||||
* 2.将现在的request,添加当前身份
|
||||
* @author: scott
|
||||
* @date: 2022/4/8 10:55
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GlobalAccessTokenFilter implements GlobalFilter, Ordered {
|
||||
public final static String X_ACCESS_TOKEN = "X-Access-Token";
|
||||
public final static String X_GATEWAY_BASE_PATH = "X_GATEWAY_BASE_PATH";
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
|
||||
String scheme = exchange.getRequest().getURI().getScheme();
|
||||
String host = exchange.getRequest().getURI().getHost();
|
||||
int port = exchange.getRequest().getURI().getPort();
|
||||
String basePath = scheme + "://" + host + ":" + port;
|
||||
// 1. 重写StripPrefix(获取真实的URL)
|
||||
addOriginalRequestUrl(exchange, exchange.getRequest().getURI());
|
||||
String rawPath = exchange.getRequest().getURI().getRawPath();
|
||||
String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/")).skip(1L).collect(Collectors.joining("/"));
|
||||
ServerHttpRequest newRequest = exchange.getRequest().mutate().path(newPath).build();
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
|
||||
//2.将现在的request,添加当前身份
|
||||
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").header(X_GATEWAY_BASE_PATH,basePath).build();
|
||||
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
|
||||
return chain.filter(mutableExchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package org.jeecg.filter;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author: zyf
|
||||
* @date: 20210715
|
||||
*/
|
||||
@Configuration
|
||||
public class SentinelFilterContextConfig {
|
||||
@Bean
|
||||
public FilterRegistrationBean sentinelFilterRegistration() {
|
||||
FilterRegistrationBean registration = new FilterRegistrationBean();
|
||||
registration.setFilter(new CommonFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
// 入口资源关闭聚合
|
||||
registration.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
|
||||
registration.setName("sentinelFilter");
|
||||
registration.setOrder(1);
|
||||
return registration;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package org.jeecg.handler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.common.constant.GlobalConstants;
|
||||
import org.jeecg.common.modules.redis.listener.JeecgRedisListener;
|
||||
import org.jeecg.loader.DynamicRouteLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 路由刷新监听(实现方式:redis监听handler)
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@Slf4j
|
||||
@Component(GlobalConstants.LODER_ROUDER_HANDLER)
|
||||
public class LoderRouderHandler implements JeecgRedisListener {
|
||||
|
||||
@Resource
|
||||
private DynamicRouteLoader dynamicRouteLoader;
|
||||
|
||||
|
||||
@Override
|
||||
public void onMessage(BaseMap message) {
|
||||
dynamicRouteLoader.refresh(message);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,120 @@
|
||||
package org.jeecg.handler.swagger;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.nacos.api.naming.NamingFactory;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 聚合各个服务的swagger接口
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@Primary
|
||||
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
|
||||
/**
|
||||
* swagger2默认的url后缀
|
||||
*/
|
||||
private static final String SWAGGER2URL = "/v2/api-docs";
|
||||
|
||||
/**
|
||||
* 网关路由
|
||||
*/
|
||||
private final RouteLocator routeLocator;
|
||||
|
||||
/**
|
||||
* nacos服务地址
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.server-addr}")
|
||||
private String serverAddr;
|
||||
|
||||
/**
|
||||
* Swagger中需要排除的服务
|
||||
*/
|
||||
private String[] excludeServiceIds=new String[]{"jeecg-cloud-monitor"};
|
||||
|
||||
|
||||
/**
|
||||
* 网关应用名称
|
||||
*/
|
||||
@Value("${spring.application.name}")
|
||||
private String self;
|
||||
|
||||
@Autowired
|
||||
public MySwaggerResourceProvider(RouteLocator routeLocator) {
|
||||
this.routeLocator = routeLocator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SwaggerResource> get() {
|
||||
List<SwaggerResource> resources = new ArrayList<>();
|
||||
List<String> routeHosts = new ArrayList<>();
|
||||
// 获取所有可用的host:serviceId
|
||||
routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
|
||||
.filter(route -> !self.equals(route.getUri().getHost()))
|
||||
.subscribe(route ->{
|
||||
//update-begin---author:zyf ---date:20220413 for:过滤掉无效路由,避免接口文档报错无法打开
|
||||
boolean hasRoute=checkRoute(route.getId());
|
||||
if(hasRoute){
|
||||
routeHosts.add(route.getUri().getHost());
|
||||
}
|
||||
//update-end---author:zyf ---date:20220413 for:过滤掉无效路由,避免接口文档报错无法打开
|
||||
});
|
||||
|
||||
// 记录已经添加过的server,存在同一个应用注册了多个服务在nacos上
|
||||
Set<String> dealed = new HashSet<>();
|
||||
routeHosts.forEach(instance -> {
|
||||
// 拼接url
|
||||
String url = "/" + instance.toLowerCase() + SWAGGER2URL;
|
||||
if (!dealed.contains(url)) {
|
||||
dealed.add(url);
|
||||
log.info(" Gateway add SwaggerResource: {}",url);
|
||||
SwaggerResource swaggerResource = new SwaggerResource();
|
||||
swaggerResource.setUrl(url);
|
||||
swaggerResource.setSwaggerVersion("2.0");
|
||||
swaggerResource.setName(instance);
|
||||
//Swagger排除不展示的服务
|
||||
if(!ArrayUtil.contains(excludeServiceIds,instance)){
|
||||
resources.add(swaggerResource);
|
||||
}
|
||||
}
|
||||
});
|
||||
return resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测nacos中是否有健康实例
|
||||
* @param routeId
|
||||
* @return
|
||||
*/
|
||||
private Boolean checkRoute(String routeId) {
|
||||
Boolean hasRoute = false;
|
||||
try {
|
||||
NamingService naming = NamingFactory.createNamingService(serverAddr);
|
||||
List<Instance> list = naming.selectInstances(routeId, true);
|
||||
if (ObjectUtil.isNotEmpty(list)) {
|
||||
hasRoute = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return hasRoute;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package org.jeecg.handler.swagger;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import springfox.documentation.swagger.web.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* swagger聚合接口,三个接口都是 doc.html需要访问的接口
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-resources")
|
||||
public class SwaggerResourceController {
|
||||
private MySwaggerResourceProvider swaggerResourceProvider;
|
||||
|
||||
@Autowired
|
||||
public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
|
||||
this.swaggerResourceProvider = swaggerResourceProvider;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/configuration/security")
|
||||
public ResponseEntity<SecurityConfiguration> securityConfiguration() {
|
||||
return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/configuration/ui")
|
||||
public ResponseEntity<UiConfiguration> uiConfiguration() {
|
||||
return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping
|
||||
public ResponseEntity<List<SwaggerResource>> swaggerResources() {
|
||||
return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,362 @@
|
||||
package org.jeecg.loader;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.common.constant.CacheConstant;
|
||||
import org.jeecg.common.util.RedisUtil;
|
||||
import org.jeecg.config.GatewayRoutersConfig;
|
||||
import org.jeecg.config.RouterDataType;
|
||||
import org.jeecg.loader.repository.DynamicRouteService;
|
||||
import org.jeecg.loader.repository.MyInMemoryRouteDefinitionRepository;
|
||||
import org.jeecg.loader.vo.MyRouteDefinition;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
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.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* 动态路由加载器
|
||||
*
|
||||
* @author : zyf
|
||||
* @date :2020-11-10
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RefreshScope
|
||||
@DependsOn({"gatewayRoutersConfig"})
|
||||
public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
public static final long DEFAULT_TIMEOUT = 30000;
|
||||
@Autowired
|
||||
private GatewayRoutersConfig gatewayRoutersConfig;
|
||||
private MyInMemoryRouteDefinitionRepository repository;
|
||||
private ApplicationEventPublisher publisher;
|
||||
private DynamicRouteService dynamicRouteService;
|
||||
private ConfigService configService;
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
|
||||
/**
|
||||
* 需要拼接key的路由条件
|
||||
*/
|
||||
private static String[] GEN_KEY_ROUTERS = new String[]{"Path", "Host", "Method", "After", "Before", "Between", "RemoteAddr"};
|
||||
|
||||
public DynamicRouteLoader(MyInMemoryRouteDefinitionRepository repository, DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
|
||||
this.repository = repository;
|
||||
this.dynamicRouteService = dynamicRouteService;
|
||||
this.redisUtil = redisUtil;
|
||||
}
|
||||
|
||||
// @PostConstruct
|
||||
// public void init() {
|
||||
// init(null);
|
||||
// }
|
||||
|
||||
|
||||
public void init(BaseMap baseMap) {
|
||||
log.info("初始化路由模式,dataType:"+ gatewayRoutersConfig.getDataType());
|
||||
if (RouterDataType.nacos.toString().endsWith(gatewayRoutersConfig.getDataType())) {
|
||||
loadRoutesByNacos();
|
||||
}
|
||||
//从数据库加载路由
|
||||
if (RouterDataType.database.toString().endsWith(gatewayRoutersConfig.getDataType())) {
|
||||
loadRoutesByRedis(baseMap);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 刷新路由
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Mono<Void> refresh(BaseMap baseMap) {
|
||||
log.info("初始化路由模式,dataType:"+ gatewayRoutersConfig.getDataType());
|
||||
if (!RouterDataType.yml.toString().endsWith(gatewayRoutersConfig.getDataType())) {
|
||||
this.init(baseMap);
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从nacos中读取路由配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private void loadRoutesByNacos() {
|
||||
List<RouteDefinition> routes = Lists.newArrayList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
}
|
||||
try {
|
||||
String configInfo = configService.getConfig(gatewayRoutersConfig.getDataId(), gatewayRoutersConfig.getRouteGroup(), DEFAULT_TIMEOUT);
|
||||
if (StringUtils.isNotBlank(configInfo)) {
|
||||
log.info("获取网关当前配置:\r\n{}", configInfo);
|
||||
routes = JSON.parseArray(configInfo, RouteDefinition.class);
|
||||
}
|
||||
} catch (NacosException e) {
|
||||
log.error("初始化网关路由时发生错误", e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (RouteDefinition definition : routes) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
dynamicRouteService.add(definition);
|
||||
}
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
dynamicRouteByNacosListener(gatewayRoutersConfig.getDataId(), gatewayRoutersConfig.getRouteGroup());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从redis中读取路由配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private void loadRoutesByRedis(BaseMap baseMap) {
|
||||
List<MyRouteDefinition> routes = Lists.newArrayList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
}
|
||||
Object configInfo = redisUtil.get(CacheConstant.GATEWAY_ROUTES);
|
||||
if (ObjectUtil.isNotEmpty(configInfo)) {
|
||||
log.debug("获取网关当前配置:\r\n{}", configInfo);
|
||||
JSONArray array = JSON.parseArray(configInfo.toString());
|
||||
try {
|
||||
routes = getRoutesByJson(array);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
for (MyRouteDefinition definition : routes) {
|
||||
log.debug("update route : {}", definition.toString());
|
||||
Integer status=definition.getStatus();
|
||||
if(status.equals(0)){
|
||||
dynamicRouteService.delete(definition.getId());
|
||||
}else{
|
||||
dynamicRouteService.add(definition);
|
||||
}
|
||||
}
|
||||
if(ObjectUtils.isNotEmpty(baseMap)){
|
||||
String delRouterId = baseMap.get("delRouterId");
|
||||
if (ObjectUtils.isNotEmpty(delRouterId)) {
|
||||
dynamicRouteService.delete(delRouterId);
|
||||
}
|
||||
}
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* redis中的信息需要处理下 转成RouteDefinition对象
|
||||
* - id: login
|
||||
* uri: lb://cloud-jeecg-system
|
||||
* predicates:
|
||||
* - Path=/jeecg-boot/sys/**,
|
||||
*
|
||||
* @param array
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static List<MyRouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
|
||||
List<MyRouteDefinition> ls = new ArrayList<>();
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject obj = array.getJSONObject(i);
|
||||
MyRouteDefinition route = new MyRouteDefinition();
|
||||
route.setId(obj.getString("routerId"));
|
||||
route.setStatus(obj.getInteger("status"));
|
||||
Object uri = obj.get("uri");
|
||||
if (uri == null) {
|
||||
route.setUri(new URI("lb://" + obj.getString("name")));
|
||||
} else {
|
||||
route.setUri(new URI(obj.getString("uri")));
|
||||
}
|
||||
Object predicates = obj.get("predicates");
|
||||
if (predicates != null) {
|
||||
JSONArray list = JSON.parseArray(predicates.toString());
|
||||
List<PredicateDefinition> predicateDefinitionList = new ArrayList<>();
|
||||
for (Object map : list) {
|
||||
JSONObject json = (JSONObject) map;
|
||||
PredicateDefinition predicateDefinition = new PredicateDefinition();
|
||||
//update-begin-author:zyf date:20220419 for:【VUEN-762】路由条件添加异常问题,原因是部分路由条件参数需要设置固定key
|
||||
String name=json.getString("name");
|
||||
predicateDefinition.setName(name);
|
||||
//路由条件是否拼接Key
|
||||
if(ArrayUtil.contains(GEN_KEY_ROUTERS,name)) {
|
||||
JSONArray jsonArray = json.getJSONArray("args");
|
||||
for (int j = 0; j < jsonArray.size(); j++) {
|
||||
predicateDefinition.addArg("_genkey" + j, jsonArray.get(j).toString());
|
||||
}
|
||||
}else{
|
||||
JSONObject jsonObject = json.getJSONObject("args");
|
||||
if(ObjectUtil.isNotEmpty(jsonObject)){
|
||||
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
||||
Object valueObj=entry.getValue();
|
||||
if(ObjectUtil.isNotEmpty(valueObj)) {
|
||||
predicateDefinition.addArg(entry.getKey(), valueObj.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//update-end-author:zyf date:20220419 for:【VUEN-762】路由条件添加异常问题,原因是部分路由条件参数需要设置固定key
|
||||
predicateDefinitionList.add(predicateDefinition);
|
||||
}
|
||||
route.setPredicates(predicateDefinitionList);
|
||||
}
|
||||
|
||||
Object filters = obj.get("filters");
|
||||
if (filters != null) {
|
||||
JSONArray list = JSON.parseArray(filters.toString());
|
||||
List<FilterDefinition> filterDefinitionList = new ArrayList<>();
|
||||
if (ObjectUtil.isNotEmpty(list)) {
|
||||
for (Object map : list) {
|
||||
JSONObject json = (JSONObject) map;
|
||||
JSONArray jsonArray = json.getJSONArray("args");
|
||||
String name = json.getString("name");
|
||||
FilterDefinition filterDefinition = new FilterDefinition();
|
||||
for (Object o : jsonArray) {
|
||||
JSONObject params = (JSONObject) o;
|
||||
filterDefinition.addArg(params.getString("key"), params.get("value").toString());
|
||||
}
|
||||
filterDefinition.setName(name);
|
||||
filterDefinitionList.add(filterDefinition);
|
||||
}
|
||||
route.setFilters(filterDefinitionList);
|
||||
}
|
||||
}
|
||||
ls.add(route);
|
||||
}
|
||||
return ls;
|
||||
}
|
||||
|
||||
|
||||
// private void loadRoutesByDataBase() {
|
||||
// List<GatewayRouteVo> routeList = jdbcTemplate.query(SELECT_ROUTES, new RowMapper<GatewayRouteVo>() {
|
||||
// @Override
|
||||
// public GatewayRouteVo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
// GatewayRouteVo result = new GatewayRouteVo();
|
||||
// result.setId(rs.getString("id"));
|
||||
// result.setName(rs.getString("name"));
|
||||
// result.setUri(rs.getString("uri"));
|
||||
// result.setStatus(rs.getInt("status"));
|
||||
// result.setRetryable(rs.getInt("retryable"));
|
||||
// result.setPredicates(rs.getString("predicates"));
|
||||
// result.setStripPrefix(rs.getInt("strip_prefix"));
|
||||
// result.setPersist(rs.getInt("persist"));
|
||||
// return result;
|
||||
// }
|
||||
// });
|
||||
// if (ObjectUtil.isNotEmpty(routeList)) {
|
||||
// // 加载路由
|
||||
// routeList.forEach(route -> {
|
||||
// RouteDefinition definition = new RouteDefinition();
|
||||
// List<PredicateDefinition> predicatesList = Lists.newArrayList();
|
||||
// List<FilterDefinition> filtersList = Lists.newArrayList();
|
||||
// definition.setId(route.getId());
|
||||
// String predicates = route.getPredicates();
|
||||
// String filters = route.getFilters();
|
||||
// if (StringUtils.isNotEmpty(predicates)) {
|
||||
// predicatesList = JSON.parseArray(predicates, PredicateDefinition.class);
|
||||
// definition.setPredicates(predicatesList);
|
||||
// }
|
||||
// if (StringUtils.isNotEmpty(filters)) {
|
||||
// filtersList = JSON.parseArray(filters, FilterDefinition.class);
|
||||
// definition.setFilters(filtersList);
|
||||
// }
|
||||
// URI uri = UriComponentsBuilder.fromUriString(route.getUri()).build().toUri();
|
||||
// definition.setUri(uri);
|
||||
// this.repository.save(Mono.just(definition)).subscribe();
|
||||
// });
|
||||
// log.info("加载路由:{}==============", routeList.size());
|
||||
// Mono.empty();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 监听Nacos下发的动态路由配置
|
||||
*
|
||||
* @param dataId
|
||||
* @param group
|
||||
*/
|
||||
public void dynamicRouteByNacosListener(String dataId, String group) {
|
||||
try {
|
||||
configService.addListener(dataId, group, new Listener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
log.info("进行网关更新:\n\r{}", configInfo);
|
||||
List<MyRouteDefinition> definitionList = JSON.parseArray(configInfo, MyRouteDefinition.class);
|
||||
for (MyRouteDefinition definition : definitionList) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
dynamicRouteService.update(definition);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Executor getExecutor() {
|
||||
log.info("getExecutor\n\r");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("从nacos接收动态路由配置出错!!!", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建ConfigService
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private ConfigService createConfigService() {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("serverAddr", gatewayRoutersConfig.getServerAddr());
|
||||
if(StringUtils.isNotBlank(gatewayRoutersConfig.getNamespace())){
|
||||
properties.setProperty("namespace", gatewayRoutersConfig.getNamespace());
|
||||
}
|
||||
if(StringUtils.isNotBlank( gatewayRoutersConfig.getUsername())){
|
||||
properties.setProperty("username", gatewayRoutersConfig.getUsername());
|
||||
}
|
||||
if(StringUtils.isNotBlank(gatewayRoutersConfig.getPassword())){
|
||||
properties.setProperty("password", gatewayRoutersConfig.getPassword());
|
||||
}
|
||||
return configService = NacosFactory.createConfigService(properties);
|
||||
} catch (Exception e) {
|
||||
log.error("创建ConfigService异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.publisher = applicationEventPublisher;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
package org.jeecg.loader.repository;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.loader.repository.MyInMemoryRouteDefinitionRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 动态更新路由网关service
|
||||
* 1)实现一个Spring提供的事件推送接口ApplicationEventPublisherAware
|
||||
* 2)提供动态路由的基础方法,可通过获取bean操作该类的方法。该类提供新增路由、更新路由、删除路由,然后实现发布的功能。
|
||||
*
|
||||
* @author zyf
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class DynamicRouteService implements ApplicationEventPublisherAware {
|
||||
|
||||
@Autowired
|
||||
private MyInMemoryRouteDefinitionRepository repository;
|
||||
|
||||
/**
|
||||
* 发布事件
|
||||
*/
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.publisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除路由
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public synchronized void delete(String id) {
|
||||
try {
|
||||
repository.delete(Mono.just(id)).subscribe();
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
}catch (Exception e){
|
||||
log.warn(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新路由
|
||||
*
|
||||
* @param definition
|
||||
* @return
|
||||
*/
|
||||
public synchronized String update(RouteDefinition definition) {
|
||||
try {
|
||||
log.info("gateway update route {}", definition);
|
||||
} catch (Exception e) {
|
||||
return "update fail,not find route routeId: " + definition.getId();
|
||||
}
|
||||
try {
|
||||
repository.save(Mono.just(definition)).subscribe();
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
return "success";
|
||||
} catch (Exception e) {
|
||||
return "update route fail";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加路由
|
||||
*
|
||||
* @param definition
|
||||
* @return
|
||||
*/
|
||||
public synchronized String add(RouteDefinition definition) {
|
||||
log.info("gateway add route {}", definition);
|
||||
try {
|
||||
repository.save(Mono.just(definition)).subscribe();
|
||||
} catch (Exception e) {
|
||||
log.warn(e.getMessage(),e);
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
//
|
||||
// Source code recreated from a .class file by IntelliJ IDEA
|
||||
// (powered by Fernflower decompiler)
|
||||
//
|
||||
|
||||
package org.jeecg.loader.repository;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
|
||||
import org.springframework.cloud.gateway.support.NotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author qinfeng
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MyInMemoryRouteDefinitionRepository implements RouteDefinitionRepository {
|
||||
private final Map<String, RouteDefinition> routes = Collections.synchronizedMap(new LinkedHashMap());
|
||||
|
||||
public MyInMemoryRouteDefinitionRepository() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> save(Mono<RouteDefinition> route) {
|
||||
return route.flatMap((r) -> {
|
||||
if (ObjectUtils.isEmpty(r.getId())) {
|
||||
return Mono.error(new IllegalArgumentException("id may not be empty"));
|
||||
} else {
|
||||
this.routes.put(r.getId(), r);
|
||||
return Mono.empty();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> delete(Mono<String> routeId) {
|
||||
return routeId.flatMap((id) -> {
|
||||
if (this.routes.containsKey(id)) {
|
||||
this.routes.remove(id);
|
||||
return Mono.empty();
|
||||
} else {
|
||||
log.warn("RouteDefinition not found: " + routeId);
|
||||
return Mono.empty();
|
||||
// return Mono.defer(() -> {
|
||||
// return Mono.error(new NotFoundException("RouteDefinition not found: " + routeId));
|
||||
// });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<RouteDefinition> getRouteDefinitions() {
|
||||
Map<String, RouteDefinition> routesSafeCopy = new LinkedHashMap(this.routes);
|
||||
return Flux.fromIterable(routesSafeCopy.values());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package org.jeecg.loader.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 路由参数模型
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@Data
|
||||
public class GatewayRouteVo {
|
||||
private String id;
|
||||
private String name;
|
||||
private String uri;
|
||||
private String predicates;
|
||||
private String filters;
|
||||
private Integer stripPrefix;
|
||||
private Integer retryable;
|
||||
private Integer persist;
|
||||
private Integer status;
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package org.jeecg.loader.vo;
|
||||
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
|
||||
/**
|
||||
* 自定义RouteDefinition
|
||||
* @author zyf
|
||||
*/
|
||||
public class MyRouteDefinition extends RouteDefinition {
|
||||
/**
|
||||
* 路由状态
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user