Spring Cloud Gateway环境搭建和配置(二)

2023-04-10 13:33:01 浏览数 (2)

除了 YAML 或 Properties 文件外,还可以通过编写 Java 代码的方式进行配置。以下是一个简单的配置示例:

代码语言:javascript复制
@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("backend-service", r -> r.path("/backend/**").uri("http://localhost:8081"))
                .route("frontend-service", r -> r.path("/frontend/**").uri("lb://frontend-service"))
                .build();
    }
    
    @Bean
    public CorsGlobalFilter corsGlobalFilter() {
        return new CorsGlobalFilter();
    }
    
    private static class CorsGlobalFilter implements GlobalFilter, Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ServerHttpResponse response = exchange.getResponse();
            HttpHeaders headers = response.getHeaders();
            headers.add("Access-Control-Allow-Origin", "*");
            headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
            headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
            headers.add("Access-Control-Allow-Credentials", "true");
            if (exchange.getRequest().getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
            return chain.filter(exchange);
        }
        
        @Override
        public int getOrder() {
            return Ordered.HIGHEST_PRECEDENCE;
        }
    }
}

在这个示例代码中,我们通过 Java 代码的方式定义了两个路由,并实现了一个全局的 CORS Filter,将一些允除了 YAML 或 Properties 文件外,还可以通过编写 Java 代码的方式进行配置。以下是一个简单的配置示例:

代码语言:javascript复制
@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("backend-service", r -> r.path("/backend/**").uri("http://localhost:8081"))
                .route("frontend-service", r -> r.path("/frontend/**").uri("lb://frontend-service"))
                .build();
    }
    
    @Bean
    public CorsGlobalFilter corsGlobalFilter() {
        return new CorsGlobalFilter();
    }
    
    private static class CorsGlobalFilter implements GlobalFilter, Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ServerHttpResponse response = exchange.getResponse();
            HttpHeaders headers = response.getHeaders();
            headers.add("Access-Control-Allow-Origin", "*");
            headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
            headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization");
            headers.add("Access-Control-Allow-Credentials", "true");
            if (exchange.getRequest().getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
            return chain.filter(exchange);
        }
        
        @Override
        public int getOrder() {
            return Ordered.HIGHEST_PRECEDENCE;
        }
    }
}

在这个示例代码中,我们通过 Java 代码的方式定义了两个路由,并实现了一个全局的 CORS Filter,将一些允许的请求头和方法添加到响应中。

除了上述基本配置外,Spring Cloud Gateway 还提供了丰富的插件机制,可以根据需要自定义 Gateway Handler 和 Gateway Filter。以下是一个简单的自定义 Gateway Filter 的示例代码:

代码语言:javascript复制
@Component
public class CustomFilter implements GatewayFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        HttpHeaders headers = response.getHeaders();
        headers.add("Custom-Header", "Custom-Value");
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

在这个示例代码中,我们定义了一个 CustomFilter 类,并实现了 GatewayFilter 接口。在 filter 方法中,我们向响应头中添加了一个自定义的响应头。在 getOrder 方法中,我们指定了该过滤器的优先级为 0。

在实际开发中,我们可以根据需要编写自定义的 Gateway Handler 和 Gateway Filter,并将它们添加到 Gateway 中,实现更加灵活的请求处理和响应处理。

0 人点赞