Search in sources :

Example 1 with AppStatusEvent

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

the class RSocketBrokerManagerGossipImplTest method testGossipMessageWithCloudEvents.

@Test
public void testGossipMessageWithCloudEvents() throws Exception {
    AppStatusEvent appStatusEvent = new AppStatusEvent("1", 1);
    CloudEventImpl<AppStatusEvent> cloudEvent = RSocketCloudEventBuilder.builder(appStatusEvent).withSubject("app status update").build();
    Message message = Message.builder().correlationId("1").data(cloudEvent).build();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    messageCodec.serialize(message, bos);
    Message message1 = messageCodec.deserialize(new ByteArrayInputStream(bos.toByteArray()));
    CloudEventImpl<AppStatusEvent> cloudEvent2 = message1.data();
    Assertions.assertThat(cloudEvent2.getData()).isPresent();
}
Also used : Message(io.scalecube.cluster.transport.api.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AppStatusEvent(com.alibaba.rsocket.events.AppStatusEvent) Test(org.junit.jupiter.api.Test)

Example 2 with AppStatusEvent

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

AppStatusEvent (com.alibaba.rsocket.events.AppStatusEvent)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 PortsUpdateEvent (com.alibaba.rsocket.events.PortsUpdateEvent)1 ServicesExposedEvent (com.alibaba.rsocket.events.ServicesExposedEvent)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 Message (io.scalecube.cluster.transport.api.Message)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collectors (java.util.stream.Collectors)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.jupiter.api.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1