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