Search in sources :

Example 1 with InvalidException

use of io.rsocket.exceptions.InvalidException in project kin-rsocket-broker by huangjianqin.

the class RSocketBrokerResponderHandler method fireAndForget.

@SuppressWarnings("ConstantConditions")
@Nonnull
@Override
public Mono<Void> fireAndForget(@Nonnull Payload payload) {
    String frameType = FrameType.REQUEST_FNF.name();
    try {
        BinaryRoutingMetadata binaryRoutingMetadata = BinaryRoutingMetadata.extract(payload.metadata());
        GSVRoutingMetadata gsvRoutingMetadata;
        // 为了兼容, 其余开发者rsocket broker client调用rsocket服务, 缺省部分信息, 也不会导致异常
        boolean encodingMetadataIncluded;
        MessageMimeTypeMetadata messageMimeTypeMetadata;
        if (Objects.isNull(binaryRoutingMetadata)) {
            // 回退到取GSVRoutingMetadata
            RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
            gsvRoutingMetadata = compositeMetadata.getMetadata(RSocketMimeType.ROUTING);
            if (Objects.isNull(gsvRoutingMetadata)) {
                return Mono.error(new InvalidException("no routing metadata"));
            }
            messageMimeTypeMetadata = compositeMetadata.getMetadata(RSocketMimeType.MESSAGE_MIME_TYPE);
            encodingMetadataIncluded = Objects.nonNull(messageMimeTypeMetadata);
        } else {
            gsvRoutingMetadata = binaryRoutingMetadata.toGSVRoutingMetadata();
            // 默认认为带了消息编码元数据
            encodingMetadataIncluded = true;
        }
        // broker local service call
        if (LocalRSocketServiceRegistry.INSTANCE.contains(gsvRoutingMetadata.handlerId())) {
            // app 与 broker通信使用rsocket connector设置的dataMimeType即可
            return localFireAndForget(gsvRoutingMetadata, defaultMessageMimeTypeMetadata, payload);
        }
        // request filters
        Mono<RSocket> destination;
        if (this.filterChain.isFiltersPresent()) {
            RSocketFilterContext filterContext = RSocketFilterContext.of(FrameType.REQUEST_FNF, gsvRoutingMetadata, this.appMetadata, payload);
            // filter可能会改变gsv metadata的数据, 影响路由结果
            destination = filterChain.filter(filterContext).then(findDestination(gsvRoutingMetadata));
        } else {
            destination = findDestination(gsvRoutingMetadata);
        }
        // call destination
        return destination.flatMap(rsocket -> {
            recordServiceInvoke(gsvRoutingMetadata.gsv());
            if (Objects.isNull(binaryRoutingMetadata)) {
                MetricsUtils.metrics(gsvRoutingMetadata, frameType);
            } else {
                ServiceLocator serviceLocator = serviceManager.getServiceLocator(binaryRoutingMetadata.getServiceId());
                MetricsUtils.metrics(serviceLocator, binaryRoutingMetadata.getHandler(), frameType);
            }
            if (encodingMetadataIncluded) {
                return rsocket.fireAndForget(payload);
            } else {
                return rsocket.fireAndForget(payloadWithDataEncoding(payload));
            }
        });
    } catch (Exception e) {
        error(failCallLog(frameType), e);
        ReferenceCountUtil.safeRelease(payload);
        return Mono.error(new InvalidException(failCallTips(frameType, e)));
    }
}
Also used : InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) InvalidException(io.rsocket.exceptions.InvalidException) Nonnull(javax.annotation.Nonnull)

Example 2 with InvalidException

use of io.rsocket.exceptions.InvalidException in project kin-rsocket-broker by huangjianqin.

the class RSocketBrokerResponderHandler method requestChannel.

