Skip to content

Commit

Permalink
Merge pull request #766 from scalecube/cleanup-service-discovery-inte…
Browse files Browse the repository at this point in the history
…rface

Cleanup service discovery interface
  • Loading branch information
artem-v authored Jun 15, 2020
2 parents 3c9f0ed + d575b4c commit 6bb4d61
Show file tree
Hide file tree
Showing 37 changed files with 607 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import java.lang.annotation.Target;

/**
* This annotation is used to mark the method which will be executed before shutdown of service
* <br>
* This annotation is used to mark the method which will be executed before shutdown of service <br>
* Scalecube services doesn't support {@link javax.annotation.PreDestroy} since Java API *
* Specification for it has strict limitation for annotated method.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
package io.scalecube.services.discovery.api;

import io.scalecube.net.Address;
import io.scalecube.services.ServiceEndpoint;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface ServiceDiscovery {

/**
* Returns service discovery address.
*
* @return discovery address
*/
Address address();

/**
* Returns service endpoint.
*
* @return service endpoint
*/
ServiceEndpoint serviceEndpoint();

/**
* Function to subscribe and listen on {@code ServiceDiscoveryEvent} events.
*
* @return stream of {@code ServiceDiscoveryEvent} events
*/
Flux<ServiceDiscoveryEvent> listenDiscovery();
Flux<ServiceDiscoveryEvent> listen();

/**
* Starting this {@code ServiceDiscovery} instance.
*
* @return started {@code ServiceDiscovery} instance
*/
Mono<ServiceDiscovery> start();
Mono<Void> start();

/**
* Shutting down this {@code ServiceDiscovery} instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.scalecube.services.discovery.api;

import io.scalecube.net.Address;
import java.util.Objects;
import java.util.StringJoiner;
import reactor.core.publisher.Flux;

public final class ServiceDiscoveryContext {

private final String id;
private final Address address;
private final ServiceDiscovery discovery;

private ServiceDiscoveryContext(Builder builder) {
this.id = Objects.requireNonNull(builder.id, "discoveryContext.id");
this.address = Objects.requireNonNull(builder.address, "discoveryContext.address");
this.discovery = Objects.requireNonNull(builder.discovery, "discoveryContext.discovery");
}

public static Builder builder() {
return new Builder();
}

public String id() {
return id;
}

public Address address() {
return address;
}

public Flux<ServiceDiscoveryEvent> listen() {
return discovery.listen();
}

@Override
public String toString() {
return new StringJoiner(", ", ServiceDiscoveryContext.class.getSimpleName() + "[", "]")
.add("id='" + id + "'")
.add("address=" + address)
.add("discovery=" + discovery)
.toString();
}

public static class Builder {

private String id;
private Address address;
private ServiceDiscovery discovery;

private Builder() {}

public Builder id(String id) {
this.id = id;
return this;
}

public Builder address(Address address) {
this.address = address;
return this;
}

public Builder discovery(ServiceDiscovery discovery) {
this.discovery = discovery;
return this;
}

public ServiceDiscoveryContext build() {
return new ServiceDiscoveryContext(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Objects;
import java.util.StringJoiner;

public class ServiceDiscoveryEvent {
public final class ServiceDiscoveryEvent {

public enum Type {
ENDPOINT_ADDED, // service endpoint added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.scalecube.services.discovery.api;

import io.scalecube.services.ServiceEndpoint;

@FunctionalInterface
public interface ServiceDiscoveryFactory {

ServiceDiscovery createServiceDiscovery(ServiceEndpoint serviceEndpoint);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.scalecube.services.discovery.api;

import io.scalecube.services.ServiceEndpoint;
import java.util.StringJoiner;
import java.util.UUID;
import java.util.function.Consumer;

public final class ServiceDiscoveryOptions {

private String id = UUID.randomUUID().toString();
private ServiceEndpoint serviceEndpoint;
private ServiceDiscoveryFactory discoveryFactory;

public ServiceDiscoveryOptions() {}

/**
* ServiceDiscoveryOptions copy constructor.
*
* @param other ServiceDiscoveryOptions to copy
*/
public ServiceDiscoveryOptions(ServiceDiscoveryOptions other) {
this.id = other.id;
this.serviceEndpoint = other.serviceEndpoint;
this.discoveryFactory = other.discoveryFactory;
}

