Search in sources :

Example 1 with RSocketEndpoint

use of org.kin.rsocket.broker.RSocketEndpoint in project kin-rsocket-broker by huangjianqin.

the class AppQueryController method query.

@GetMapping("/{appName}")
public Flux<AppVO> query(@PathVariable(name = "appName") String appName) {
    List<AppVO> apps = new ArrayList<>();
    Collection<RSocketEndpoint> rsocketEndpoints = serviceManager.getByAppName(appName);
    if (CollectionUtils.isEmpty(rsocketEndpoints)) {
        return Flux.empty();
    }
    for (RSocketEndpoint rsocketEndpoint : rsocketEndpoints) {
        AppMetadata appMetadata = rsocketEndpoint.getAppMetadata();
        apps.add(appMetadata.toVo());
    }
    return Flux.fromIterable(apps);
}
Also used : AppVO(org.kin.rsocket.core.domain.AppVO) ArrayList(java.util.ArrayList) AppMetadata(org.kin.rsocket.core.metadata.AppMetadata) RSocketEndpoint(org.kin.rsocket.broker.RSocketEndpoint) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with RSocketEndpoint

use of org.kin.rsocket.broker.RSocketEndpoint in project kin-rsocket-broker by huangjianqin.

the class RSocketApiController method handle.

@RequestMapping(value = "/{service}/{method}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Mono<ResponseEntity<String>> handle(@PathVariable("service") String service, @PathVariable("method") String method, @RequestParam(name = "group", required = false, defaultValue = "") String group, @RequestParam(name = "version", required = false, defaultValue = "") String version, @RequestBody(required = false) byte[] body, @RequestHeader(name = "X-Endpoint", required = false, defaultValue = "") String endpoint, @RequestHeader(name = HttpHeaders.AUTHORIZATION, required = false, defaultValue = "") String token) {
    try {
        GSVRoutingMetadata routingMetadata = GSVRoutingMetadata.of(group, service, method, version);
        int serviceId = routingMetadata.serviceId();
        ByteBuf bodyBuf = body == null ? EMPTY_BUFFER : Unpooled.wrappedBuffer(body);
        RSocketEndpoint rsocketEndpoint;
        if (endpoint.startsWith("id:")) {
            // 存在endpoint
            int instanceId = Integer.parseInt(endpoint.substring(3).trim());
            rsocketEndpoint = serviceManager.getByInstanceId(instanceId);
        } else {
            rsocketEndpoint = serviceManager.routeByServiceId(serviceId);
        }
        if (Objects.nonNull(rsocketEndpoint)) {
            if (rsocketBrokerProperties.isAuth()) {
                RSocketAppPrincipal principal = authenticationService.auth(token);
                if (principal == null || !serviceMeshInspector.isAllowed(principal, serviceId, rsocketEndpoint.getPrincipal())) {
                    return Mono.just(error(String.format("Service request not allowed '%s'", routingMetadata.gsv())));
                }
            }
            RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(routingMetadata, JSON_ENCODING_METADATA);
            return rsocketEndpoint.requestResponse(DefaultPayload.create(bodyBuf, compositeMetadata.getContent())).map(payload -> {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                headers.setCacheControl(CacheControl.noCache().getHeaderValue());
                return new ResponseEntity<>(payload.getDataUtf8(), headers, HttpStatus.OK);
            });
        }
        return Mono.just(error(String.format("service not found, '%s'", routingMetadata.gsv())));
    } catch (Exception e) {
        return Mono.just(error(e.getMessage()));
    }
}
Also used : RSocketCompositeMetadata(org.kin.rsocket.core.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) ByteBuf(io.netty.buffer.ByteBuf) RSocketEndpoint(org.kin.rsocket.broker.RSocketEndpoint) RSocketAppPrincipal(org.kin.rsocket.auth.RSocketAppPrincipal) RSocketEndpoint(org.kin.rsocket.broker.RSocketEndpoint)

Example 3 with RSocketEndpoint

use of org.kin.rsocket.broker.RSocketEndpoint in project kin-rsocket-broker by huangjianqin.

the class RSocketServiceQueryController method queryDefinition.

