Init
Advertise, not parsing response
This commit is contained in:
commit
beb6fdd353
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
target
|
||||
*.iml
|
||||
*.swp
|
||||
*~
|
||||
.idea
|
5
module.conf
Normal file
5
module.conf
Normal file
@ -0,0 +1,5 @@
|
||||
org.jboss.msc
|
||||
org.wildfly.swarm.topology:runtime
|
||||
org.wildfly.swarm.topology
|
||||
org.jboss.as.network
|
||||
io.vertx
|
89
pom.xml
Normal file
89
pom.xml
Normal file
@ -0,0 +1,89 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.meutel.swarm</groupId>
|
||||
<artifactId>topology-etcd</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>Topology etcd</name>
|
||||
<description>Wildfly Swarm topology implementation with etcd</description>
|
||||
<properties>
|
||||
<version.swarm>2017.6.0</version.swarm>
|
||||
<version.swarm.fraction-plugin>55</version.swarm.fraction-plugin>
|
||||
<version.vertx>3.3.3</version.vertx>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.wildfly.swarm</groupId>
|
||||
<artifactId>bom-all</artifactId>
|
||||
<version>${version.swarm}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>${version.vertx}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.inject</groupId>
|
||||
<artifactId>javax.inject</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wildfly.swarm</groupId>
|
||||
<artifactId>topology</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wildfly.swarm</groupId>
|
||||
<artifactId>msc</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wildfly.core</groupId>
|
||||
<artifactId>wildfly-network</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.wildfly.swarm</groupId>
|
||||
<artifactId>wildfly-swarm-fraction-plugin</artifactId>
|
||||
<version>${version.swarm.fraction-plugin}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,47 @@
|
||||
package net.meutel.swarm.etcd;
|
||||
|
||||
import org.wildfly.swarm.config.runtime.AttributeDocumentation;
|
||||
import org.wildfly.swarm.spi.api.Defaultable;
|
||||
import org.wildfly.swarm.spi.api.Fraction;
|
||||
import org.wildfly.swarm.spi.api.annotations.Configurable;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Fraction Etcd Topology.
|
||||
*/
|
||||
@Configurable("net.meutel.etcd")
|
||||
public class EtcdTopologyFraction implements Fraction<EtcdTopologyFraction> {
|
||||
|
||||
private static final URL DEFAULT_URL;
|
||||
|
||||
@AttributeDocumentation("Etcd URL")
|
||||
private Defaultable<URL> url = Defaultable.url(DEFAULT_URL);
|
||||
|
||||
static {
|
||||
URL tmp = null;
|
||||
try {
|
||||
tmp = new URL("http://localhost:2379");
|
||||
} catch (MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
DEFAULT_URL = tmp;
|
||||
}
|
||||
|
||||
public EtcdTopologyFraction() {
|
||||
this(DEFAULT_URL);
|
||||
}
|
||||
|
||||
public EtcdTopologyFraction(URL url) {
|
||||
this.url.set(url);
|
||||
}
|
||||
|
||||
public EtcdTopologyFraction(String url) throws MalformedURLException {
|
||||
this.url.set(new URL(url));
|
||||
}
|
||||
|
||||
public URL url() {
|
||||
return this.url.get();
|
||||
}
|
||||
}
|
74
src/main/java/net/meutel/swarm/etcd/runtime/EtcdClient.java
Normal file
74
src/main/java/net/meutel/swarm/etcd/runtime/EtcdClient.java
Normal file
@ -0,0 +1,74 @@
|
||||
package net.meutel.swarm.etcd.runtime;
|
||||
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpClient;
|
||||
import io.vertx.core.http.HttpClientOptions;
|
||||
import io.vertx.core.http.HttpClientResponse;
|
||||
import org.jboss.msc.service.*;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Etcd Client.
|
||||
*/
|
||||
public class EtcdClient implements Service<EtcdClient> {
|
||||
|
||||
public static final ServiceName SERVICE_NAME = ServiceName.of("net", "meutel", "swarm","topology", "etcd", "client");
|
||||
|
||||
HttpClient httpClient;
|
||||
|
||||
URL url;
|
||||
|
||||
public EtcdClient(URL url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public void createInDir(String dir, String value) {
|
||||
String body = "value=" + value;
|
||||
// String body = "nimp";
|
||||
System.out.println(" - createInDir: " + body);
|
||||
this.httpClient.post("/v2/keys/" + dir, this::parseResponse)
|
||||
.exceptionHandler(throwable -> {
|
||||
System.err.println("EtcdClient error");
|
||||
throwable.printStackTrace();
|
||||
})
|
||||
// .setChunked(true)
|
||||
.putHeader("Content-Length", String.valueOf(body.length()))
|
||||
.putHeader("Content-Type", "application/x-www-form-urlencoded")
|
||||
.write(body)
|
||||
.end();
|
||||
}
|
||||
|
||||
private void parseResponse(HttpClientResponse resp) {
|
||||
int st = resp.statusCode();
|
||||
if (st < 200 || (st >= 300 && st < 400)) {
|
||||
System.err.println("Unexpected status: " + st);
|
||||
} else if (st >= 400 && st < 500) {
|
||||
System.err.println("Request error: " + st+ " (" + resp.statusMessage() + ")");
|
||||
} else if (st >= 500) {
|
||||
System.err.println("Etcd error: " + st+ " (" + resp.statusMessage() + ")");
|
||||
}
|
||||
resp.handler(buffer -> System.out.print(" = " + buffer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(StartContext startContext) throws StartException {
|
||||
System.out.println("Starting EtcdClient for " + this.url);
|
||||
this.httpClient = Vertx.vertx()
|
||||
.createHttpClient(new HttpClientOptions()
|
||||
.setLogActivity(true)
|
||||
.setDefaultHost(this.url.getHost())
|
||||
.setDefaultPort(this.url.getPort())
|
||||
.setConnectTimeout(1000)
|
||||
.setMaxPoolSize(10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(StopContext stopContext) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public EtcdClient getValue() throws IllegalStateException, IllegalArgumentException {
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package net.meutel.swarm.etcd.runtime;
|
||||
|
||||
import net.meutel.swarm.etcd.EtcdTopologyFraction;
|
||||
import org.jboss.msc.service.ServiceActivator;
|
||||
import org.jboss.msc.service.ServiceActivatorContext;
|
||||
import org.jboss.msc.service.ServiceRegistryException;
|
||||
import org.jboss.msc.service.ServiceTarget;
|
||||
import org.wildfly.swarm.topology.runtime.TopologyManager;
|
||||
import org.wildfly.swarm.topology.runtime.TopologyManagerActivator;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Any;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* EtcdClient MSC activator.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EtcdClientActivator implements ServiceActivator {
|
||||
|
||||
@Inject
|
||||
@Any
|
||||
EtcdTopologyFraction fraction;
|
||||
|
||||
public void activate(ServiceActivatorContext context) throws ServiceRegistryException {
|
||||
System.out.println("Activate Etcd client");
|
||||
ServiceTarget target = context.getServiceTarget();
|
||||
EtcdClient client = new EtcdClient(fraction.url());
|
||||
target.addService(EtcdClient.SERVICE_NAME, client)
|
||||
.install();
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package net.meutel.swarm.etcd.runtime;
|
||||
|
||||
import org.jboss.as.network.SocketBinding;
|
||||
import org.jboss.msc.inject.Injector;
|
||||
import org.jboss.msc.service.Service;
|
||||
import org.jboss.msc.service.StartContext;
|
||||
import org.jboss.msc.service.StartException;
|
||||
import org.jboss.msc.service.StopContext;
|
||||
import org.jboss.msc.value.InjectedValue;
|
||||
import org.wildfly.swarm.topology.TopologyConnector;
|
||||
import org.wildfly.swarm.topology.runtime.Registration;
|
||||
import org.wildfly.swarm.topology.runtime.TopologyManager;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Topology connector for etcd.
|
||||
*/
|
||||
public class EtcdTopologyConnector implements Service<EtcdTopologyConnector>, TopologyConnector {
|
||||
|
||||
private static String TOPOLOGY_SOURCE_KEY = "etcd";
|
||||
|
||||
private EtcdClient client;
|
||||
|
||||
private InjectedValue<TopologyManager> topologyManagerInjector = new InjectedValue<>();
|
||||
|
||||
private InjectedValue<EtcdClient> clientInjector = new InjectedValue<>();
|
||||
|
||||
public Injector<EtcdClient> getClientInjector() {
|
||||
return this.clientInjector;
|
||||
}
|
||||
|
||||
public EtcdTopologyConnector getValue() throws IllegalStateException, IllegalArgumentException {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Injector<TopologyManager> getTopologyManagerInjector() {
|
||||
return this.topologyManagerInjector;
|
||||
}
|
||||
|
||||
public void start(StartContext startContext) throws StartException {
|
||||
System.out.println("Starting EtcdTopology");
|
||||
this.client = this.clientInjector.getValue();
|
||||
}
|
||||
|
||||
public void stop(StopContext stopContext) {
|
||||
this.client = null;
|
||||
}
|
||||
|
||||
public synchronized void advertise(String name, SocketBinding binding, String... tags) throws Exception {
|
||||
Registration registration = new Registration(TOPOLOGY_SOURCE_KEY, name, binding.getAddress().getHostAddress(), binding.getAbsolutePort(), tags);
|
||||
System.out.println("registration: " + registration);
|
||||
|
||||
this.topologyManagerInjector.getValue().register(registration);
|
||||
|
||||
URL url = new URL("http", registration.getAddress(), registration.getPort(),"");
|
||||
System.out.println("URL for service " + name + ": " + url);
|
||||
client.createInDir(name, url.toExternalForm());
|
||||
}
|
||||
|
||||
public synchronized void unadvertise(String name, SocketBinding binding) throws Exception {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package net.meutel.swarm.etcd.runtime;
|
||||
|
||||
import org.jboss.msc.service.*;
|
||||
import org.wildfly.swarm.topology.runtime.TopologyManager;
|
||||
import org.wildfly.swarm.topology.runtime.TopologyManagerActivator;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
|
||||
/**
|
||||
* Etcd Topology MSC activator.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EtcdTopologyConnectorActivator implements ServiceActivator {
|
||||
|
||||
public void activate(ServiceActivatorContext context) throws ServiceRegistryException {
|
||||
System.out.println("Activate Etcd topology");
|
||||
ServiceTarget target = context.getServiceTarget();
|
||||
|
||||
EtcdTopologyConnector connector = new EtcdTopologyConnector();
|
||||
|
||||
target.addService(TopologyManagerActivator.CONNECTOR_SERVICE_NAME, connector)
|
||||
.addDependency(TopologyManagerActivator.SERVICE_NAME, TopologyManager.class, connector.getTopologyManagerInjector())
|
||||
.addDependency(EtcdClient.SERVICE_NAME, EtcdClient.class, connector.getClientInjector())
|
||||
.install();
|
||||
}
|
||||
}
|
11
src/main/resources/modules/io/vertx/main/module.xml
Normal file
11
src/main/resources/modules/io/vertx/main/module.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<module xmlns="urn:jboss:module:1.3" name="io.vertx">
|
||||
<resources>
|
||||
<artifact name="io.vertx:vertx-core:${version.vertx}"/>
|
||||
<artifact name="io.netty:netty-all:4.1.1.Final"/>
|
||||
</resources>
|
||||
<dependencies>
|
||||
<module name="javax.api"/>
|
||||
<module name="sun.jdk"/>
|
||||
<module name="org.javassist" optional="true"/>
|
||||
</dependencies>
|
||||
</module>
|
Loading…
Reference in New Issue
Block a user