Created
December 30, 2017 11:48
-
-
Save johnou/92d967ea28f8d8ecdc2e105e8f109c4e to your computer and use it in GitHub Desktop.
Atomix cluster test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.atomix.cluster.Node; | |
import io.atomix.core.Atomix; | |
import io.atomix.messaging.Endpoint; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import java.io.File; | |
import java.util.concurrent.CompletableFuture; | |
/** | |
* @author Johno Crawford ([email protected]) | |
*/ | |
public class Bootstrap { | |
private static final Logger logger = LogManager.getLogger(Bootstrap.class); | |
private static final Object lock = new Object(); | |
public static void main(String[] args) throws InterruptedException { | |
Node[] bootstrapNodes = new Node[] {Node.builder("server1") | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", 5001)) | |
.build(), | |
Node.builder("server2") | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", 5002)) | |
.build() }; | |
Atomix atomix1 = createAtomicNode("server1", 5001, bootstrapNodes); | |
Atomix atomix2 = createAtomicNode("server2", 5002, bootstrapNodes); | |
CompletableFuture.allOf(atomix1.start(), atomix2.start()).whenCompleteAsync((aVoid, throwable) -> { | |
}).join(); | |
logger.info("server3 joining cluster"); | |
Atomix atomix3 = createAtomicNode("server3", 5003, bootstrapNodes).start().join(); | |
for (Node node : atomix3.metadataService().getMetadata().bootstrapNodes()) { | |
logger.info("Node id {} state {}", node.id(), node.getState()); | |
} | |
atomix3.stop().join(); | |
logger.info("server3 left cluster"); | |
synchronized (lock) { | |
lock.wait(); | |
} | |
} | |
private static Atomix createAtomicNode(String nodeId, int port, Node[] bootstrapNodes) { | |
Atomix.Builder builder = Atomix.builder(); | |
builder.withLocalNode(Node.builder(nodeId) | |
.withType(Node.Type.DATA) | |
.withEndpoint(Endpoint.from("127.0.0.1", port)) | |
.build()); | |
return builder.withDataDirectory(new File(System.getProperty("user.dir"), "data/" + nodeId)).withBootstrapNodes(bootstrapNodes).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment