Skip to content

Commit

Permalink
Enhance microservices builder (#823)
Browse files Browse the repository at this point in the history
* Added "default" shortcut id for discovery
  • Loading branch information
artem-v authored Oct 25, 2021
1 parent 7762601 commit fb3603d
Show file tree
Hide file tree
Showing 22 changed files with 63 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ public String name() {
}
}

private class SubServiceImpl extends ServiceImpl {

}
private class SubServiceImpl extends ServiceImpl {}

@Test
public void testSubServiceInterfaces() {
Expand All @@ -134,7 +132,6 @@ public void testSubServiceInterfaces() {
Stream<Class<?>> interfaces = Reflect.serviceInterfaces(new SubServiceImpl());
// Then:
Assertions.assertEquals(
1,
interfaces.count(), "serviceInterfaces(..) should detect interfaces in SubServiceImpl");
1, interfaces.count(), "serviceInterfaces(..) should detect interfaces in SubServiceImpl");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static void main(String[] args) {
Microservices service =
Microservices.builder()
.discovery(
"service",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -44,7 +43,7 @@ public static void main(String[] args) {

Microservices userProfileCaller =
Microservices.builder()
.discovery("caller", endpoint -> discovery(service, endpoint))
.discovery(endpoint -> discovery(service, endpoint))
.transport(
() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier()))
.startAwait();
Expand All @@ -60,7 +59,7 @@ public static void main(String[] args) {

Microservices apiKeyCaller =
Microservices.builder()
.discovery("caller", endpoint -> discovery(service, endpoint))
.discovery(endpoint -> discovery(service, endpoint))
.transport(
() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier()))
.startAwait();
Expand Down Expand Up @@ -161,6 +160,6 @@ private static ScalecubeServiceDiscovery discovery(
return new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(endpoint))
.membership(opts -> opts.seedMembers(service.discovery("service").address()));
.membership(opts -> opts.seedMembers(service.discovery().address()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void main(String[] args) {
Microservices service =
Microservices.builder()
.discovery(
"service",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -35,7 +34,7 @@ public static void main(String[] args) {

Microservices caller =
Microservices.builder()
.discovery("caller", endpoint -> discovery(service, endpoint))
.discovery(endpoint -> discovery(service, endpoint))
.transport(() -> new RSocketServiceTransport().credentialsSupplier(credsSupplier()))
.startAwait();

Expand Down Expand Up @@ -80,6 +79,6 @@ private static ScalecubeServiceDiscovery discovery(
return new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(endpoint))
.membership(opts -> opts.seedMembers(service.discovery("service").address()));
.membership(opts -> opts.seedMembers(service.discovery().address()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public class Example1 {
* @param args ignored
*/
public static void main(String[] args) {
// ScaleCube Node node with no members
// ScaleCube Node with no members
Microservices seed =
Microservices.builder()
.discovery(
"seed",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -33,13 +32,12 @@ public static void main(String[] args) {
.defaultContentType(PROTOSTUFF) // set explicit default data format
.startAwait();

final Address seedAddress = seed.discovery("seed").address();
final Address seedAddress = seed.discovery().address();

// Construct a ScaleCube node which joins the cluster hosting the Greeting Service
Microservices ms =
Microservices.builder()
.discovery(
"ms",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static void main(String[] args) throws InterruptedException {
Microservices ms1 =
Microservices.builder()
.discovery(
"ms1",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -35,12 +34,11 @@ public static void main(String[] args) throws InterruptedException {

System.err.println("ms1 started: " + ms1.serviceAddress());

final Address address1 = ms1.discovery("ms1").address();
final Address address1 = ms1.discovery().address();

Microservices ms2 =
Microservices.builder()
.discovery(
"ms2",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ public class Example1 {
* @param args ignored
*/
public static void main(String[] args) {
// ScaleCube Node node with no members
// ScaleCube Node with no members
Microservices seed =
Microservices.builder()
.discovery(
"seed",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(serviceEndpoint)))
.transport(RSocketServiceTransport::new)
.startAwait();

final Address seedAddress = seed.discovery("seed").address();
final Address seedAddress = seed.discovery().address();

// Construct a ScaleCube node which joins the cluster hosting the Greeting Service
Microservices ms =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ public static void main(String[] args) {
.startAwait();

// Construct a ScaleCube node which joins the cluster hosting the Greeting Service
final Address seedAddress = seed.discovery("seed").address();
final Address seedAddress = seed.discovery().address();

Microservices ms =
Microservices.builder()
.discovery(
"ms",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ public class Example3 {
* @param args ignored
*/
public static void main(String[] args) {
// ScaleCube Node node with no members
// ScaleCube Node with no members
Microservices seed =
Microservices.builder()
.discovery(
"seed",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(serviceEndpoint)))
.transport(RSocketServiceTransport::new)
.startAwait();

final Address seedAddress = seed.discovery("seed").address();
final Address seedAddress = seed.discovery().address();

// Construct a ScaleCube node which joins the cluster hosting the Greeting Service
Microservices ms =
Microservices.builder()
.discovery(
"ms",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ public static void main(String[] args) {
Microservices gateway =
Microservices.builder()
.discovery(
"gateway",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(serviceEndpoint)))
.transport(RSocketServiceTransport::new)
.startAwait();

final Address gatewayAddress = gateway.discovery("gateway").address();
final Address gatewayAddress = gateway.discovery().address();

Microservices service2Node =
Microservices.builder()
.discovery(
"service2Node",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -45,7 +43,6 @@ public static void main(String[] args) {
Microservices service1Node =
Microservices.builder()
.discovery(
"service1Node",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ public static void main(String[] args) {
Microservices gateway =
Microservices.builder()
.discovery(
"gateway",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(serviceEndpoint)))
.transport(RSocketServiceTransport::new)
.startAwait();

final Address gatewayAddress = gateway.discovery("gateway").address();
final Address gatewayAddress = gateway.discovery().address();

Microservices service2Node =
Microservices.builder()
.discovery(
"service2Node",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -45,7 +43,6 @@ public static void main(String[] args) {
Microservices service1Node =
Microservices.builder()
.discovery(
"service1Node",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
14 changes: 14 additions & 0 deletions services/src/main/java/io/scalecube/services/Microservices.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,16 @@ public List<ServiceEndpoint> serviceEndpoints() {
return serviceRegistry.listServiceEndpoints();
}

/**
* Returns default service discovery context.
*
* @see Microservices.Builder#discovery(String, ServiceDiscoveryFactory)
* @return service discovery context
*/
public ServiceDiscoveryContext discovery() {
return discovery("default");
}

/**
* Returns service discovery context by id.
*
Expand Down Expand Up @@ -459,6 +469,10 @@ public Builder methodRegistry(ServiceMethodRegistry methodRegistry) {
return this;
}

public Builder discovery(ServiceDiscoveryFactory discoveryFactory) {
return discovery("default", discoveryFactory);
}

public Builder discovery(String id, ServiceDiscoveryFactory discoveryFactory) {
this.compositeDiscovery.addOperator(opts -> opts.id(id).discoveryFactory(discoveryFactory));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static void initNodes() {
provider =
Microservices.builder()
.discovery(
"provider",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -43,12 +42,11 @@ public static void initNodes() {
.services(new GreetingServiceImpl())
.startAwait();

final Address seedAddress = provider.discovery("provider").address();
final Address seedAddress = provider.discovery().address();

consumer =
Microservices.builder()
.discovery(
"consumer",
endpoint ->
new ScalecubeServiceDiscovery()
.membership(cfg -> cfg.seedMembers(seedAddress))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ static void beforeAll() {
service =
Microservices.builder()
.discovery(
"service",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -92,7 +91,6 @@ static void beforeAll() {
serviceWithoutAuthenticator =
Microservices.builder()
.discovery(
"service",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand All @@ -107,7 +105,6 @@ static void beforeAll() {
partiallySecuredService =
Microservices.builder()
.discovery(
"service",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down Expand Up @@ -285,7 +282,7 @@ void successfulCallOfPublicMethodWithoutAuthentication() {

private static Microservices newCaller() {
return Microservices.builder()
.discovery("caller", ServiceAuthRemoteTest::serviceDiscovery)
.discovery(ServiceAuthRemoteTest::serviceDiscovery)
.transport(
() ->
new RSocketServiceTransport()
Expand All @@ -295,14 +292,14 @@ private static Microservices newCaller() {

private static Microservices newEmptyCredentialsCaller() {
return Microservices.builder()
.discovery("caller", ServiceAuthRemoteTest::serviceDiscovery)
.discovery(ServiceAuthRemoteTest::serviceDiscovery)
.transport(RSocketServiceTransport::new)
.startAwait();
}

private static Microservices newInvalidCredentialsCaller() {
return Microservices.builder()
.discovery("caller", ServiceAuthRemoteTest::serviceDiscovery)
.discovery(ServiceAuthRemoteTest::serviceDiscovery)
.transport(
() ->
new RSocketServiceTransport()
Expand Down Expand Up @@ -339,8 +336,8 @@ private static ServiceDiscovery serviceDiscovery(ServiceEndpoint endpoint) {
.membership(
opts ->
opts.seedMembers(
service.discovery("service").address(),
serviceWithoutAuthenticator.discovery("service").address(),
partiallySecuredService.discovery("service").address()));
service.discovery().address(),
serviceWithoutAuthenticator.discovery().address(),
partiallySecuredService.discovery().address()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void test_local_async_no_params() {
private static Microservices serviceProvider() {
return Microservices.builder()
.discovery(
"serviceProvider",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ public static void tearDown() {
private static Microservices serviceProvider(Object service) {
return Microservices.builder()
.discovery(
"serviceProvider",
endpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
.options(opts -> opts.metadata(endpoint))
.membership(cfg -> cfg.seedMembers(gateway.discovery("gateway").address())))
.membership(cfg -> cfg.seedMembers(gateway.discovery().address())))
.transport(RSocketServiceTransport::new)
.services(service)
.startAwait();
Expand Down Expand Up @@ -302,7 +301,6 @@ private static Optional<ServiceReference> route(
private static Microservices gateway() {
return Microservices.builder()
.discovery(
"gateway",
serviceEndpoint ->
new ScalecubeServiceDiscovery()
.transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory()))
Expand Down
Loading

0 comments on commit fb3603d

Please sign in to comment.