use of com.alibaba.rsocket.metadata.GSVRoutingMetadata in project alibaba-broker-kotlin-example by alibaba-rsocket-broker.
the class UserServiceRSocketTest method testFindById.
@Test
public void testFindById() throws Exception {
RSocketCompositeMetadata compositeMetadata = new RSocketCompositeMetadata();
GSVRoutingMetadata routingMetadata = new GSVRoutingMetadata("", "com.alibaba.user.UserService2", "findById", "");
compositeMetadata.addMetadata(routingMetadata);
MessageMimeTypeMetadata dataEncodingMetadata = new MessageMimeTypeMetadata(WellKnownMimeType.APPLICATION_JSON);
compositeMetadata.addMetadata(dataEncodingMetadata);
rsocket.requestResponse(DefaultPayload.create(Unpooled.wrappedBuffer(objectMapper.writeValueAsBytes(1)), compositeMetadata.getContent())).doOnTerminate(() -> {
ReferenceCountUtil.safeRelease(compositeMetadata);
}).subscribe(payload -> {
System.out.println(payload.getDataUtf8());
});
Thread.sleep(1000);
}
use of com.alibaba.rsocket.metadata.GSVRoutingMetadata in project alibaba-rsocket-broker by alibaba.
the class UpstreamForwardRSocket method requestResponse.
@Override
@NotNull
public Mono<Payload> requestResponse(@NotNull Payload payload) {
BinaryRoutingMetadata binaryRoutingMetadata = binaryRoutingMetadata(payload.metadata());
GSVRoutingMetadata gsvRoutingMetadata;
if (binaryRoutingMetadata != null) {
gsvRoutingMetadata = GSVRoutingMetadata.from(new String(binaryRoutingMetadata.getRoutingText(), StandardCharsets.UTF_8));
} else {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
gsvRoutingMetadata = compositeMetadata.getRoutingMetaData();
if (gsvRoutingMetadata == null) {
return Mono.error(new InvalidException(RsocketErrorCode.message("RST-600404")));
}
}
// request filters
Mono<RSocket> destination = findDestination(gsvRoutingMetadata);
if (this.filterChain.isFiltersPresent()) {
RSocketExchange exchange = new RSocketExchange(FrameType.REQUEST_RESPONSE, gsvRoutingMetadata, payload, this.upstreamBrokerMetadata);
destination = filterChain.filter(exchange).then(destination);
}
// call destination
return destination.flatMap(rsocket -> {
metrics(gsvRoutingMetadata, "0x05");
return rsocket.requestResponse(payload);
});
}
use of com.alibaba.rsocket.metadata.GSVRoutingMetadata in project alibaba-rsocket-broker by alibaba.
the class UpstreamForwardRSocket method fireAndForget.
@Override
@NotNull
public Mono<Void> fireAndForget(@NotNull Payload payload) {
BinaryRoutingMetadata binaryRoutingMetadata = binaryRoutingMetadata(payload.metadata());
GSVRoutingMetadata gsvRoutingMetadata;
if (binaryRoutingMetadata != null) {
gsvRoutingMetadata = GSVRoutingMetadata.from(new String(binaryRoutingMetadata.getRoutingText(), StandardCharsets.UTF_8));
} else {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
gsvRoutingMetadata = compositeMetadata.getRoutingMetaData();
if (gsvRoutingMetadata == null) {
return Mono.error(new InvalidException(RsocketErrorCode.message("RST-600404")));
}
}
// request filters
Mono<RSocket> destination = findDestination(gsvRoutingMetadata);
if (this.filterChain.isFiltersPresent()) {
RSocketExchange exchange = new RSocketExchange(FrameType.REQUEST_FNF, gsvRoutingMetadata, payload, this.upstreamBrokerMetadata);
destination = filterChain.filter(exchange).then(destination);
}
// call destination
return destination.flatMap(rsocket -> {
metrics(gsvRoutingMetadata, "0x05");
return rsocket.fireAndForget(payload);
});
}
use of com.alibaba.rsocket.metadata.GSVRoutingMetadata in project alibaba-rsocket-broker by alibaba.
the class ServiceTestingView method callRSocketService.
public void callRSocketService(String service, String method, @Nullable String jsonData, Pre response) {
Integer handlerId = this.routingSelector.findHandler(ServiceLocator.serviceHashCode(service));
if (handlerId != null) {
RSocketBrokerResponderHandler handler = handlerRegistry.findById(handlerId);
if (handler != null) {
// composite metadata for health check
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(new GSVRoutingMetadata(null, service, method, null), new MessageMimeTypeMetadata(RSocketMimeType.Json));
ByteBuf payLoadData;
if (jsonData == null || jsonData.isEmpty()) {
payLoadData = Unpooled.EMPTY_BUFFER;
} else {
payLoadData = Unpooled.wrappedBuffer(jsonData.getBytes(StandardCharsets.UTF_8));
}
Payload requestPayload = ByteBufPayload.create(payLoadData, compositeMetadata.getContent());
handler.getPeerRsocket().requestResponse(requestPayload).doOnError(throwable -> getUI().ifPresent(ui -> ui.access(() -> {
response.setText(throwable.getMessage());
}))).subscribe(payload -> getUI().ifPresent(ui -> ui.access(() -> {
response.setText(payload.getDataUtf8());
})));
} else {
this.serviceNameFiled.setInvalid(true);
this.serviceNameFiled.setErrorMessage("No Service Provider!");
}
} else {
this.serviceNameFiled.setInvalid(true);
this.serviceNameFiled.setErrorMessage("Service not found!");
}
}
use of com.alibaba.rsocket.metadata.GSVRoutingMetadata in project alibaba-rsocket-broker by alibaba.
the class ServiceQueryController method queryDefinition.
@GetMapping(value = "/definition/{serviceName}")
public Mono<String> queryDefinition(@PathVariable(name = "serviceName") String serviceName) {
Integer handler = routingSelector.findHandler(new ServiceLocator("", serviceName, "").getId());
if (handler != null) {
RSocketBrokerResponderHandler brokerResponderHandler = brokerHandlerRegistry.findById(handler);
if (brokerResponderHandler != null) {
GSVRoutingMetadata routingMetadata = new GSVRoutingMetadata("", ReactiveServiceDiscovery.class.getCanonicalName() + ".findServiceByFullName", "");
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(routingMetadata, jsonMetaEncoding);
ByteBuf bodyBuf = Unpooled.wrappedBuffer(("[\"" + serviceName + "\"]").getBytes(StandardCharsets.UTF_8));
return brokerResponderHandler.getPeerRsocket().requestResponse(ByteBufPayload.create(bodyBuf, compositeMetadata.getContent())).map(Payload::getDataUtf8);
}
}
return Mono.error(new Exception(RsocketErrorCode.message("RST-900404", serviceName)));
}
Aggregations