mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-01-02 19:15:26 +08:00
JeecgBoot 2.4 微服务正式版本发布,基于SpringBoot的低代码平台
This commit is contained in:
@ -3,10 +3,12 @@ package org.jeecg;
|
||||
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;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class JeecgGatewayApplication {
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext applicationContext = SpringApplication.run(JeecgGatewayApplication.class, args);
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
package org.jeecg.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.handler.HystrixFallbackHandler;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -10,6 +11,8 @@ import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author scott
|
||||
* @date 2020/05/26
|
||||
@ -17,15 +20,61 @@ import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
public class GatewayRoutersConfiguration {
|
||||
private final HystrixFallbackHandler hystrixFallbackHandler;
|
||||
|
||||
public static final long DEFAULT_TIMEOUT = 30000;
|
||||
|
||||
public static String SERVER_ADDR;
|
||||
|
||||
public static String NAMESPACE;
|
||||
|
||||
public static String DATA_ID;
|
||||
|
||||
public static String ROUTE_GROUP;
|
||||
|
||||
/**
|
||||
* 路由配置文件数据获取方式yml,nacos,database
|
||||
*/
|
||||
public static String DATA_TYPE;
|
||||
|
||||
@Value("${spring.cloud.nacos.discovery.server-addr}")
|
||||
public void setServerAddr(String serverAddr) {
|
||||
SERVER_ADDR = serverAddr;
|
||||
}
|
||||
|
||||
@Value("${spring.cloud.nacos.discovery.namespace}")
|
||||
public void setNamespace(String namespace) {
|
||||
NAMESPACE = namespace;
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.data-id:#{null}}")
|
||||
public void setRouteDataId(String dataId) {
|
||||
DATA_ID = dataId + ".json";
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.group:DEFAULT_GROUP:#{null}}")
|
||||
public void setRouteGroup(String routeGroup) {
|
||||
ROUTE_GROUP = routeGroup;
|
||||
}
|
||||
|
||||
@Value("${jeecg.route.config.data-type}")
|
||||
public void setDataType(String dataType) {
|
||||
DATA_TYPE = dataType;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 路由断言
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RouterFunction routerFunction() {
|
||||
return RouterFunctions.route(
|
||||
RequestPredicates.path("/fallback").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler);
|
||||
RequestPredicates.path("/globalFallback").and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler);
|
||||
|
||||
}
|
||||
|
||||
@Resource
|
||||
private HystrixFallbackHandler hystrixFallbackHandler;
|
||||
|
||||
}
|
||||
|
||||
@ -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,19 @@
|
||||
package org.jeecg.config;
|
||||
|
||||
/**
|
||||
* nocos配置方式枚举
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* 响应超时熔断处理器
|
||||
*
|
||||
* @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("访问超时,请稍后再试!");
|
||||
}
|
||||
}
|
||||
@ -21,12 +21,18 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.a
|
||||
@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 url = exchange.getRequest().getURI().getPath();
|
||||
// log.info(" access url : "+ url);
|
||||
|
||||
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());
|
||||
@ -36,7 +42,7 @@ public class GlobalAccessTokenFilter implements GlobalFilter, Ordered {
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
|
||||
|
||||
//将现在的request,添加当前身份
|
||||
ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").build();
|
||||
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);
|
||||
}
|
||||
|
||||
@ -29,6 +29,6 @@ public class HystrixFallbackHandler implements HandlerFunction<ServerResponse> {
|
||||
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("服务异常"));
|
||||
.header("Content-Type","text/plain; charset=utf-8").body(BodyInserters.fromObject("访问超时,请稍后再试"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
package org.jeecg.handler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.boot.starter.redis.listener.JeecgRedisListerer;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.loader.DynamicRouteLoader;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 路由刷新监听
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LoderRouderHandler implements JeecgRedisListerer {
|
||||
|
||||
@Resource
|
||||
private DynamicRouteLoader dynamicRouteLoader;
|
||||
|
||||
|
||||
@Override
|
||||
public void onMessage(BaseMap message) {
|
||||
dynamicRouteLoader.refresh();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,315 @@
|
||||
package org.jeecg.loader;
|
||||
|
||||
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.lang.StringUtils;
|
||||
import org.jeecg.boot.starter.redis.client.JeecgRedisClient;
|
||||
import org.jeecg.config.GatewayRoutersConfiguration;
|
||||
import org.jeecg.config.RouterDataType;
|
||||
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.InMemoryRouteDefinitionRepository;
|
||||
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 javax.annotation.PostConstruct;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* 动态路由加载器
|
||||
*
|
||||
* @author : zyf
|
||||
* @date :2020-11-10
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@DependsOn({"gatewayRoutersConfiguration"})
|
||||
public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
private InMemoryRouteDefinitionRepository repository;
|
||||
|
||||
private DynamicRouteService dynamicRouteService;
|
||||
|
||||
private ConfigService configService;
|
||||
|
||||
private JeecgRedisClient redisClient;
|
||||
|
||||
|
||||
public DynamicRouteLoader(InMemoryRouteDefinitionRepository repository, DynamicRouteService dynamicRouteService, JeecgRedisClient redisClient) {
|
||||
|
||||
this.repository = repository;
|
||||
this.dynamicRouteService = dynamicRouteService;
|
||||
this.redisClient = redisClient;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||
log.info("初始化路由,dataType:"+ dataType);
|
||||
if (RouterDataType.nacos.toString().endsWith(dataType)) {
|
||||
loadRoutesByNacos();
|
||||
}
|
||||
//从数据库加载路由
|
||||
if (RouterDataType.database.toString().endsWith(dataType)) {
|
||||
loadRoutesByRedis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 刷新路由
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Mono<Void> refresh() {
|
||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||
if (!RouterDataType.yml.toString().endsWith(dataType)) {
|
||||
this.init();
|
||||
// 触发默认路由刷新事件,刷新缓存路由
|
||||
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
log.info("路由已刷新..............");
|
||||
}
|
||||
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(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP, GatewayRoutersConfiguration.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(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从redis中读取路由配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private void loadRoutesByRedis() {
|
||||
List<RouteDefinition> routes = Lists.newArrayList();
|
||||
configService = createConfigService();
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
}
|
||||
String configInfo = redisClient.get("geteway_routes");
|
||||
if (StringUtils.isNotBlank(configInfo)) {
|
||||
log.info("获取网关当前配置:\r\n{}", configInfo);
|
||||
JSONArray array = JSON.parseArray(configInfo);
|
||||
try {
|
||||
routes = getRoutesByJson(array);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
for (RouteDefinition definition : routes) {
|
||||
log.info("update route : {}", definition.toString());
|
||||
dynamicRouteService.add(definition);
|
||||
}
|
||||
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<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
|
||||
List<RouteDefinition> ls = new ArrayList<>();
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject obj = array.getJSONObject(i);
|
||||
RouteDefinition route = new RouteDefinition();
|
||||
route.setId(obj.getString("routerId"));
|
||||
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();
|
||||
predicateDefinition.setName(json.getString("name"));
|
||||
JSONArray jsonArray = json.getJSONArray("args");
|
||||
for (int j = 0; j < jsonArray.size(); j++) {
|
||||
predicateDefinition.addArg("_genkey" + j, jsonArray.get(j).toString());
|
||||
}
|
||||
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<RouteDefinition> definitionList = JSON.parseArray(configInfo, RouteDefinition.class);
|
||||
for (RouteDefinition 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", GatewayRoutersConfiguration.SERVER_ADDR);
|
||||
properties.setProperty("namespace", GatewayRoutersConfiguration.NAMESPACE);
|
||||
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,97 @@
|
||||
package org.jeecg.loader;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
||||
import org.springframework.cloud.gateway.route.InMemoryRouteDefinitionRepository;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
|
||||
import org.springframework.cloud.gateway.support.NotFoundException;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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 RouteDefinitionWriter routeDefinitionWriter;
|
||||
|
||||
@Autowired
|
||||
private InMemoryRouteDefinitionRepository repository;
|
||||
|
||||
/**
|
||||
* 发布事件
|
||||
*/
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.publisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除路由
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public synchronized Mono<ResponseEntity<Object>> delete(String id) {
|
||||
return this.repository.delete(Mono.just(id)).then(Mono.defer(() -> {
|
||||
return Mono.just(ResponseEntity.ok().build());
|
||||
})).onErrorResume((t) -> {
|
||||
return t instanceof NotFoundException;
|
||||
}, (t) -> {
|
||||
return Mono.just(ResponseEntity.notFound().build());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新路由
|
||||
*
|
||||
* @param definition
|
||||
* @return
|
||||
*/
|
||||
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();
|
||||
}
|
||||
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) {
|
||||
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package org.jeecg.loader;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@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;
|
||||
}
|
||||
Reference in New Issue
Block a user