use of com.alibaba.rsocket.upstream.UpstreamCluster in project alibaba-rsocket-broker by alibaba.
the class RSocketAutoConfiguration method rsocketUpstreamManager.
@Bean(initMethod = "init")
public UpstreamManager rsocketUpstreamManager(@Autowired RSocketRequesterSupport rsocketRequesterSupport) throws JwtTokenNotFoundException {
SmartLifecycleUpstreamManagerImpl upstreamManager = new SmartLifecycleUpstreamManagerImpl(rsocketRequesterSupport);
if (properties.getBrokers() != null && !properties.getBrokers().isEmpty()) {
if (properties.getJwtToken() == null || properties.getJwtToken().isEmpty()) {
throw new JwtTokenNotFoundException();
}
UpstreamCluster cluster = new UpstreamCluster(null, "*", null);
cluster.setUris(properties.getBrokers());
upstreamManager.add(cluster);
}
upstreamManager.setP2pServices(properties.getP2pServices());
if (properties.getRoutes() != null && !properties.getRoutes().isEmpty()) {
for (RoutingEndpoint route : properties.getRoutes()) {
UpstreamCluster cluster = new UpstreamCluster(route.getGroup(), route.getService(), route.getVersion());
cluster.setUris(route.getUris());
upstreamManager.add(cluster);
}
}
return upstreamManager;
}
use of com.alibaba.rsocket.upstream.UpstreamCluster in project alibaba-rsocket-broker by alibaba.
the class RSocketEndpoint method info.
@ReadOperation
public Map<String, Object> info() {
Map<String, Object> info = new HashMap<>();
info.put("id", RSocketAppContext.ID);
info.put("serviceStatus", AppStatusEvent.statusText(this.rsocketServiceStatus));
if (this.serviceProvider) {
info.put("published", rsocketRequesterSupport.exposedServices().get());
}
if (!RSocketRemoteServiceBuilder.CONSUMED_SERVICES.isEmpty()) {
info.put("subscribed", rsocketRequesterSupport.subscribedServices().get().stream().filter(serviceLocator -> !RSocketServiceHealth.class.getCanonicalName().equals(serviceLocator.getService())).collect(Collectors.toList()));
}
Collection<UpstreamCluster> upstreamClusters = upstreamManager.findAllClusters();
if (!upstreamClusters.isEmpty()) {
info.put("upstreams", upstreamClusters.stream().map(upstreamCluster -> {
Map<String, Object> temp = new HashMap<>();
temp.put("service", upstreamCluster.getServiceId());
temp.put("uris", upstreamCluster.getUris());
LoadBalancedRSocket loadBalancedRSocket = upstreamCluster.getLoadBalancedRSocket();
temp.put("activeUris", loadBalancedRSocket.getActiveSockets().keySet());
if (!loadBalancedRSocket.getUnHealthyUriSet().isEmpty()) {
temp.put("unHealthyUris", loadBalancedRSocket.getUnHealthyUriSet());
}
temp.put("lastRefreshTimeStamp", new Date(loadBalancedRSocket.getLastRefreshTimeStamp()));
temp.put("lastHealthCheckTimeStamp", new Date(loadBalancedRSocket.getLastHealthCheckTimeStamp()));
return temp;
}).collect(Collectors.toList()));
}
UpstreamCluster brokerCluster = upstreamManager.findClusterByServiceId("*");
if (brokerCluster != null) {
info.put("brokers", brokerCluster.getUris());
}
if (properties.getMetadata() != null && !properties.getMetadata().isEmpty()) {
info.put("metadata", properties.getMetadata());
}
if (!offlineServices.isEmpty()) {
info.put("offlineServices", offlineServices);
}
return info;
}
use of com.alibaba.rsocket.upstream.UpstreamCluster in project alibaba-rsocket-broker by alibaba.
the class RSocketServicesPublishHook method onApplicationEvent.
@Override
public void onApplicationEvent(@NotNull ApplicationReadyEvent applicationReadyEvent) {
UpstreamCluster brokerCluster = upstreamManager.findBroker();
if (brokerCluster == null)
return;
// rsocket broker cluster logic
CloudEventImpl<AppStatusEvent> appStatusEventCloudEvent = RSocketCloudEventBuilder.builder(new AppStatusEvent(RSocketAppContext.ID, AppStatusEvent.STATUS_SERVING)).build();
LoadBalancedRSocket loadBalancedRSocket = brokerCluster.getLoadBalancedRSocket();
// ports update
ConfigurableEnvironment env = applicationReadyEvent.getApplicationContext().getEnvironment();
int serverPort = Integer.parseInt(env.getProperty("server.port", "0"));
if (serverPort == 0) {
if (RSocketAppContext.webPort > 0 || RSocketAppContext.managementPort > 0 || RSocketAppContext.rsocketPorts != null) {
PortsUpdateEvent portsUpdateEvent = new PortsUpdateEvent();
portsUpdateEvent.setAppId(RSocketAppContext.ID);
portsUpdateEvent.setWebPort(RSocketAppContext.webPort);
portsUpdateEvent.setManagementPort(RSocketAppContext.managementPort);
portsUpdateEvent.setRsocketPorts(RSocketAppContext.rsocketPorts);
CloudEventImpl<PortsUpdateEvent> portsUpdateCloudEvent = RSocketCloudEventBuilder.builder(portsUpdateEvent).build();
loadBalancedRSocket.fireCloudEventToUpstreamAll(portsUpdateCloudEvent).doOnSuccess(aVoid -> log.info(RsocketErrorCode.message("RST-301200", loadBalancedRSocket.getActiveUris()))).subscribe();
}
}
// app status
loadBalancedRSocket.fireCloudEventToUpstreamAll(appStatusEventCloudEvent).doOnSuccess(aVoid -> log.info(RsocketErrorCode.message("RST-301200", loadBalancedRSocket.getActiveUris()))).subscribe();
// service exposed
CloudEventImpl<ServicesExposedEvent> servicesExposedEventCloudEvent = rsocketRequesterSupport.servicesExposedEvent().get();
if (servicesExposedEventCloudEvent != null) {
loadBalancedRSocket.fireCloudEventToUpstreamAll(servicesExposedEventCloudEvent).doOnSuccess(aVoid -> {
String exposedServices = rsocketRequesterSupport.exposedServices().get().stream().map(ServiceLocator::getGsv).collect(Collectors.joining(","));
log.info(RsocketErrorCode.message("RST-301201", exposedServices, loadBalancedRSocket.getActiveUris()));
}).subscribe();
}
}
use of com.alibaba.rsocket.upstream.UpstreamCluster in project alibaba-rsocket-broker by alibaba.
the class RSocketBrokerClient method initUpstreamManager.
private void initUpstreamManager() {
this.upstreamManager = new UpstreamManagerImpl(rsocketRequesterSupport);
upstreamManager.add(new UpstreamCluster(null, "*", null, this.brokers));
try {
upstreamManager.init();
this.cloudEventsProcessor.addConsumer(new UpstreamClusterChangedEventConsumer(upstreamManager));
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.alibaba.rsocket.upstream.UpstreamCluster in project alibaba-rsocket-broker by alibaba.
the class GrpcServiceRSocketImplBuilder method upstreamManager.
public GrpcServiceRSocketImplBuilder<T> upstreamManager(UpstreamManager upstreamManager) {
String serviceId = ServiceLocator.serviceId(interceptor.getGroup(), interceptor.getService(), interceptor.getVersion());
UpstreamCluster upstream = upstreamManager.findClusterByServiceId(serviceId);
if (upstream == null) {
upstream = upstreamManager.findBroker();
}
interceptor.setRsocket(upstream.getLoadBalancedRSocket());
return this;
}
Aggregations