Search in sources :

Example 6 with GSVRoutingMetadata

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);
}
Also used : RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata)

Example 7 with GSVRoutingMetadata

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);
    });
}
Also used : RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) RSocketExchange(com.alibaba.rsocket.RSocketExchange) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) BinaryRoutingMetadata(com.alibaba.rsocket.metadata.BinaryRoutingMetadata) InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) AbstractRSocket(com.alibaba.rsocket.AbstractRSocket) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with GSVRoutingMetadata

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);
    });
}
Also used : RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) RSocketExchange(com.alibaba.rsocket.RSocketExchange) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) BinaryRoutingMetadata(com.alibaba.rsocket.metadata.BinaryRoutingMetadata) InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) AbstractRSocket(com.alibaba.rsocket.AbstractRSocket) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GSVRoutingMetadata

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!");
    }
}
Also used : RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) TextArea(com.vaadin.flow.component.textfield.TextArea) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Autowired(org.springframework.beans.factory.annotation.Autowired) RSocketBrokerHandlerRegistry(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerHandlerRegistry) Route(com.vaadin.flow.router.Route) Unpooled(io.netty.buffer.Unpooled) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata) ByteBuf(io.netty.buffer.ByteBuf) RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) TextField(com.vaadin.flow.component.textfield.TextField) ByteBufPayload(io.rsocket.util.ByteBufPayload) RSocketMimeType(com.alibaba.rsocket.metadata.RSocketMimeType) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H3(com.vaadin.flow.component.html.H3) ServiceRoutingSelector(com.alibaba.spring.boot.rsocket.broker.route.ServiceRoutingSelector) H4(com.vaadin.flow.component.html.H4) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) StandardCharsets(java.nio.charset.StandardCharsets) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) Nullable(org.jetbrains.annotations.Nullable) Button(com.vaadin.flow.component.button.Button) Payload(io.rsocket.Payload) NAV(com.alibaba.rsocket.broker.web.ui.ServiceTestingView.NAV) Pre(com.vaadin.flow.component.html.Pre) ServiceLocator(com.alibaba.rsocket.ServiceLocator) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) MessageMimeTypeMetadata(com.alibaba.rsocket.metadata.MessageMimeTypeMetadata) ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) ByteBuf(io.netty.buffer.ByteBuf)

Example 10 with GSVRoutingMetadata

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)));
}
Also used : ServiceLocator(com.alibaba.rsocket.ServiceLocator) RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) RSocketCompositeMetadata(com.alibaba.rsocket.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(com.alibaba.rsocket.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) ByteBufPayload(io.rsocket.util.ByteBufPayload) ByteBuf(io.netty.buffer.ByteBuf) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

GSVRoutingMetadata (com.alibaba.rsocket.metadata.GSVRoutingMetadata)14 RSocketCompositeMetadata (com.alibaba.rsocket.metadata.RSocketCompositeMetadata)13 MessageMimeTypeMetadata (com.alibaba.rsocket.metadata.MessageMimeTypeMetadata)6 ByteBuf (io.netty.buffer.ByteBuf)5 AbstractRSocket (com.alibaba.rsocket.AbstractRSocket)4 BinaryRoutingMetadata (com.alibaba.rsocket.metadata.BinaryRoutingMetadata)4 RSocket (io.rsocket.RSocket)4 InvalidException (io.rsocket.exceptions.InvalidException)4 RSocketExchange (com.alibaba.rsocket.RSocketExchange)3 NotNull (org.jetbrains.annotations.NotNull)3 ServiceLocator (com.alibaba.rsocket.ServiceLocator)2 RSocketMimeType (com.alibaba.rsocket.metadata.RSocketMimeType)2 RSocketBrokerHandlerRegistry (com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerHandlerRegistry)2 RSocketBrokerResponderHandler (com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler)2 ServiceRoutingSelector (com.alibaba.spring.boot.rsocket.broker.route.ServiceRoutingSelector)2 Unpooled (io.netty.buffer.Unpooled)2 Payload (io.rsocket.Payload)2 ByteBufPayload (io.rsocket.util.ByteBufPayload)2 Nullable (org.jetbrains.annotations.Nullable)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2