mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-01-03 20:35:29 +08:00
JeecgBoot3.2.0 版本发布 [优化微服务模块]
This commit is contained in:
@ -2,6 +2,7 @@ package org.jeecg.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -19,11 +20,9 @@ import static org.springframework.web.reactive.function.server.ServerResponse.ok
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RefreshScope
|
||||
public class GatewayRoutersConfiguration {
|
||||
/**
|
||||
* 路由配置方式:database,yml,nacos
|
||||
*/
|
||||
public static String DATA_TYPE;
|
||||
|
||||
public static final long DEFAULT_TIMEOUT = 30000;
|
||||
public static String SERVER_ADDR;
|
||||
public static String NAMESPACE;
|
||||
@ -52,10 +51,6 @@ public class GatewayRoutersConfiguration {
|
||||
ROUTE_GROUP = routeGroup;
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.data-type:#{null}}")
|
||||
public void setDataType(String dataType) {
|
||||
DATA_TYPE = dataType;
|
||||
}
|
||||
|
||||
@Value("${spring.cloud.nacos.config.username}")
|
||||
public void setUsername(String username) {
|
||||
|
||||
@ -2,6 +2,8 @@ package org.jeecg.config;
|
||||
|
||||
/**
|
||||
* nocos配置方式枚举
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
public enum RouterDataType {
|
||||
/**
|
||||
|
||||
@ -33,7 +33,7 @@ public class GatewaySentinelExceptionConfig {
|
||||
} else {
|
||||
msg = "未知限流降级";
|
||||
}
|
||||
HashMap<String, String> map = new HashMap();
|
||||
HashMap<String, String> map = new HashMap(5);
|
||||
map.put("code", HttpStatus.TOO_MANY_REQUESTS.toString());
|
||||
map.put("message", msg);
|
||||
//自定义异常处理
|
||||
|
||||
@ -30,22 +30,17 @@ public class GlobalAccessTokenFilter implements GlobalFilter, Ordered {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
String url = exchange.getRequest().getURI().getPath();
|
||||
// log.info(" access url : "+ url);
|
||||
|
||||
String scheme = exchange.getRequest().getURI().getScheme();
|
||||
String host = exchange.getRequest().getURI().getHost();
|
||||
int port = exchange.getRequest().getURI().getPort();
|
||||
String basePath = scheme + "://" + host + ":" + port;
|
||||
// log.info(" base path : "+ basePath);
|
||||
|
||||
// 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();
|
||||
|
||||
@ -11,6 +11,8 @@ import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 路由刷新监听(实现方式:redis监听handler)
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@Slf4j
|
||||
@Component(GlobalConstants.LODER_ROUDER_HANDLER)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
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;
|
||||
@ -21,6 +22,8 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* 聚合各个服务的swagger接口
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@ -42,6 +45,11 @@ public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
|
||||
@Value("${spring.cloud.nacos.discovery.server-addr}")
|
||||
private String serverAddr;
|
||||
|
||||
/**
|
||||
* Swagger中需要排除的服务
|
||||
*/
|
||||
private String[] excludeServiceIds=new String[]{"jeecg-cloud-monitor"};
|
||||
|
||||
|
||||
/**
|
||||
* 网关应用名称
|
||||
@ -82,8 +90,8 @@ public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
|
||||
swaggerResource.setUrl(url);
|
||||
swaggerResource.setSwaggerVersion("2.0");
|
||||
swaggerResource.setName(instance);
|
||||
//Swagger排除监控
|
||||
if(instance.indexOf("jeecg-cloud-monitor")==-1){
|
||||
//Swagger排除不展示的服务
|
||||
if(!ArrayUtil.contains(excludeServiceIds,instance)){
|
||||
resources.add(swaggerResource);
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* swagger聚合接口,三个接口都是 doc.html需要访问的接口
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-resources")
|
||||
|
||||
@ -21,6 +21,8 @@ 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.Value;
|
||||
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;
|
||||
@ -49,6 +51,7 @@ import java.util.concurrent.Executor;
|
||||
@Slf4j
|
||||
@Component
|
||||
@DependsOn({"gatewayRoutersConfiguration"})
|
||||
@RefreshScope
|
||||
public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
|
||||
@ -57,6 +60,12 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
private DynamicRouteService dynamicRouteService;
|
||||
private ConfigService configService;
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
/**
|
||||
* 路由配置方式:database(数据库),yml(配置文件),nacos
|
||||
*/
|
||||
@Value("${jeecg.route.config.data-type:database}")
|
||||
public String dataType;
|
||||
/**
|
||||
* 需要拼接key的路由条件
|
||||
*/
|
||||
@ -76,7 +85,6 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
|
||||
public void init(BaseMap baseMap) {
|
||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||
log.info("初始化路由,dataType:"+ dataType);
|
||||
if (RouterDataType.nacos.toString().endsWith(dataType)) {
|
||||
loadRoutesByNacos();
|
||||
@ -92,7 +100,6 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
* @return
|
||||
*/
|
||||
public Mono<Void> refresh(BaseMap baseMap) {
|
||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||
if (!RouterDataType.yml.toString().endsWith(dataType)) {
|
||||
this.init(baseMap);
|
||||
}
|
||||
|
||||
@ -59,7 +59,6 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
|
||||
public synchronized String update(RouteDefinition definition) {
|
||||
try {
|
||||
log.info("gateway update route {}", definition);
|
||||
//delete(definition.getId());
|
||||
} catch (Exception e) {
|
||||
return "update fail,not find route routeId: " + definition.getId();
|
||||
}
|
||||
|
||||
@ -2,6 +2,11 @@ package org.jeecg.loader.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 路由参数模型
|
||||
* @author zyf
|
||||
* @date: 2022/4/21 10:55
|
||||
*/
|
||||
@Data
|
||||
public class GatewayRouteVo {
|
||||
private String id;
|
||||
|
||||
Reference in New Issue
Block a user