use of io.scalecube.services.Microservices in project scalecube by scalecube.
the class TestStreamingService method test_just_one_message.
@Test
public void test_just_one_message() throws InterruptedException {
int batchSize = 1;
Microservices gateway = Microservices.builder().build();
Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
ServiceCall service = gateway.dispatcher().create();
final CountDownLatch latch1 = new CountDownLatch(batchSize);
AtomicReference<Subscription> sub1 = new AtomicReference<Subscription>(null);
Message justOne = Messages.builder().request(QuoteService.NAME, "justOne").build();
sub1.set(service.listen(justOne).serialize().subscribe(onNext -> {
sub1.get().unsubscribe();
latch1.countDown();
}));
latch1.await(2, TimeUnit.SECONDS);
assertTrue(latch1.getCount() == 0);
assertTrue(sub1.get().isUnsubscribed());
gateway.shutdown();
node.shutdown();
}
use of io.scalecube.services.Microservices in project scalecube by scalecube.
the class TestStreamingService method test_remote_node_died.
@Test
public void test_remote_node_died() throws InterruptedException {
int batchSize = 1;
Microservices gateway = Microservices.builder().build();
Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
ServiceCall service = gateway.dispatcher().create();
final CountDownLatch latch1 = new CountDownLatch(batchSize);
AtomicReference<Subscription> sub1 = new AtomicReference<Subscription>(null);
Message justOne = Messages.builder().request(QuoteService.NAME, "justOne").build();
sub1.set(service.listen(justOne).subscribe(onNext -> {
System.out.println(onNext);
}));
gateway.cluster().listenMembership().filter(predicate -> predicate.isRemoved()).subscribe(onNext -> {
latch1.countDown();
});
node.cluster().shutdown();
latch1.await(20, TimeUnit.SECONDS);
Thread.sleep(100);
assertTrue(latch1.getCount() == 0);
assertTrue(sub1.get().isUnsubscribed());
gateway.shutdown();
}
use of io.scalecube.services.Microservices in project scalecube by scalecube.
the class ServiceTagsExample method main.
public static void main(String[] args) {
Microservices gateway = Microservices.builder().build();
Microservices services1 = Microservices.builder().seeds(gateway.cluster().address()).services().service(new GreetingServiceImplA()).tag("Weight", "0.3").add().build().build();
Microservices services2 = Microservices.builder().seeds(gateway.cluster().address()).services().service(new GreetingServiceImplB()).tag("Weight", "0.7").add().build().build();
CanaryService service = gateway.proxy().router(CanaryTestingRouter.class).api(CanaryService.class).create();
for (int i = 0; i < 10; i++) {
service.greeting("joe").whenComplete((success, error) -> {
success.startsWith("B");
System.out.println(success);
});
}
}
use of io.scalecube.services.Microservices in project scalecube by scalecube.
the class StockMain method main.
/**
* Example that shows the basic usage of streaming quotes from a remove microservice. the example creates 2 cluster
* nodes that communicates over the network: 1. gateway node cluster member that acts as seed and gateway. 2. service
* nodes that provisions the SimpleQuoteService at the cluster.
*
* <pre>
*
* {@code
* Request:
* QuoteService -> gateway (--- network --->) node1 -> SimpleQuoteService
*
* Stream:
* onNext(quote) <- gateway (<--- network ---) node1 <- onNext(quote)
* }
* </pre>
* the SimpleQuoteService generates random stock price every 1 second. each time subscriber subscribe he will get all
* the history plus the next coming update of stock price.
* the example for simplicity using both nodes on the same jvm for ease of debugging.
* @param args none.
*/
public static void main(String[] args) {
// seed node as gateway
Microservices gateway = Microservices.builder().build();
// cluster member node with SimpleQuoteService
// join gateway cluster.
Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
// create stock Quote service from gateway.
QuoteService qouteService = gateway.proxy().api(// Quote service interface.
QuoteService.class).create();
// subscribe on quotes changes
qouteService.quotes("ORCL").subscribe(onNext -> {
System.out.println(onNext);
});
}
use of io.scalecube.services.Microservices in project scalecube by scalecube.
the class TestStreamingService method test_unknown_method.
@Test
public void test_unknown_method() throws InterruptedException {
Microservices gateway = Microservices.builder().build();
Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
ServiceCall service = gateway.dispatcher().create();
final CountDownLatch latch1 = new CountDownLatch(1);
Message scheduled = Messages.builder().request(QuoteService.NAME, "unknonwn").build();
try {
service.listen(scheduled);
} catch (Exception ex) {
if (ex.getMessage().contains("No reachable member with such service: unknonwn")) {
latch1.countDown();
}
}
latch1.await(2, TimeUnit.SECONDS);
assertTrue(latch1.getCount() == 0);
node.shutdown();
gateway.shutdown();
}
Aggregations