@GetMapping(value = "/definition/{service}")
public Mono<String> queryDefinition(@RequestParam(name = "group", defaultValue = "") String group, @PathVariable(name = "service") String service, @RequestParam(name = "version", defaultValue = "") String version) {
    ByteBuf bodyBuf = Unpooled.wrappedBuffer(("[\"".concat(service).concat("\"]")).getBytes(StandardCharsets.UTF_8));
    RSocketEndpoint RSocketEndpoint = serviceManager.routeByServiceId(ServiceLocator.of(group, service, version).getId());
    if (Objects.nonNull(RSocketEndpoint)) {
        GSVRoutingMetadata routingMetadata = GSVRoutingMetadata.of("", RSocketServiceInfoSupport.class.getName() + ".getReactiveServiceInfoByName", "");
        RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(routingMetadata, JSON_ENCODING_METADATA);
        return RSocketEndpoint.requestResponse(ByteBufPayload.create(bodyBuf, compositeMetadata.getContent())).map(Payload::getDataUtf8);
    }
    return Mono.error(new Exception(String.format("Service not found '%s'", service)));
}
Also used : RSocketCompositeMetadata(org.kin.rsocket.core.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) ByteBufPayload(io.rsocket.util.ByteBufPayload) ByteBuf(io.netty.buffer.ByteBuf) RSocketEndpoint(org.kin.rsocket.broker.RSocketEndpoint)

Example 4 with RSocketEndpoint

use of org.kin.rsocket.broker.RSocketEndpoint in project kin-rsocket-broker by huangjianqin.

the class RSocketServicesExposedEventConsumer method consume.

@Override
public Mono<Void> consume(CloudEventData<?> cloudEventData, RSocketServicesExposedEvent event) {
    if (event != null && event.getAppId().equals(cloudEventData.getAttributes().getSource().getHost())) {
        RSocketEndpoint rsocketEndpoint = serviceManager.getByUUID(event.getAppId());
        if (rsocketEndpoint != null) {
            Set<ServiceLocator> serviceLocators = event.getServices();
            rsocketEndpoint.registerServices(serviceLocators);
        }
    }
    return Mono.empty();
}
Also used : ServiceLocator(org.kin.rsocket.core.ServiceLocator) RSocketEndpoint(org.kin.rsocket.broker.RSocketEndpoint)

Example 5 with RSocketEndpoint

use of org.kin.rsocket.broker.RSocketEndpoint in project kin-rsocket-broker by huangjianqin.

the class PortsUpdateEventConsumer method consume.

@Override
public Mono<Void> consume(CloudEventData<?> cloudEventData, PortsUpdateEvent event) {
    if (event != null) {
        RSocketEndpoint rsocketEndpoint = serviceManager.getByUUID(event.getAppId());
        if (rsocketEndpoint != null) {
            AppMetadata appMetadata = rsocketEndpoint.getAppMetadata();
            appMetadata.updateWebPort(event.getWebPort());
            appMetadata.updateManagementPort(event.getManagementPort());
            appMetadata.updateRSocketPorts(event.getRsocketPorts());
        }
    }
    return Mono.empty();
}
Also used : AppMetadata(org.kin.rsocket.core.metadata.AppMetadata) RSocketEndpoint(org.kin.rsocket.broker.RSocketEndpoint)

Aggregations

RSocketEndpoint (org.kin.rsocket.broker.RSocketEndpoint)6 ByteBuf (io.netty.buffer.ByteBuf)2 ServiceLocator (org.kin.rsocket.core.ServiceLocator)2 AppMetadata (org.kin.rsocket.core.metadata.AppMetadata)2 GSVRoutingMetadata (org.kin.rsocket.core.metadata.GSVRoutingMetadata)2 RSocketCompositeMetadata (org.kin.rsocket.core.metadata.RSocketCompositeMetadata)2 Payload (io.rsocket.Payload)1 ByteBufPayload (io.rsocket.util.ByteBufPayload)1 ArrayList (java.util.ArrayList)1 RSocketAppPrincipal (org.kin.rsocket.auth.RSocketAppPrincipal)1 AppVO (org.kin.rsocket.core.domain.AppVO)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1