Search in sources :

Example 1 with UpstreamCluster

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;
}
Also used : RoutingEndpoint(com.alibaba.rsocket.route.RoutingEndpoint) SmartLifecycleUpstreamManagerImpl(com.alibaba.spring.boot.rsocket.upstream.SmartLifecycleUpstreamManagerImpl) JwtTokenNotFoundException(com.alibaba.spring.boot.rsocket.upstream.JwtTokenNotFoundException) UpstreamCluster(com.alibaba.rsocket.upstream.UpstreamCluster) Bean(org.springframework.context.annotation.Bean)

Example 2 with UpstreamCluster

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;
}
Also used : RSocketServiceHealth(com.alibaba.rsocket.health.RSocketServiceHealth) LoadBalancedRSocket(com.alibaba.rsocket.loadbalance.LoadBalancedRSocket) UpstreamCluster(com.alibaba.rsocket.upstream.UpstreamCluster) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation)

Example 3 with UpstreamCluster

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();
    }
}
Also used : Logger(org.slf4j.Logger) PortsUpdateEvent(com.alibaba.rsocket.events.PortsUpdateEvent) UpstreamCluster(com.alibaba.rsocket.upstream.UpstreamCluster) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) RSocketAppContext(com.alibaba.rsocket.RSocketAppContext) RSocketRequesterSupport(com.alibaba.rsocket.RSocketRequesterSupport) RsocketErrorCode(com.alibaba.rsocket.observability.RsocketErrorCode) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationListener(org.springframework.context.ApplicationListener) Collectors(java.util.stream.Collectors) LoadBalancedRSocket(com.alibaba.rsocket.loadbalance.LoadBalancedRSocket) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ServicesExposedEvent(com.alibaba.rsocket.events.ServicesExposedEvent) CloudEventImpl(com.alibaba.rsocket.cloudevents.CloudEventImpl) UpstreamManager(com.alibaba.rsocket.upstream.UpstreamManager) RSocketCloudEventBuilder(com.alibaba.rsocket.cloudevents.RSocketCloudEventBuilder) NotNull(org.jetbrains.annotations.NotNull) ServiceLocator(com.alibaba.rsocket.ServiceLocator) AppStatusEvent(com.alibaba.rsocket.events.AppStatusEvent) PortsUpdateEvent(com.alibaba.rsocket.events.PortsUpdateEvent) UpstreamCluster(com.alibaba.rsocket.upstream.UpstreamCluster) ServiceLocator(com.alibaba.rsocket.ServiceLocator) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) LoadBalancedRSocket(com.alibaba.rsocket.loadbalance.LoadBalancedRSocket) ServicesExposedEvent(com.alibaba.rsocket.events.ServicesExposedEvent) AppStatusEvent(com.alibaba.rsocket.events.AppStatusEvent)

Example 4 with UpstreamCluster

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();
    }
}
Also used : UpstreamManagerImpl(com.alibaba.rsocket.upstream.UpstreamManagerImpl) UpstreamCluster(com.alibaba.rsocket.upstream.UpstreamCluster) UpstreamClusterChangedEventConsumer(com.alibaba.rsocket.upstream.UpstreamClusterChangedEventConsumer)

Example 5 with UpstreamCluster

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;
}
Also used : UpstreamCluster(com.alibaba.rsocket.upstream.UpstreamCluster)

Aggregations

UpstreamCluster (com.alibaba.rsocket.upstream.UpstreamCluster)5 LoadBalancedRSocket (com.alibaba.rsocket.loadbalance.LoadBalancedRSocket)2 RSocketAppContext (com.alibaba.rsocket.RSocketAppContext)1 RSocketRequesterSupport (com.alibaba.rsocket.RSocketRequesterSupport)1 ServiceLocator (com.alibaba.rsocket.ServiceLocator)1 CloudEventImpl (com.alibaba.rsocket.cloudevents.CloudEventImpl)1 RSocketCloudEventBuilder (com.alibaba.rsocket.cloudevents.RSocketCloudEventBuilder)1 AppStatusEvent (com.alibaba.rsocket.events.AppStatusEvent)1 PortsUpdateEvent (com.alibaba.rsocket.events.PortsUpdateEvent)1 ServicesExposedEvent (com.alibaba.rsocket.events.ServicesExposedEvent)1 RSocketServiceHealth (com.alibaba.rsocket.health.RSocketServiceHealth)1 RsocketErrorCode (com.alibaba.rsocket.observability.RsocketErrorCode)1 RoutingEndpoint (com.alibaba.rsocket.route.RoutingEndpoint)1 UpstreamClusterChangedEventConsumer (com.alibaba.rsocket.upstream.UpstreamClusterChangedEventConsumer)1 UpstreamManager (com.alibaba.rsocket.upstream.UpstreamManager)1 UpstreamManagerImpl (com.alibaba.rsocket.upstream.UpstreamManagerImpl)1 JwtTokenNotFoundException (com.alibaba.spring.boot.rsocket.upstream.JwtTokenNotFoundException)1 SmartLifecycleUpstreamManagerImpl (com.alibaba.spring.boot.rsocket.upstream.SmartLifecycleUpstreamManagerImpl)1 Collectors (java.util.stream.Collectors)1 NotNull (org.jetbrains.annotations.NotNull)1