Search in sources :

Example 6 with ServiceLocator

use of com.alibaba.rsocket.ServiceLocator in project alibaba-rsocket-broker by alibaba.

the class RSocketRequesterSupportImpl method exposedServices.

@Override
public Supplier<Set<ServiceLocator>> exposedServices() {
    return () -> {
        return applicationContext.getBeansWithAnnotation(RSocketService.class).values().stream().filter(bean -> !(bean instanceof RSocketServiceHealth || bean instanceof MetricsService)).map(o -> {
            Class<?> managedBeanClass = AopUtils.isAopProxy(o) ? AopUtils.getTargetClass(o) : o.getClass();
            RSocketService rsocketService = AnnotationUtils.findAnnotation(managedBeanClass, RSocketService.class);
            // noinspection ConstantConditions
            String serviceName = rsocketService.serviceInterface().getCanonicalName();
            if (!rsocketService.name().isEmpty()) {
                serviceName = rsocketService.name();
            }
            String group = properties.getGroup();
            if (group == null || group.isEmpty()) {
                group = rsocketService.group();
            }
            String version = properties.getVersion();
            if (version == null || version.isEmpty()) {
                version = rsocketService.version();
            }
            return new ServiceLocator(group, serviceName, version, rsocketService.tags());
        }).collect(Collectors.toSet());
    };
}
Also used : java.util(java.util) RSocketServiceHealth(com.alibaba.rsocket.health.RSocketServiceHealth) AopUtils(org.springframework.aop.support.AopUtils) URL(java.net.URL) RSocketAppContext(com.alibaba.rsocket.RSocketAppContext) RSocketRequesterSupport(com.alibaba.rsocket.RSocketRequesterSupport) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) RSocketRemoteServiceBuilder(com.alibaba.rsocket.invocation.RSocketRemoteServiceBuilder) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) ServicesExposedEvent(com.alibaba.rsocket.events.ServicesExposedEvent) CloudEventImpl(com.alibaba.rsocket.cloudevents.CloudEventImpl) URI(java.net.URI) ByteBufPayload(io.rsocket.util.ByteBufPayload) MetricsService(com.alibaba.rsocket.observability.MetricsService) RSocketService(com.alibaba.rsocket.RSocketService) ResourceLoader(org.springframework.core.io.ResourceLoader) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) Reader(java.io.Reader) SocketAcceptor(io.rsocket.SocketAcceptor) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) RSocketInterceptor(io.rsocket.plugins.RSocketInterceptor) Payload(io.rsocket.Payload) NetworkUtil(com.alibaba.rsocket.transport.NetworkUtil) com.alibaba.rsocket.metadata(com.alibaba.rsocket.metadata) NotNull(org.jetbrains.annotations.NotNull) ServiceLocator(com.alibaba.rsocket.ServiceLocator) ApplicationContextAware(org.springframework.context.ApplicationContextAware) FileCopyUtils(org.springframework.util.FileCopyUtils) InputStream(java.io.InputStream) ServiceLocator(com.alibaba.rsocket.ServiceLocator) RSocketService(com.alibaba.rsocket.RSocketService) MetricsService(com.alibaba.rsocket.observability.MetricsService) RSocketServiceHealth(com.alibaba.rsocket.health.RSocketServiceHealth)

Example 7 with ServiceLocator

use of com.alibaba.rsocket.ServiceLocator 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 8 with ServiceLocator

use of com.alibaba.rsocket.ServiceLocator in project alibaba-rsocket-broker by alibaba.

the class ServicesHiddenEvent method convertServicesToCloudEvent.

public static CloudEventImpl<ServicesHiddenEvent> convertServicesToCloudEvent(Collection<ServiceLocator> serviceLocators) {
    ServicesHiddenEvent servicesHiddenEvent = new ServicesHiddenEvent();
    for (ServiceLocator serviceLocator : serviceLocators) {
        servicesHiddenEvent.addService(serviceLocator);
    }
    servicesHiddenEvent.setAppId(RSocketAppContext.ID);
    return RSocketCloudEventBuilder.builder(servicesHiddenEvent).build();
}
Also used : ServiceLocator(com.alibaba.rsocket.ServiceLocator)

Example 9 with ServiceLocator

use of com.alibaba.rsocket.ServiceLocator in project alibaba-rsocket-broker by alibaba.

the class RSocketRemoteServiceBuilder method buildByteBuddyProxy.

public T buildByteBuddyProxy() {
    CONSUMED_SERVICES.add(new ServiceLocator(group, service, version));
    RSocketRequesterRpcProxy proxy = getRequesterProxy();
    return ByteBuddyUtils.build(this.serviceInterface, proxy);
}
Also used : ServiceLocator(com.alibaba.rsocket.ServiceLocator)

Example 10 with ServiceLocator

use of com.alibaba.rsocket.ServiceLocator in project alibaba-rsocket-broker by alibaba.

the class ServiceRoutingSelectorImplTest method testOperation.

@Test
public void testOperation() {
    Integer instanceId = 1;
    Set<ServiceLocator> services = new HashSet<>();
    services.add(new ServiceLocator("", "1", ""));
    services.add(new ServiceLocator("", "2", ""));
    services.add(new ServiceLocator("", "3", ""));
    routingSelector.register(instanceId, services);
    Assertions.assertTrue(routingSelector.containInstance(instanceId));
    Assertions.assertTrue(routingSelector.containService(MurmurHash3.hash32("1")));
    Assertions.assertNotNull(routingSelector.findHandler(MurmurHash3.hash32("1")));
    Assertions.assertNull(routingSelector.findHandler(MurmurHash3.hash32("4")));
    Assertions.assertEquals(1, routingSelector.getInstanceCount(MurmurHash3.hash32("1")));
    routingSelector.deregister(instanceId);
    Assertions.assertNull(routingSelector.findHandler(MurmurHash3.hash32("1")));
}
Also used : ServiceLocator(com.alibaba.rsocket.ServiceLocator) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceLocator (com.alibaba.rsocket.ServiceLocator)13 CloudEventImpl (com.alibaba.rsocket.cloudevents.CloudEventImpl)3 ServicesExposedEvent (com.alibaba.rsocket.events.ServicesExposedEvent)3 Collectors (java.util.stream.Collectors)3 RSocketAppContext (com.alibaba.rsocket.RSocketAppContext)2 RSocketRequesterSupport (com.alibaba.rsocket.RSocketRequesterSupport)2 RSocketServiceHealth (com.alibaba.rsocket.health.RSocketServiceHealth)2 RSocketRemoteServiceBuilder (com.alibaba.rsocket.invocation.RSocketRemoteServiceBuilder)2 RsocketErrorCode (com.alibaba.rsocket.observability.RsocketErrorCode)2 UpstreamCluster (com.alibaba.rsocket.upstream.UpstreamCluster)2 UpstreamManager (com.alibaba.rsocket.upstream.UpstreamManager)2 Payload (io.rsocket.Payload)2 ByteBufPayload (io.rsocket.util.ByteBufPayload)2 NotNull (org.jetbrains.annotations.NotNull)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 RSocketService (com.alibaba.rsocket.RSocketService)1 RSocketCloudEventBuilder (com.alibaba.rsocket.cloudevents.RSocketCloudEventBuilder)1 AppStatusEvent (com.alibaba.rsocket.events.AppStatusEvent)1 CloudEventsProcessor (com.alibaba.rsocket.events.CloudEventsProcessor)1