聊聊spring cloud gateway的streaming-media-types属性

本文主要研究下spring cloud gateway的streaming-media-types属性

配置

配置说明

    {
      "sourceType": "org.springframework.cloud.gateway.config.GatewayProperties",
      "name": "spring.cloud.gateway.streaming-media-types",
      "type": "java.util.List<org.springframework.http.MediaType>"
    }

GatewayProperties

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config/GatewayProperties.java

@ConfigurationProperties("spring.cloud.gateway")
@Validated
public class GatewayProperties {

    /**
     * List of Routes
     */
    @NotNull
    @Valid
    private List<RouteDefinition> routes = new ArrayList<>();

    /**
     * List of filter definitions that are applied to every route.
     */
    private List<FilterDefinition> defaultFilters = new ArrayList<>();

    private List<MediaType> streamingMediaTypes = Arrays.asList(MediaType.TEXT_EVENT_STREAM,
            MediaType.APPLICATION_STREAM_JSON);

    public List<RouteDefinition> getRoutes() {
        return routes;
    }

    public void setRoutes(List<RouteDefinition> routes) {
        this.routes = routes;
    }

    public List<FilterDefinition> getDefaultFilters() {
        return defaultFilters;
    }

    public void setDefaultFilters(List<FilterDefinition> defaultFilters) {
        this.defaultFilters = defaultFilters;
    }

    public List<MediaType> getStreamingMediaTypes() {
        return streamingMediaTypes;
    }

    public void setStreamingMediaTypes(List<MediaType> streamingMediaTypes) {
        this.streamingMediaTypes = streamingMediaTypes;
    }

    @Override
    public String toString() {
        return "GatewayProperties{" +
                "routes=" + routes +
                ", defaultFilters=" + defaultFilters +
                ", streamingMediaTypes=" + streamingMediaTypes +
                '}';
    }
}

可以看到默认是MediaType.TEXT_EVENT_STREAM(text/event-stream)、MediaType.APPLICATION_STREAM_JSON(application/stream+json)

使用

GatewayAutoConfiguration

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

    @Configuration
    @ConditionalOnClass(HttpClient.class)
    protected static class NettyConfiguration {
        @Bean
        @ConditionalOnMissingBean
        public HttpClient httpClient(@Qualifier("nettyClientOptions") Consumer<? super HttpClientOptions.Builder> options) {
            return HttpClient.create(options);
        }

        //......

        @Bean
        public HttpClientProperties httpClientProperties() {
            return new HttpClientProperties();
        }

        @Bean
        public NettyRoutingFilter routingFilter(HttpClient httpClient,
                                                ObjectProvider<List<HttpHeadersFilter>> headersFilters) {
            return new NettyRoutingFilter(httpClient, headersFilters);
        }

        @Bean
        public NettyWriteResponseFilter nettyWriteResponseFilter(GatewayProperties properties) {
            return new NettyWriteResponseFilter(properties.getStreamingMediaTypes());
        }

        @Bean
        public ReactorNettyWebSocketClient reactorNettyWebSocketClient(@Qualifier("nettyClientOptions") Consumer<? super HttpClientOptions.Builder> options) {
            return new ReactorNettyWebSocketClient(options);
        }
    }

这里的NettyWriteResponseFilter使用到了properties.getStreamingMediaTypes()

NettyWriteResponseFilter

spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/filter/NettyWriteResponseFilter.javac

public class NettyWriteResponseFilter implements GlobalFilter, Ordered {

    private static final Log log = LogFactory.getLog(NettyWriteResponseFilter.class);

    public static final int WRITE_RESPONSE_FILTER_ORDER = -1;

    private final List<MediaType> streamingMediaTypes;

    public NettyWriteResponseFilter(List<MediaType> streamingMediaTypes) {
        this.streamingMediaTypes = streamingMediaTypes;
    }

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

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added
        // until the WebHandler is run
        return chain.filter(exchange).then(Mono.defer(() -> {
            HttpClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR);

            if (clientResponse == null) {
                return Mono.empty();
            }
            log.trace("NettyWriteResponseFilter start");
            ServerHttpResponse response = exchange.getResponse();

            NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory();
            //TODO: what if it's not netty

            final Flux<NettyDataBuffer> body = clientResponse.receive()
                    .retain() //TODO: needed?
                    .map(factory::wrap);

            MediaType contentType = response.getHeaders().getContentType();
            return (isStreamingMediaType(contentType) ?
                    response.writeAndFlushWith(body.map(Flux::just)) : response.writeWith(body));
        }));
    }

    //TODO: use framework if possible
    //TODO: port to WebClientWriteResponseFilter
    private boolean isStreamingMediaType(@Nullable MediaType contentType) {
        return (contentType != null && this.streamingMediaTypes.stream()
                        .anyMatch(contentType::isCompatibleWith));
    }

}

