Merge remote-tracking branch 'origin/fix-bug-202303' into fix-bug-202303
This commit is contained in:
+7
-8
@@ -6,7 +6,6 @@ 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 javax.annotation.Resource;
|
||||
|
||||
@@ -19,16 +18,16 @@ public class JeroGatewayApplication implements CommandLineRunner {
|
||||
private DynamicRouteLoader dynamicRouteLoader;
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext applicationContext = SpringApplication.run(JeroGatewayApplication.class, args);
|
||||
SpringApplication.run(JeroGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 容器初始化后加载路由
|
||||
* @param strings
|
||||
*/
|
||||
@Override
|
||||
public void run(String... strings) {
|
||||
/**
|
||||
* 容器初始化后加载路由
|
||||
* @param strings
|
||||
*/
|
||||
@Override
|
||||
public void run(String... strings) {
|
||||
dynamicRouteLoader.refresh();
|
||||
}
|
||||
|
||||
|
||||
+7
-4
@@ -3,17 +3,20 @@ package com.jero.config;
|
||||
/**
|
||||
* nocos配置方式枚举
|
||||
*/
|
||||
public enum RouterDataType {
|
||||
public class RouterDataType {
|
||||
private RouterDataType(){
|
||||
|
||||
}
|
||||
/**
|
||||
* 数据库加载路由配置
|
||||
*/
|
||||
database,
|
||||
public static final String DATABASE = "database";
|
||||
/**
|
||||
* 本地yml加载路由配置
|
||||
*/
|
||||
yml,
|
||||
public static final String YML = "yml";
|
||||
/**
|
||||
* nacos加载路由配置
|
||||
*/
|
||||
nacos
|
||||
public static final String NACOS = "nacos";
|
||||
}
|
||||
|
||||
-3
@@ -27,14 +27,11 @@ 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());
|
||||
|
||||
+5
-3
@@ -5,14 +5,16 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
*/
|
||||
@Configuration
|
||||
public class SentinelFilterContextConfig {
|
||||
@Bean
|
||||
public FilterRegistrationBean sentinelFilterRegistration() {
|
||||
FilterRegistrationBean registration = new FilterRegistrationBean();
|
||||
public FilterRegistrationBean<Filter> sentinelFilterRegistration() {
|
||||
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(new CommonFilter());
|
||||
registration.addUrlPatterns("/*");
|
||||
// 入口资源关闭聚合
|
||||
@@ -21,4 +23,4 @@ public class SentinelFilterContextConfig {
|
||||
registration.setOrder(1);
|
||||
return registration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+40
-35
@@ -48,7 +48,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
// private InMemoryRouteDefinitionRepository repository;
|
||||
private InMemoryRouteDefinitionRepository repository;
|
||||
|
||||
private DynamicRouteService dynamicRouteService;
|
||||
|
||||
@@ -59,14 +59,9 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
private static String updateRouteText = "update route : {}";
|
||||
|
||||
|
||||
/*public DynamicRouteLoader(InMemoryRouteDefinitionRepository repository, DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
public DynamicRouteLoader(InMemoryRouteDefinitionRepository repository, DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
|
||||
this.repository = repository;
|
||||
this.dynamicRouteService = dynamicRouteService;
|
||||
this.redisUtil = redisUtil;
|
||||
}*/
|
||||
public DynamicRouteLoader(DynamicRouteService dynamicRouteService, RedisUtil redisUtil) {
|
||||
|
||||
this.dynamicRouteService = dynamicRouteService;
|
||||
this.redisUtil = redisUtil;
|
||||
}
|
||||
@@ -75,11 +70,11 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
public void init() {
|
||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||
log.info("初始化路由,dataType:"+ dataType);
|
||||
if (RouterDataType.nacos.toString().endsWith(dataType)) {
|
||||
if (RouterDataType.NACOS.endsWith(dataType)) {
|
||||
loadRoutesByNacos();
|
||||
}
|
||||
//从数据库加载路由
|
||||
if (RouterDataType.database.toString().endsWith(dataType)) {
|
||||
if (RouterDataType.DATABASE.endsWith(dataType)) {
|
||||
loadRoutesByRedis();
|
||||
}
|
||||
}
|
||||
@@ -92,7 +87,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
*/
|
||||
public Mono<Void> refresh() {
|
||||
String dataType = GatewayRoutersConfiguration.DATA_TYPE;
|
||||
if (!RouterDataType.yml.toString().endsWith(dataType)) {
|
||||
if (!RouterDataType.YML.endsWith(dataType)) {
|
||||
this.init();
|
||||
}
|
||||
return Mono.empty();
|
||||
@@ -110,9 +105,10 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
try {
|
||||
String configInfo = "";
|
||||
if (configService == null) {
|
||||
log.warn("initConfigService fail");
|
||||
log.warn("initConfigService fail");
|
||||
}else {
|
||||
configInfo = configService.getConfig(GatewayRoutersConfiguration.DATA_ID, GatewayRoutersConfiguration.ROUTE_GROUP, GatewayRoutersConfiguration.DEFAULT_TIMEOUT);
|
||||
}
|
||||
else 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);
|
||||
@@ -189,17 +185,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
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);
|
||||
}
|
||||
List<PredicateDefinition> predicateDefinitionList = getPredicateDefinitions(list);
|
||||
route.setPredicates(predicateDefinitionList);
|
||||
}
|
||||
|
||||
@@ -208,18 +194,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
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);
|
||||
}
|
||||
addFilterDefinition(list, filterDefinitionList);
|
||||
route.setFilters(filterDefinitionList);
|
||||
}
|
||||
}
|
||||
@@ -228,6 +203,36 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||
return ls;
|
||||
}
|
||||
|
||||
private static void addFilterDefinition(JSONArray list, List<FilterDefinition> filterDefinitionList) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<PredicateDefinition> getPredicateDefinitions(JSONArray list) {
|
||||
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);
|
||||
}
|
||||
return predicateDefinitionList;
|
||||
}
|
||||
|
||||
|
||||
// private void loadRoutesByDataBase() {
|
||||
// List<GatewayRouteVo> routeList = jdbcTemplate.query(SELECT_ROUTES, new RowMapper<GatewayRouteVo>() {
|
||||
|
||||
+5
-2
@@ -3,12 +3,15 @@ package com.jero.modules.cloud.constant;
|
||||
/**
|
||||
* 微服务单元测试常量定义
|
||||
*/
|
||||
public interface CloudConstant {
|
||||
public class CloudConstant {
|
||||
private CloudConstant(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 微服务名【对应模块jero-boot-module-demo】
|
||||
*/
|
||||
public static final String SERVER_NAME_JeroDemo = "jero-demo";
|
||||
public static final String SERVER_NAME_JERO_DEMO = "jero-demo";
|
||||
|
||||
/**
|
||||
* MQ测试队列名字
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ public class JeroTestFeignController {
|
||||
@GetMapping("getMessage2")
|
||||
@ApiOperation(value = "测试动态feign", notes = "测试动态feign")
|
||||
public Result<String> getMessage2() {
|
||||
JeroTestClientDyn myClientDyn = jeroFeignService.newInstance(JeroTestClientDyn.class, CloudConstant.SERVER_NAME_JeroDemo);
|
||||
JeroTestClientDyn myClientDyn = jeroFeignService.newInstance(JeroTestClientDyn.class, CloudConstant.SERVER_NAME_JERO_DEMO);
|
||||
return myClientDyn.getMessage("动态fegin——jero-boot2");
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
/**
|
||||
* 常规feign接口定义
|
||||
*/
|
||||
@FeignClient(value = CloudConstant.SERVER_NAME_JeroDemo, fallbackFactory = JeroTestClientFallback.class)
|
||||
@FeignClient(value = CloudConstant.SERVER_NAME_JERO_DEMO, fallbackFactory = JeroTestClientFallback.class)
|
||||
@Component
|
||||
public interface JeroTestClient {
|
||||
|
||||
|
||||
+3
-4
@@ -1,16 +1,14 @@
|
||||
package com.jero.modules.cloud.lock;
|
||||
|
||||
import com.jero.boot.starter.lock.annotation.JLock;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.jero.boot.starter.lock.client.RedissonLockClient;
|
||||
import com.jero.boot.starter.rabbitmq.client.RabbitMqClient;
|
||||
import com.jero.common.base.BaseMap;
|
||||
import com.jero.modules.cloud.constant.CloudConstant;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -34,7 +32,7 @@ public class DemoLockTest {
|
||||
log.info("执行execute任务开始,休眠三秒");
|
||||
Thread.sleep(3000);
|
||||
log.info("=======================业务逻辑1=============================");
|
||||
Map map = new BaseMap();
|
||||
Map<String,Object> map = new BaseMap();
|
||||
map.put("orderId", "BJ0001");
|
||||
rabbitMqClient.sendMessage(CloudConstant.MQ_JERO_PLACE_ORDER, map);
|
||||
//延迟10秒发送
|
||||
@@ -44,6 +42,7 @@ public class DemoLockTest {
|
||||
}
|
||||
|
||||
public DemoLockTest() {
|
||||
// do other
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user