use of io.scalecube.net.Address in project scalecube by scalecube.
the class ServiceRegistryTest method test_listen_to_discovery_events.
@ParameterizedTest
@MethodSource("metadataCodecSource")
public void test_listen_to_discovery_events(MetadataCodec metadataCodec) {
Sinks.Many<ServiceDiscoveryEvent> processor = Sinks.many().replay().all();
List<Microservices> cluster = new CopyOnWriteArrayList<>();
Microservices seed = Microservices.builder().discovery(defServiceDiscovery(metadataCodec)).transport(RSocketServiceTransport::new).services(new AnnotationServiceImpl()).startAwait();
cluster.add(seed);
seed.listenDiscovery().subscribe(processor::tryEmitNext, processor::tryEmitError, processor::tryEmitComplete);
Address seedAddress = seed.discovery().address();
StepVerifier.create(processor.asFlux().onBackpressureBuffer()).then(() -> {
Microservices ms1 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
cluster.add(ms1);
}).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> {
Microservices ms2 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
cluster.add(ms2);
}).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> cluster.remove(2).shutdown().block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).then(() -> cluster.remove(1).shutdown().block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).thenCancel().verify(TIMEOUT);
StepVerifier.create(seed.call().api(AnnotationService.class).serviceDiscoveryEventTypes()).assertNext(type -> assertEquals(ENDPOINT_ADDED, type)).assertNext(type -> assertEquals(ENDPOINT_ADDED, type)).assertNext(type -> assertEquals(ENDPOINT_LEAVING, type)).assertNext(type -> assertEquals(ENDPOINT_REMOVED, type)).assertNext(type -> assertEquals(ENDPOINT_LEAVING, type)).assertNext(type -> assertEquals(ENDPOINT_REMOVED, type)).thenCancel().verify(TIMEOUT);
Mono.whenDelayError(cluster.stream().map(Microservices::shutdown).toArray(Mono[]::new)).block(TIMEOUT);
}
use of io.scalecube.net.Address in project scalecube by scalecube.
the class RoutersTest method setup.
@BeforeAll
public static void setup() {
gateway = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).startAwait();
gatewayAddress = gateway.discovery().address();
// Create microservices instance cluster.
provider1 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(gatewayAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new GreetingServiceImpl(1)).tag("ONLYFOR", "joe").tag("SENDER", "1").build(), ServiceInfo.fromServiceInstance(new GreetingServiceImplA()).tag("Weight", "0.1").build()).startAwait();
// Create microservices instance cluster.
provider2 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(gatewayAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new GreetingServiceImpl(2)).tag("ONLYFOR", "fransin").tag("SENDER", "2").build(), ServiceInfo.fromServiceInstance(new GreetingServiceImplB()).tag("Weight", "0.9").build()).startAwait();
TagService tagService = input -> input.map(String::toUpperCase);
provider3 = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(gatewayAddress))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(tagService).tag("tagB", "bb").tag("tagC", "c").build()).startAwait();
}
use of io.scalecube.net.Address in project scalecube by scalecube.
the class Microservices method newServiceEndpoint.
private ServiceEndpoint newServiceEndpoint(ServiceEndpoint serviceEndpoint) {
ServiceEndpoint.Builder builder = ServiceEndpoint.from(serviceEndpoint);
int port = Optional.ofNullable(externalPort).orElse(serviceEndpoint.address().port());
// calculate local service endpoint address
Address newAddress = Optional.ofNullable(externalHost).map(host -> Address.create(host, port)).orElseGet(() -> Address.create(serviceEndpoint.address().host(), port));
return builder.address(newAddress).build();
}
use of io.scalecube.net.Address in project scalecube by scalecube.
the class Microservices method start.
private Mono<Microservices> start() {
LOGGER.info("[{}][start] Starting", id);
// Create bootstrap scheduler
Scheduler scheduler = Schedulers.newSingle(toString(), true);
return transportBootstrap.start(this).publishOn(scheduler).flatMap(transportBootstrap -> {
final ServiceCall call = call();
final Address serviceAddress = transportBootstrap.transportAddress;
final ServiceEndpoint.Builder serviceEndpointBuilder = ServiceEndpoint.builder().id(id).address(serviceAddress).contentTypes(DataCodec.getAllContentTypes()).tags(tags);
// invoke service providers and register services
List<Object> serviceInstances = serviceProviders.stream().flatMap(serviceProvider -> serviceProvider.provide(call).stream()).peek(this::registerInMethodRegistry).peek(serviceInfo -> serviceEndpointBuilder.appendServiceRegistrations(ServiceScanner.scanServiceInfo(serviceInfo))).map(ServiceInfo::serviceInstance).collect(Collectors.toList());
if (transportBootstrap == ServiceTransportBootstrap.NULL_INSTANCE && !serviceInstances.isEmpty()) {
LOGGER.warn("[{}] ServiceTransport is not set", this.id());
}
serviceEndpoint = newServiceEndpoint(serviceEndpointBuilder.build());
return createDiscovery(this, new ServiceDiscoveryOptions().serviceEndpoint(serviceEndpoint)).publishOn(scheduler).then(startGateway(new GatewayOptions().call(call))).publishOn(scheduler).then(Mono.fromCallable(() -> Injector.inject(this, serviceInstances))).then(Mono.fromCallable(() -> JmxMonitorMBean.start(this))).then(compositeDiscovery.startListen()).publishOn(scheduler).thenReturn(this);
}).onErrorResume(ex -> Mono.defer(this::shutdown).then(Mono.error(ex)).cast(Microservices.class)).doOnSuccess(m -> LOGGER.info("[{}][start] Started", id)).doOnTerminate(scheduler::dispose);
}
use of io.scalecube.net.Address in project scalecube by scalecube.
the class RSocketClientTransport method create.
@Override
public ClientChannel create(ServiceReference serviceReference) {
// keep reference for threadsafety
final Map<Address, Mono<RSocket>> monoMap = rsockets.get();
final Address address = serviceReference.address();
Mono<RSocket> mono = monoMap.computeIfAbsent(address, key -> getCredentials(serviceReference).flatMap(creds -> connect(key, creds, monoMap)).cache().doOnError(ex -> monoMap.remove(key)));
return new RSocketClientChannel(mono, new ServiceMessageCodec(headersCodec, dataCodecs));
}
Aggregations