可以看到这里根据isStreamingMediaType方法判断是否是stream类型,如果是则采用writeAndFlushWith方法,不是则采用writeWith方法

ReactiveHttpOutputMessage

spring-web-5.0.6.RELEASE-sources.jar!/org/springframework/http/ReactiveHttpOutputMessage.java

/**
 * A "reactive" HTTP output message that accepts output as a {@link Publisher}.
 *
 * <p>Typically implemented by an HTTP request on the client-side or an
 * HTTP response on the server-side.
 *
 * @author Arjen Poutsma
 * @author Sebastien Deleuze
 * @since 5.0
 */
public interface ReactiveHttpOutputMessage extends HttpMessage {

    /**
     * Return a {@link DataBufferFactory} that can be used to create the body.
     * @return a buffer factory
     * @see #writeWith(Publisher)
     */
    DataBufferFactory bufferFactory();

    /**
     * Register an action to apply just before the HttpOutputMessage is committed.
     * <p><strong>Note:</strong> the supplied action must be properly deferred,
     * e.g. via {@link Mono#defer} or {@link Mono#fromRunnable}, to ensure it's
     * executed in the right order, relative to other actions.
     * @param action the action to apply
     */
    void beforeCommit(Supplier<? extends Mono<Void>> action);

    /**
     * Whether the HttpOutputMessage is committed.
     */
    boolean isCommitted();

    /**
     * Use the given {@link Publisher} to write the body of the message to the
     * underlying HTTP layer.
     * @param body the body content publisher
     * @return a {@link Mono} that indicates completion or error
     */

    Mono<Void> writeWith(Publisher<? extends DataBuffer> body);

    /**
     * Use the given {@link Publisher} of {@code Publishers} to write the body
     * of the HttpOutputMessage to the underlying HTTP layer, flushing after
     * each {@code Publisher<DataBuffer>}.
     * @param body the body content publisher
     * @return a {@link Mono} that indicates completion or error
     */
    Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body);

    /**
     * Indicate that message handling is complete, allowing for any cleanup or
     * end-of-processing tasks to be performed such as applying header changes
     * made via {@link #getHeaders()} to the underlying HTTP message (if not
     * applied already).
     * <p>This method should be automatically invoked at the end of message
     * processing so typically applications should not have to invoke it.
     * If invoked multiple times it should have no side effects.
     * @return a {@link Mono} that indicates completion or error
     */
    Mono<Void> setComplete();

}

从接口的注释可以看到,writeWith与writeAndFlushWith的参数泛型不同,一个是Publisher<? extends DataBuffer>,一个是Publisher<? extends Publisher<? extends DataBuffer>>。而writeAndFlushWith则是在每个Publisher写入之后就flush。

小结

NettyWriteResponseFilter根据spring.cloud.gateway.streaming-media-types配置的类型来判断是writeAndFlushWith还是writeWith,如果是指定类型则选择用writeAndFlushWith写入response。默认该配置指定了MediaType.TEXT_EVENT_STREAM(text/event-stream)、MediaType.APPLICATION_STREAM_JSON(application/stream+json)这两种类型。

doc

  • 113.5 Netty Write Response Filter

原文发布于微信公众号 - 码匠的流水账(geek_luandun)

原文发表时间:2018-06-07

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

How to build Multi-Language Web Sites with ASP.NET 2.0 and VS.Net 2005

Introduction: In order to reach international markets through the Internet, sup...

24080
来自专栏c#开发者

Configure Many-to-Many relationship:

Configure Many-to-Many relationship: Here, we will learn how to configure Many-t...

358110
来自专栏Hongten

HIbernate 中的 继承映射

数据库中Delivery表:id,recipient,phone,address,postcode,delivery_type,parcel_number,ex...

7410
来自专栏猿人谷

c/c++ 宏中"#"和"##"的用法

c/c++ 宏中"#"和"##"的用法 一、一般用法 我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. 用法: #i nclude<cstdio...

22750
来自专栏算法修养

POJ-2777 Count Color(线段树,区间染色问题)

Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

35840
来自专栏码匠的流水账

springboot2上报metrics到statsd

micrometer-registry-statsd-1.0.1-sources.jar!/io/micrometer/statsd/StatsdFlavor....

31210
来自专栏码匠的流水账

聊聊resilience4j的CircuitBreakerStateMachine

本文主要研究一下resilience4j的CircuitBreakerStateMachine

13620
来自专栏一个会写诗的程序员的博客

《JSP极简教程》c:forEach 如何输出序号

11930
来自专栏码匠的流水账

聊聊storm的submitTopology

storm-core-1.1.0-sources.jar!/org/apache/storm/StormSubmitter.java

15650
来自专栏码匠的流水账

spring mvc如何计算BEST_MATCHING_PATTERN_ATTRIBUTE

spring-webmvc-4.3.10.RELEASE-sources.jar!/org/springframework/web/servlet/Dispat...

16120

扫码关注云+社区

领取腾讯云代金券