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)));
}
}
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)));
}
}
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);
});
}
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));
}
});
}
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);
});
}
Aggregations