Merge branch 'main' of http://115.190.8.52:3000/sczx_group/sczx_gateway
This commit is contained in:
@ -2,9 +2,12 @@ FROM openjdk:8-jdk
|
||||
COPY target/*.jar app.jar
|
||||
|
||||
ENTRYPOINT ["java", "-jar", \
|
||||
"-Xms128m", \
|
||||
"-Xmx128m", \
|
||||
"-Xms256m", \
|
||||
"-Xmx256m", \
|
||||
"-XX:MaxMetaspaceSize=128m", \
|
||||
"-Xss256k", \
|
||||
"-XX:+UseContainerSupport", \
|
||||
"-Xloggc:/app/logs/gc.log", \
|
||||
"-XX:+PrintGCDetails", \
|
||||
"-XX:+PrintGCTimeStamps", \
|
||||
"app.jar"]
|
||||
7
Jenkinsfile
vendored
7
Jenkinsfile
vendored
@ -69,11 +69,12 @@ pipeline {
|
||||
--name \${CONTAINER_NAME} \
|
||||
--network sczx-net \
|
||||
-p 8089:8089 \
|
||||
-e JAVA_OPTS="-Xms128m -Xmx256m -Duser.timezone=Asia/Shanghai" \
|
||||
-e JAVA_OPTS="-Xms256m -Xmx256m -Duser.timezone=Asia/Shanghai" \
|
||||
-e TZ=Asia/Shanghai \
|
||||
-v /etc/localtime:/etc/localtime:ro \
|
||||
--memory="300m" \
|
||||
--memory-swap="300m" \
|
||||
-v /root/log/sczx-gateway:/app/logs \
|
||||
--memory="512m" \
|
||||
--memory-swap="512m" \
|
||||
--restart always \
|
||||
\${DOCKER_IMAGE}
|
||||
"""
|
||||
|
||||
@ -19,7 +19,7 @@ public class DynamicRouteScheduler {
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
// 延迟5秒后开始执行,避免与启动时的自动刷新冲突,每30秒刷新一次路由(可按需调整)
|
||||
@Scheduled(fixedRate = 30_000, initialDelay = 5_000)
|
||||
// @Scheduled(fixedRate = 60_000, initialDelay = 10_000)
|
||||
public void refreshRoutes() {
|
||||
log.info("refreshRoutes");
|
||||
publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||
|
||||
@ -39,52 +39,60 @@ public class DynamicRouteConfig implements RouteDefinitionLocator {
|
||||
private final AtomicReference<Long> lastRefreshTime = new AtomicReference<>(0L);
|
||||
|
||||
// 最小刷新间隔(毫秒)
|
||||
private static final long MIN_REFRESH_INTERVAL = 3000;
|
||||
private static final long MIN_REFRESH_INTERVAL = 120000;
|
||||
|
||||
private static final URI DEFAULT_URI = URI.create("http://localhost");
|
||||
|
||||
@Override
|
||||
public Flux<RouteDefinition> getRouteDefinitions() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long lastTime = lastRefreshTime.get();
|
||||
try{
|
||||
long lastTime = lastRefreshTime.get();
|
||||
|
||||
// 防抖处理:如果距离上次刷新时间太短,则不处理
|
||||
if (currentTime - lastTime < MIN_REFRESH_INTERVAL && refreshCount.get() > 0) {
|
||||
log.info("刷新过于频繁,跳过本次刷新");
|
||||
return Flux.empty();
|
||||
}
|
||||
lastRefreshTime.set(currentTime);
|
||||
log.info("开始加载路由定义,第{}次调用", refreshCount.incrementAndGet());
|
||||
List<RouteDefinition> routeDefinitions = new ArrayList<>();
|
||||
|
||||
// 定义需要路由的服务列表
|
||||
String[] serviceNames = {"sczx-user", "sczx-store", "sczx-car", "sczx-order", "sczx-sync", "sczx-singlepay"};
|
||||
|
||||
// 为每个服务添加路由规则(如果服务存在)
|
||||
for (String serviceName : serviceNames) {
|
||||
if (hasServiceInstances(serviceName)) {
|
||||
RouteDefinition routeDefinition = new RouteDefinition();
|
||||
routeDefinition.setId(serviceName);
|
||||
|
||||
// 设置路径断言
|
||||
routeDefinition.setPredicates(Collections.singletonList(
|
||||
new PredicateDefinition("Path=/zc/" + serviceName.split("-")[1] + "/**")
|
||||
));
|
||||
|
||||
// 设置过滤器
|
||||
routeDefinition.setFilters(Collections.singletonList(
|
||||
new org.springframework.cloud.gateway.filter.FilterDefinition(
|
||||
"RewritePath=/zc/" + serviceName.split("-")[1] + "/(?<segment>.*), /$\\{segment}")
|
||||
));
|
||||
|
||||
// 设置URI
|
||||
routeDefinition.setUri(getServiceUri(serviceName));
|
||||
|
||||
routeDefinitions.add(routeDefinition);
|
||||
} else {
|
||||
log.warn("服务 {} 不存在实例,跳过路由配置", serviceName);
|
||||
// 防抖处理:如果距离上次刷新时间太短,则不处理
|
||||
if (currentTime - lastTime < MIN_REFRESH_INTERVAL && refreshCount.get() > 0) {
|
||||
log.info("刷新过于频繁,跳过本次刷新");
|
||||
return Flux.empty();
|
||||
}
|
||||
lastRefreshTime.set(currentTime);
|
||||
log.info("开始加载路由定义,第{}次调用", refreshCount.incrementAndGet());
|
||||
List<RouteDefinition> routeDefinitions = new ArrayList<>();
|
||||
|
||||
// 定义需要路由的服务列表
|
||||
String[] serviceNames = {"sczx-user", "sczx-store", "sczx-car", "sczx-order", "sczx-sync", "sczx-singlepay"};
|
||||
|
||||
// 为每个服务添加路由规则(如果服务存在)
|
||||
for (String serviceName : serviceNames) {
|
||||
if (hasServiceInstances(serviceName)) {
|
||||
RouteDefinition routeDefinition = new RouteDefinition();
|
||||
routeDefinition.setId(serviceName);
|
||||
|
||||
// 设置路径断言
|
||||
routeDefinition.setPredicates(Collections.singletonList(
|
||||
new PredicateDefinition("Path=/zc/" + serviceName.split("-")[1] + "/**")
|
||||
));
|
||||
|
||||
// 设置过滤器
|
||||
routeDefinition.setFilters(Collections.singletonList(
|
||||
new org.springframework.cloud.gateway.filter.FilterDefinition(
|
||||
"RewritePath=/zc/" + serviceName.split("-")[1] + "/(?<segment>.*), /$\\{segment}")
|
||||
));
|
||||
|
||||
// 设置URI
|
||||
routeDefinition.setUri(getServiceUri(serviceName));
|
||||
|
||||
routeDefinitions.add(routeDefinition);
|
||||
} else {
|
||||
log.warn("服务 {} 不存在实例,跳过路由配置", serviceName);
|
||||
}
|
||||
}
|
||||
log.info("路由加载完成,共加载 {} 个路由定义", routeDefinitions.size());
|
||||
return Flux.fromIterable(routeDefinitions);
|
||||
}catch (Exception e){
|
||||
log.error("路由加载失败", e);
|
||||
}
|
||||
log.info("路由加载完成,共加载 {} 个路由定义", routeDefinitions.size());
|
||||
return Flux.fromIterable(routeDefinitions);
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +101,9 @@ public class DynamicRouteConfig implements RouteDefinitionLocator {
|
||||
@EventListener
|
||||
public void onRefreshRoutes(RefreshRoutesEvent event) {
|
||||
long count = refreshCount.get();
|
||||
log.info("接收到路由刷新事件,当前为第{}次刷新", count);
|
||||
log.info("接收到路由刷新事件,当前为第{}次刷新, JVM内存使用: {}/{}", count,
|
||||
Runtime.getRuntime().freeMemory(),
|
||||
Runtime.getRuntime().totalMemory());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,10 +122,10 @@ public class DynamicRouteConfig implements RouteDefinitionLocator {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
||||
if (instances == null || instances.isEmpty()) {
|
||||
log.warn("No available instances for service: {}, returning default URI", serviceName);
|
||||
return URI.create("http://localhost");
|
||||
return DEFAULT_URI;
|
||||
}
|
||||
|
||||
ServiceInstance selected = instances.get(Math.abs(index.getAndIncrement()) % instances.size());
|
||||
int idx = index.getAndIncrement() & Integer.MAX_VALUE;
|
||||
ServiceInstance selected = instances.get(idx % instances.size());
|
||||
log.info("Selected instance: {}", selected);
|
||||
return URI.create("http://" + selected.getHost() + ":" + selected.getPort());
|
||||
}
|
||||
|
||||
@ -29,6 +29,7 @@ spring:
|
||||
discovery:
|
||||
server-addr: 115.190.8.52:8848 # Nacos 地址
|
||||
group: DEFAULT_GROUP
|
||||
watch-delay: 120000 # 60秒刷新一次
|
||||
loadbalancer:
|
||||
ribbon:
|
||||
enabled: false
|
||||
@ -47,8 +48,8 @@ auth:
|
||||
secret-key: his-is-a-very-long-and-secure-secret-key-for-jwt-signing-please-dont-use-short-keys
|
||||
token-expiration: 86400000 # 24小时
|
||||
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: DEBUG
|
||||
org.springframework.cloud.loadbalancer: DEBUG
|
||||
reactor.core.publisher: DEBUG
|
||||
#logging:
|
||||
# level:
|
||||
# org.springframework.cloud.gateway: DEBUG
|
||||
# org.springframework.cloud.loadbalancer: DEBUG
|
||||
# reactor.core.publisher: DEBUG
|
||||
|
||||
Reference in New Issue
Block a user