mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-01-03 20:35:29 +08:00
spring3
This commit is contained in:
@ -28,8 +28,8 @@
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<groupId>jakarta.servlet</groupId>
|
||||
<artifactId>jakarta.servlet-api</artifactId>
|
||||
</dependency>
|
||||
<!-- redis方式限流 -->
|
||||
<dependency>
|
||||
@ -58,10 +58,10 @@
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
</dependency>
|
||||
<!--- sentinel流控链路不生效 -->
|
||||
<dependency>
|
||||
<!--<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-web-servlet</artifactId>
|
||||
</dependency>
|
||||
</dependency>-->
|
||||
|
||||
|
||||
<!--健康监控-->
|
||||
@ -73,7 +73,7 @@
|
||||
<!-- Swagger API文档 -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
|
||||
<version>${knife4j-spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@ -13,7 +13,7 @@ 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 jakarta.annotation.Resource;
|
||||
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
||||
@ -50,8 +50,8 @@ public class JeecgGatewayApplication implements CommandLineRunner {
|
||||
* @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));
|
||||
}
|
||||
// @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));
|
||||
// }
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ 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 jakarta.annotation.PostConstruct;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
|
||||
@ -1,25 +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;
|
||||
}
|
||||
}
|
||||
//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;
|
||||
// }
|
||||
//}
|
||||
@ -7,7 +7,7 @@ import org.jeecg.common.modules.redis.listener.JeecgRedisListener;
|
||||
import org.jeecg.loader.DynamicRouteLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 路由刷新监听(实现方式:redis监听handler)
|
||||
|
||||
@ -1,160 +1,160 @@
|
||||
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.*;
|
||||
|
||||
/**
|
||||
* 聚合各个服务的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名字服务
|
||||
*/
|
||||
private NamingService naming;
|
||||
|
||||
/**
|
||||
* nacos服务地址
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.server-addr}")
|
||||
private String serverAddr;
|
||||
/**
|
||||
* nacos namespace
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.namespace:#{null}}")
|
||||
private String namespace;
|
||||
|
||||
/**
|
||||
* nacos groupName
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.config.group:DEFAULT_GROUP:#{null}}")
|
||||
private String group;
|
||||
|
||||
/**
|
||||
* nacos username
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.username:#{null}}")
|
||||
private String username;
|
||||
/**
|
||||
* nacos password
|
||||
*/
|
||||
@Value("${spring.cloud.nacos.discovery.password:#{null}}")
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
//修复使用带命名空间启动网关swagger看不到接口文档的问题
|
||||
Properties properties=new Properties();
|
||||
properties.setProperty("serverAddr",serverAddr);
|
||||
if(namespace!=null && !"".equals(namespace)){
|
||||
log.info("nacos.discovery.namespace = {}", namespace);
|
||||
properties.setProperty("namespace",namespace);
|
||||
}
|
||||
if(username!=null && !"".equals(username)){
|
||||
properties.setProperty("username",username);
|
||||
}
|
||||
if(password!=null && !"".equals(password)){
|
||||
properties.setProperty("password",password);
|
||||
}
|
||||
//【issues/5115】因swagger文档导致gateway内存溢出
|
||||
if (this.naming == null) {
|
||||
this.naming = NamingFactory.createNamingService(properties);
|
||||
}
|
||||
log.info(" config.group : {}", group);
|
||||
List<Instance> list = this.naming.selectInstances(routeId, group , true);
|
||||
if (ObjectUtil.isNotEmpty(list)) {
|
||||
hasRoute = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return hasRoute;
|
||||
}
|
||||
}
|
||||
//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.*;
|
||||
//
|
||||
///**
|
||||
// * 聚合各个服务的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名字服务
|
||||
// */
|
||||
// private NamingService naming;
|
||||
//
|
||||
// /**
|
||||
// * nacos服务地址
|
||||
// */
|
||||
// @Value("${spring.cloud.nacos.discovery.server-addr}")
|
||||
// private String serverAddr;
|
||||
// /**
|
||||
// * nacos namespace
|
||||
// */
|
||||
// @Value("${spring.cloud.nacos.discovery.namespace:#{null}}")
|
||||
// private String namespace;
|
||||
//
|
||||
// /**
|
||||
// * nacos groupName
|
||||
// */
|
||||
// @Value("${spring.cloud.nacos.config.group:DEFAULT_GROUP:#{null}}")
|
||||
// private String group;
|
||||
//
|
||||
// /**
|
||||
// * nacos username
|
||||
// */
|
||||
// @Value("${spring.cloud.nacos.discovery.username:#{null}}")
|
||||
// private String username;
|
||||
// /**
|
||||
// * nacos password
|
||||
// */
|
||||
// @Value("${spring.cloud.nacos.discovery.password:#{null}}")
|
||||
// private String password;
|
||||
//
|
||||
// /**
|
||||
// * 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 {
|
||||
// //修复使用带命名空间启动网关swagger看不到接口文档的问题
|
||||
// Properties properties=new Properties();
|
||||
// properties.setProperty("serverAddr",serverAddr);
|
||||
// if(namespace!=null && !"".equals(namespace)){
|
||||
// log.info("nacos.discovery.namespace = {}", namespace);
|
||||
// properties.setProperty("namespace",namespace);
|
||||
// }
|
||||
// if(username!=null && !"".equals(username)){
|
||||
// properties.setProperty("username",username);
|
||||
// }
|
||||
// if(password!=null && !"".equals(password)){
|
||||
// properties.setProperty("password",password);
|
||||
// }
|
||||
// //【issues/5115】因swagger文档导致gateway内存溢出
|
||||
// if (this.naming == null) {
|
||||
// this.naming = NamingFactory.createNamingService(properties);
|
||||
// }
|
||||
// log.info(" config.group : {}", group);
|
||||
// List<Instance> list = this.naming.selectInstances(routeId, group , true);
|
||||
// if (ObjectUtil.isNotEmpty(list)) {
|
||||
// hasRoute = true;
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return hasRoute;
|
||||
// }
|
||||
//}
|
||||
@ -1,41 +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);
|
||||
}
|
||||
}
|
||||
//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);
|
||||
// }
|
||||
//}
|
||||
@ -9,10 +9,7 @@ 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;
|
||||
@ -32,6 +29,8 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
@ -109,7 +108,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
* @return
|
||||
*/
|
||||
private void loadRoutesByNacos() {
|
||||
List<RouteDefinition> routes = Lists.newArrayList();
|
||||
List<RouteDefinition> routes = Collections.emptyList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
@ -118,7 +117,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
log.info("jeecg.route.config.data-id = {}", gatewayRoutersConfig.getDataId());
|
||||
log.info("nacos.config.group = {}", gatewayRoutersConfig.getRouteGroup());
|
||||
String configInfo = configService.getConfig(gatewayRoutersConfig.getDataId(), gatewayRoutersConfig.getRouteGroup(), DEFAULT_TIMEOUT);
|
||||
if (StringUtils.isNotBlank(configInfo)) {
|
||||
if (StringUtils.hasText(configInfo)) {
|
||||
log.info("获取网关当前配置:\r\n{}", configInfo);
|
||||
routes = JSON.parseArray(configInfo, RouteDefinition.class);
|
||||
}else{
|
||||
@ -143,7 +142,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
* @return
|
||||
*/
|
||||
private void loadRoutesByRedis(BaseMap baseMap) {
|
||||
List<MyRouteDefinition> routes = Lists.newArrayList();
|
||||
List<MyRouteDefinition> routes = Collections.emptyList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
@ -170,9 +169,9 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
dynamicRouteService.add(definition);
|
||||
}
|
||||
}
|
||||
if(ObjectUtils.isNotEmpty(baseMap)){
|
||||
if(!ObjectUtils.isEmpty(baseMap)){
|
||||
String delRouterId = baseMap.get("delRouterId");
|
||||
if (ObjectUtils.isNotEmpty(delRouterId)) {
|
||||
if (!ObjectUtils.isEmpty(delRouterId)) {
|
||||
dynamicRouteService.delete(delRouterId);
|
||||
}
|
||||
}
|
||||
@ -360,13 +359,13 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
try {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("serverAddr", gatewayRoutersConfig.getServerAddr());
|
||||
if(StringUtils.isNotBlank(gatewayRoutersConfig.getNamespace())){
|
||||
if(StringUtils.hasText(gatewayRoutersConfig.getNamespace())){
|
||||
properties.setProperty("namespace", gatewayRoutersConfig.getNamespace());
|
||||
}
|
||||
if(StringUtils.isNotBlank( gatewayRoutersConfig.getUsername())){
|
||||
if(StringUtils.hasText( gatewayRoutersConfig.getUsername())){
|
||||
properties.setProperty("username", gatewayRoutersConfig.getUsername());
|
||||
}
|
||||
if(StringUtils.isNotBlank(gatewayRoutersConfig.getPassword())){
|
||||
if(StringUtils.hasText(gatewayRoutersConfig.getPassword())){
|
||||
properties.setProperty("password", gatewayRoutersConfig.getPassword());
|
||||
}
|
||||
return configService = NacosFactory.createConfigService(properties);
|
||||
|
||||
@ -1,6 +1,14 @@
|
||||
server:
|
||||
port: 9999
|
||||
|
||||
knife4j:
|
||||
gateway:
|
||||
enabled: true
|
||||
discover:
|
||||
excluded-services: ${spring.application.name}
|
||||
enabled: true
|
||||
strategy: discover
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: jeecg-gateway
|
||||
@ -8,21 +16,19 @@ spring:
|
||||
allow-circular-references: true
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:${spring.application.name}-@profile.name@.yaml
|
||||
- optional:nacos:jeecg-gateway-dev.yaml
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: @config.server-addr@
|
||||
group: @config.group@
|
||||
namespace: @config.namespace@
|
||||
username: @config.username@
|
||||
password: @config.password@
|
||||
server-addr: localhost:8848
|
||||
namespace: public
|
||||
# username: @config.username@
|
||||
# password: @config.password@
|
||||
discovery:
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
group: @config.group@
|
||||
namespace: @config.namespace@
|
||||
username: @config.username@
|
||||
password: @config.password@
|
||||
namespace: public
|
||||
# username: @config.username@
|
||||
# password: @config.password@
|
||||
gateway:
|
||||
discovery:
|
||||
locator:
|
||||
@ -50,7 +56,7 @@ spring:
|
||||
flow: # 指定数据源名称
|
||||
# 指定nacos数据源
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
# 指定配置文件
|
||||
dataId: ${spring.application.name}-flow-rules
|
||||
# 指定分组
|
||||
@ -62,7 +68,7 @@ spring:
|
||||
#降级规则
|
||||
degrade:
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
dataId: ${spring.application.name}-degrade-rules
|
||||
groupId: SENTINEL_GROUP
|
||||
rule-type: degrade
|
||||
@ -70,7 +76,7 @@ spring:
|
||||
#系统规则
|
||||
system:
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
dataId: ${spring.application.name}-system-rules
|
||||
groupId: SENTINEL_GROUP
|
||||
rule-type: system
|
||||
@ -78,7 +84,7 @@ spring:
|
||||
#授权规则
|
||||
authority:
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
dataId: ${spring.application.name}-authority-rules
|
||||
groupId: SENTINEL_GROUP
|
||||
rule-type: authority
|
||||
@ -86,7 +92,7 @@ spring:
|
||||
#热点参数
|
||||
param-flow:
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
dataId: ${spring.application.name}-param-rules
|
||||
groupId: SENTINEL_GROUP
|
||||
rule-type: param-flow
|
||||
@ -94,7 +100,7 @@ spring:
|
||||
#网关流控规则
|
||||
gw-flow:
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
dataId: ${spring.application.name}-flow-rules
|
||||
groupId: SENTINEL_GROUP
|
||||
rule-type: gw-flow
|
||||
@ -102,7 +108,7 @@ spring:
|
||||
#API流控规则
|
||||
gw-api-group:
|
||||
nacos:
|
||||
server-addr: @config.server-addr@
|
||||
server-addr: ${spring.cloud.nacos.config.server-addr}
|
||||
dataId: ${spring.application.name}-api-rules
|
||||
groupId: SENTINEL_GROUP
|
||||
rule-type: gw-api-group
|
||||
|
||||
Reference in New Issue
Block a user