增加sczx_order服务的路由映射,增加请求响应的日志
This commit is contained in:
@ -2,6 +2,7 @@ package com.sczx.gateway.component;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
@ -14,6 +15,9 @@ public class DynamicRouteScheduler {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationEventPublisher publisher;
|
private ApplicationEventPublisher publisher;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
// 每30秒刷新一次路由(可按需调整)
|
// 每30秒刷新一次路由(可按需调整)
|
||||||
@Scheduled(fixedRate = 30_000)
|
@Scheduled(fixedRate = 30_000)
|
||||||
public void refreshRoutes() {
|
public void refreshRoutes() {
|
||||||
|
|||||||
@ -33,6 +33,9 @@ public class CustomRouteConfig {
|
|||||||
.route("sczx-car", r -> r.path("/zc/car/**")
|
.route("sczx-car", r -> r.path("/zc/car/**")
|
||||||
.filters(f -> f.rewritePath("/zc/car/(?<segment>.*)", "/${segment}"))
|
.filters(f -> f.rewritePath("/zc/car/(?<segment>.*)", "/${segment}"))
|
||||||
.uri(uriWithCustomLoadBalance("sczx-car")))
|
.uri(uriWithCustomLoadBalance("sczx-car")))
|
||||||
|
.route("sczx-order", r -> r.path("/zc/order/**")
|
||||||
|
.filters(f -> f.rewritePath("/zc/order/(?<segment>.*)", "/${segment}"))
|
||||||
|
.uri(uriWithCustomLoadBalance("sczx-order")))
|
||||||
.build();
|
.build();
|
||||||
// .route("sczx_user", r -> r.path("/zc/user/**")
|
// .route("sczx_user", r -> r.path("/zc/user/**")
|
||||||
// .uri("lb://sczx_user"))
|
// .uri("lb://sczx_user"))
|
||||||
@ -44,6 +47,7 @@ public class CustomRouteConfig {
|
|||||||
|
|
||||||
|
|
||||||
private URI uriWithCustomLoadBalance(String serviceName) {
|
private URI uriWithCustomLoadBalance(String serviceName) {
|
||||||
|
log.info("{} ,开始负载均衡路由", serviceName);
|
||||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
||||||
if (instances == null || instances.isEmpty()) {
|
if (instances == null || instances.isEmpty()) {
|
||||||
throw new RuntimeException("No available instances for service: " + serviceName);
|
throw new RuntimeException("No available instances for service: " + serviceName);
|
||||||
|
|||||||
Reference in New Issue
Block a user