private ServiceDiscoveryOptions set(Consumer<ServiceDiscoveryOptions> c) {
ServiceDiscoveryOptions s = new ServiceDiscoveryOptions(this);
c.accept(s);
return s;
}

public ServiceDiscoveryOptions id(String id) {
return set(o -> o.id = id);
}

public String id() {
return id;
}

public ServiceDiscoveryOptions serviceEndpoint(ServiceEndpoint serviceEndpoint) {
return set(o -> o.serviceEndpoint = serviceEndpoint);
}

public ServiceEndpoint serviceEndpoint() {
return serviceEndpoint;
}

public ServiceDiscoveryOptions discoveryFactory(ServiceDiscoveryFactory discoveryFactory) {
return set(o -> o.discoveryFactory = discoveryFactory);
}

public ServiceDiscoveryFactory discoveryFactory() {
return discoveryFactory;
}

@Override
public String toString() {
return new StringJoiner(", ", ServiceDiscoveryOptions.class.getSimpleName() + "[", "]")
.add("id='" + id + "'")
.add("serviceEndpoint=" + serviceEndpoint)
.add("discoveryFactory=" + discoveryFactory)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package io.scalecube.services.gateway;

import io.scalecube.services.ServiceCall;
import java.util.StringJoiner;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

public class GatewayOptions {

private Executor workerPool;
private ServiceCall call;
private String id;
private int port = 0;
private Executor workerPool;
private ServiceCall call;

public GatewayOptions() {}

Expand Down Expand Up @@ -62,4 +63,14 @@ public GatewayOptions call(ServiceCall call) {
public ServiceCall call() {
return call;
}

@Override
public String toString() {
return new StringJoiner(", ", GatewayOptions.class.getSimpleName() + "[", "]")
.add("id='" + id + "'")
.add("port=" + port)
.add("workerPool=" + workerPool)
.add("call=" + call)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
import java.util.Map;
import java.util.Map.Entry;

/** Simple headers and data codec based on JDK only. */
public class JdkCodec implements DataCodec, HeadersCodec {

/**
* {@inheritDoc}
*/
@Override
public String contentType() {
return "application/octet-stream";
Expand All @@ -37,9 +33,6 @@ public void encode(OutputStream stream, Object value) throws IOException {
}
}

/**
* {@inheritDoc}
*/
@Override
public void encode(OutputStream stream, Map<String, String> headers) throws IOException {
if (headers.isEmpty()) {
Expand Down Expand Up @@ -70,9 +63,6 @@ public Object decode(InputStream stream, Type type) throws IOException {
}
}

/**
* {@inheritDoc}
*/
@Override
public Map<String, String> decode(InputStream stream) throws IOException {
if (stream.available() < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ public int hashCode() {
return Objects.hash(name);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@ public BenchmarkServiceState(BenchmarkSettings settings, Object... services) {
public void beforeAll() {
seed =
Microservices.builder()
.discovery(ScalecubeServiceDiscovery::new)
.discovery("seed", ScalecubeServiceDiscovery::new)
.transport(RSocketServiceTransport::new)
.startAwait();

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

node =
Microservices.builder()
.discovery(
endpoint ->
new ScalecubeServiceDiscovery(endpoint)
"node",
serviceEndpoint ->
new ScalecubeServiceDiscovery(serviceEndpoint)
.membership(cfg -> cfg.seedMembers(seedAddress)))
.transport(RSocketServiceTransport::new)
.services(services)
.startAwait();

LOGGER.info(
"Seed address: "
+ seed.discovery().address()
+ seed.discovery("seed").address()
+ ", services address: "
+ node.serviceAddress());
}
Expand Down
Loading

0 comments on commit 6bb4d61

Please sign in to comment.