private Flux<Payload> requestChannel(Payload signal, Flux<Payload> payloads) {
    String frameType = FrameType.REQUEST_CHANNEL.name();
    try {
        BinaryRoutingMetadata binaryRoutingMetadata = BinaryRoutingMetadata.extract(signal.metadata());
        GSVRoutingMetadata gsvRoutingMetadata;
        if (Objects.isNull(binaryRoutingMetadata)) {
            // 回退到取GSVRoutingMetadata
            RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(signal.metadata());
            gsvRoutingMetadata = compositeMetadata.getMetadata(RSocketMimeType.ROUTING);
            if (Objects.isNull(gsvRoutingMetadata)) {
                return Flux.error(new InvalidException("no routing metadata"));
            }
        } else {
            gsvRoutingMetadata = binaryRoutingMetadata.toGSVRoutingMetadata();
        }
        Mono<RSocket> destination = findDestination(gsvRoutingMetadata);
        return destination.flatMapMany(rsocket -> {
            recordServiceInvoke(gsvRoutingMetadata.gsv());
            if (Objects.isNull(binaryRoutingMetadata)) {
                MetricsUtils.metrics(gsvRoutingMetadata, frameType);
            } else {
                ServiceLocator serviceLocator = serviceManager.getServiceLocator(binaryRoutingMetadata.getServiceId());
                MetricsUtils.metrics(serviceLocator, binaryRoutingMetadata.getHandler(), frameType);
            }
            return rsocket.requestChannel(payloads);
        });
    } catch (Exception e) {
        error(failCallLog(frameType), e);
        ReferenceCountUtil.safeRelease(signal);
        payloads.subscribe(ReferenceCountUtil::safeRelease);
        return Flux.error(new InvalidException(failCallTips(frameType, e)));
    }
}
Also used : InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 3 with InvalidException

use of io.rsocket.exceptions.InvalidException in project kin-rsocket-broker by huangjianqin.

the class BrokerRequestHandler method fireAndForget.

@Nonnull
@Override
public Mono<Void> fireAndForget(Payload payload) {
    BinaryRoutingMetadata binaryRoutingMetadata = BinaryRoutingMetadata.extract(payload.metadata());
    GSVRoutingMetadata gsvRoutingMetadata;
    if (binaryRoutingMetadata != null) {
        gsvRoutingMetadata = binaryRoutingMetadata.toGSVRoutingMetadata();
    } else {
        RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
        gsvRoutingMetadata = compositeMetadata.getMetadata(RSocketMimeType.ROUTING);
        if (gsvRoutingMetadata == null) {
            return Mono.error(new InvalidException("No Routing metadata"));
        }
    }
    // request filters
    Mono<RSocket> destination;
    if (this.filterChain.isFiltersPresent()) {
        RSocketFilterContext filterContext = RSocketFilterContext.of(FrameType.REQUEST_FNF, gsvRoutingMetadata, this.upstreamBrokerMetadata, payload);
        // filter可能会改变gsv metadata的数据, 影响路由结果
        destination = filterChain.filter(filterContext).then(findDestination(gsvRoutingMetadata));
    } else {
        destination = findDestination(gsvRoutingMetadata);
    }
    // call destination
    return destination.flatMap(rsocket -> {
        MetricsUtils.metrics(gsvRoutingMetadata, FrameType.REQUEST_FNF.name());
        return rsocket.fireAndForget(payload);
    });
}
Also used : RSocketCompositeMetadata(org.kin.rsocket.core.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) BinaryRoutingMetadata(org.kin.rsocket.core.metadata.BinaryRoutingMetadata) InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) AbstractRSocket(org.kin.rsocket.core.AbstractRSocket) Nonnull(javax.annotation.Nonnull)

Example 4 with InvalidException

use of io.rsocket.exceptions.InvalidException in project kin-rsocket-broker by huangjianqin.

the class BrokerRequestHandler method findDestination.

/**
 * 寻找目标服务provider instance
 */
