Search in sources :

Example 1 with RSocketBrokerResponderHandler

use of com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler in project alibaba-rsocket-broker by alibaba.

the class DiscoveryServiceImpl method findServiceInstances.

@NotNull
private Flux<RSocketServiceInstance> findServiceInstances(String serviceId) {
    Integer serviceHashCode = ServiceLocator.serviceHashCode(serviceId);
    Collection<Integer> instanceIdList = routingSelector.findHandlers(serviceHashCode);
    if (instanceIdList.isEmpty()) {
        return findServiceInstancesByAppName(serviceId);
    }
    List<RSocketServiceInstance> serviceInstances = new ArrayList<>();
    for (Integer handlerId : instanceIdList) {
        RSocketBrokerResponderHandler handler = handlerRegistry.findById(handlerId);
        if (handler != null) {
            serviceInstances.add(constructServiceInstance(handler));
        }
    }
    return Flux.fromIterable(serviceInstances);
}
Also used : RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) RSocketServiceInstance(com.alibaba.rsocket.discovery.RSocketServiceInstance) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with RSocketBrokerResponderHandler

use of com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler in project alibaba-rsocket-broker by alibaba.

the class UpstreamForwardRSocket method findDestination.

private Mono<RSocket> findDestination(GSVRoutingMetadata routingMetaData) {
    return Mono.create(sink -> {
        String gsv = routingMetaData.gsv();
        Integer serviceId = routingMetaData.id();
        RSocketBrokerResponderHandler targetHandler = null;
        RSocket rsocket = null;
        Exception error = null;
        String endpoint = routingMetaData.getEndpoint();
        if (endpoint != null && !endpoint.isEmpty()) {
            targetHandler = findDestinationWithEndpoint(endpoint, serviceId);
            if (targetHandler == null) {
                error = new InvalidException(RsocketErrorCode.message("RST-900405", gsv, endpoint));
            }
        } else {
            Integer targetHandlerId = routingSelector.findHandler(serviceId);
            if (targetHandlerId != null) {
                targetHandler = handlerRegistry.findById(targetHandlerId);
            } else {
                error = new InvalidException(RsocketErrorCode.message("RST-900404", gsv));
            }
        }
        // security check
        if (targetHandler != null) {
            rsocket = targetHandler.getPeerRsocket();
        /* if (serviceMeshInspector.isRequestAllowed(this.principal, gsv, targetHandler.principal)) {
                        rsocket = targetHandler.getPeerRsocket();
                    } else {
                        error = new ApplicationErrorException(RsocketErrorCode.message("RST-900401", gsv));
                    }*/
        }
        if (rsocket != null) {
            sink.success(rsocket);
        } else if (error != null) {
            sink.error(error);
        } else {
            sink.error(new ApplicationErrorException(RsocketErrorCode.message("RST-900404", gsv)));
        }
    });
}
Also used : RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) RSocket(io.rsocket.RSocket) AbstractRSocket(com.alibaba.rsocket.AbstractRSocket) InvalidException(io.rsocket.exceptions.InvalidException) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 3 with RSocketBrokerResponderHandler

use of com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler in project alibaba-rsocket-broker by alibaba.

the class AppQueryController method query.

@GetMapping("/{appName}")
public Flux<Map<String, Object>> query(@PathVariable(name = "appName") String appName) {
    List<Map<String, Object>> apps = new ArrayList<>();
    Collection<RSocketBrokerResponderHandler> handlers = handlerRegistry.findByAppName(appName);
    if (handlers != null) {
        for (RSocketBrokerResponderHandler handler : handlers) {
            Map<String, Object> app = new HashMap<>();
            AppMetadata appMetadata = handler.getAppMetadata();
            app.put("ip", appMetadata.getIp());
            app.put("uuid", appMetadata.getUuid());
            app.put("startedAt", appMetadata.getConnectedAt());
            apps.add(app);
        }
    }
    return Flux.fromIterable(apps);
}
Also used : RSocketBrokerResponderHandler(com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler) AppMetadata(com.alibaba.rsocket.metadata.AppMetadata) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with RSocketBrokerResponderHandler

use of com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler 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 5 with RSocketBrokerResponderHandler

use of com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler 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

RSocketBrokerResponderHandler (com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerResponderHandler)5 ServiceLocator (com.alibaba.rsocket.ServiceLocator)2 GSVRoutingMetadata (com.alibaba.rsocket.metadata.GSVRoutingMetadata)2 RSocketCompositeMetadata (com.alibaba.rsocket.metadata.RSocketCompositeMetadata)2 ByteBuf (io.netty.buffer.ByteBuf)2 Payload (io.rsocket.Payload)2 ByteBufPayload (io.rsocket.util.ByteBufPayload)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 AbstractRSocket (com.alibaba.rsocket.AbstractRSocket)1 NAV (com.alibaba.rsocket.broker.web.ui.ServiceTestingView.NAV)1 RSocketServiceInstance (com.alibaba.rsocket.discovery.RSocketServiceInstance)1 AppMetadata (com.alibaba.rsocket.metadata.AppMetadata)1 MessageMimeTypeMetadata (com.alibaba.rsocket.metadata.MessageMimeTypeMetadata)1 RSocketMimeType (com.alibaba.rsocket.metadata.RSocketMimeType)1 RSocketBrokerHandlerRegistry (com.alibaba.spring.boot.rsocket.broker.responder.RSocketBrokerHandlerRegistry)1 ServiceRoutingSelector (com.alibaba.spring.boot.rsocket.broker.route.ServiceRoutingSelector)1 Button (com.vaadin.flow.component.button.Button)1 H3 (com.vaadin.flow.component.html.H3)1 H4 (com.vaadin.flow.component.html.H4)1 Pre (com.vaadin.flow.component.html.Pre)1