Search in sources :

Example 1 with ServicesExposedEvent

use of com.alibaba.rsocket.events.ServicesExposedEvent in project alibaba-rsocket-broker by alibaba.

the class LoadBalancedRSocket method onRSocketReconnected.

public void onRSocketReconnected(String rsocketUri, RSocket rsocket) {
    this.activeSockets.put(rsocketUri, rsocket);
    this.unHealthyUriSet.remove(rsocketUri);
    this.randomSelector = new RandomSelector<>(this.serviceId, new ArrayList<>(activeSockets.values()));
    rsocket.onClose().subscribe(aVoid -> onRSocketClosed(rsocketUri, rsocket, null));
    CloudEventImpl<ServicesExposedEvent> cloudEvent = requesterSupport.servicesExposedEvent().get();
    if (cloudEvent != null) {
        try {
            Payload payload = cloudEventToMetadataPushPayload(cloudEvent);
            rsocket.metadataPush(payload).subscribe();
        } catch (Exception ignore) {
        }
    }
}
Also used : ByteBufPayload(io.rsocket.util.ByteBufPayload) Payload(io.rsocket.Payload) ServicesExposedEvent(com.alibaba.rsocket.events.ServicesExposedEvent) ConnectException(java.net.ConnectException) ConnectionErrorException(io.rsocket.exceptions.ConnectionErrorException) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 2 with ServicesExposedEvent

use of com.alibaba.rsocket.events.ServicesExposedEvent 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)

Aggregations

ServicesExposedEvent (com.alibaba.rsocket.events.ServicesExposedEvent)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 LoadBalancedRSocket (com.alibaba.rsocket.loadbalance.LoadBalancedRSocket)1 RsocketErrorCode (com.alibaba.rsocket.observability.RsocketErrorCode)1 UpstreamCluster (com.alibaba.rsocket.upstream.UpstreamCluster)1 UpstreamManager (com.alibaba.rsocket.upstream.UpstreamManager)1 Payload (io.rsocket.Payload)1 ConnectionErrorException (io.rsocket.exceptions.ConnectionErrorException)1 ByteBufPayload (io.rsocket.util.ByteBufPayload)1 ConnectException (java.net.ConnectException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Collectors (java.util.stream.Collectors)1 NotNull (org.jetbrains.annotations.NotNull)1 Logger (org.slf4j.Logger)1