private Mono<RSocket> findDestination(GSVRoutingMetadata routingMetaData) {
    return Mono.create(sink -> {
        String gsv = routingMetaData.gsv();
        int serviceId = routingMetaData.serviceId();
        RSocketEndpoint targetEndpoint;
        RSocket rsocket = null;
        Exception error = null;
        String endpoint = routingMetaData.getEndpoint();
        if (StringUtils.isNotBlank(endpoint)) {
            targetEndpoint = findDestinationWithEndpoint(endpoint, serviceId);
            if (targetEndpoint == null) {
                error = new InvalidException(String.format("Service not found with endpoint '%s' '%s'", gsv, endpoint));
            }
        } else {
            targetEndpoint = serviceManager.routeByServiceId(serviceId);
            if (Objects.isNull(targetEndpoint)) {
                error = new InvalidException(String.format("Service not found '%s'", gsv));
            }
        }
        if (targetEndpoint != null) {
            rsocket = targetEndpoint;
        }
        if (rsocket != null) {
            sink.success(rsocket);
        } else {
            sink.error(new ApplicationErrorException(String.format("Service not found '%s'", gsv), error));
        }
    });
}
Also used : RSocket(io.rsocket.RSocket) AbstractRSocket(org.kin.rsocket.core.AbstractRSocket) InvalidException(io.rsocket.exceptions.InvalidException) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 5 with InvalidException

use of io.rsocket.exceptions.InvalidException in project kin-rsocket-broker by huangjianqin.

the class BrokerRequestHandler method requestStream.

@Nonnull
@Override
public Flux<Payload> requestStream(Payload payload) {
    BinaryRoutingMetadata binaryRoutingMetadata = BinaryRoutingMetadata.extract(payload.metadata());
    GSVRoutingMetadata gsvRoutingMetadata;
    if (binaryRoutingMetadata != null) {
        gsvRoutingMetadata = binaryRoutingMetadata.toGSVRoutingMetadata();
    } else {
        RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
        gsvRoutingMetadata = compositeMetadata.getMetadata(RSocketMimeType.ROUTING);
        if (gsvRoutingMetadata == null) {
            return Flux.error(new InvalidException("No Routing metadata"));
        }
    }
    // request filters
    Mono<RSocket> destination;
    if (this.filterChain.isFiltersPresent()) {
        RSocketFilterContext filterContext = RSocketFilterContext.of(FrameType.REQUEST_STREAM, gsvRoutingMetadata, this.upstreamBrokerMetadata, payload);
        // filter可能会改变gsv metadata的数据, 影响路由结果
        destination = filterChain.filter(filterContext).then(findDestination(gsvRoutingMetadata));
    } else {
        destination = findDestination(gsvRoutingMetadata);
    }
    return destination.flatMapMany(rsocket -> {
        MetricsUtils.metrics(gsvRoutingMetadata, FrameType.REQUEST_STREAM.name());
        return rsocket.requestStream(payload);
    });
}
Also used : RSocketCompositeMetadata(org.kin.rsocket.core.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) BinaryRoutingMetadata(org.kin.rsocket.core.metadata.BinaryRoutingMetadata) InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) AbstractRSocket(org.kin.rsocket.core.AbstractRSocket) Nonnull(javax.annotation.Nonnull)

Aggregations

InvalidException (io.rsocket.exceptions.InvalidException)32 RSocket (io.rsocket.RSocket)20 Payload (io.rsocket.Payload)9 NotNull (org.jetbrains.annotations.NotNull)9 AbstractRSocket (com.alibaba.rsocket.AbstractRSocket)8 ApplicationErrorException (io.rsocket.exceptions.ApplicationErrorException)8 GSVRoutingMetadata (org.kin.rsocket.core.metadata.GSVRoutingMetadata)7 Flux (reactor.core.publisher.Flux)7 RSocketExchange (com.alibaba.rsocket.RSocketExchange)6 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)6 ByteBufPayload (io.rsocket.util.ByteBufPayload)6 Nonnull (javax.annotation.Nonnull)6 Mono (reactor.core.publisher.Mono)6 CloudEventRSocket (com.alibaba.rsocket.cloudevents.CloudEventRSocket)5 AbstractRSocket (org.kin.rsocket.core.AbstractRSocket)5 BinaryRoutingMetadata (com.alibaba.rsocket.metadata.BinaryRoutingMetadata)4 GSVRoutingMetadata (com.alibaba.rsocket.metadata.GSVRoutingMetadata)4 RSocketCompositeMetadata (com.alibaba.rsocket.metadata.RSocketCompositeMetadata)4 ReactiveMethodHandler (com.alibaba.rsocket.rpc.ReactiveMethodHandler)4 BinaryRoutingMetadata (org.kin.rsocket.core.metadata.BinaryRoutingMetadata)4