Commit 30f9cd9f by amir

version 1.2.0, read the readme

parent 6396ffcb
### Microservice Framework in JAVA ### Microservice Framework in JAVA
## Version 1.2.0:
- Add Graphite Repoter for metrics:
Env Variables:
'graphite_hostport' (default to "localhost:2015")
'graphite_report_intrerval' in seconds, default is 10
- Add '/_stat' in monitoring to returns stats of metrics, more to come
- Change '/reload' to '/_reload'
## Version '1.1.0': ## Version '1.1.0':
- Add resolveService in service discovery, used by http command-client to resolve the address - Add resolveService in service discovery, used by http command-client to resolve the address
of the dest service of the dest service
......
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '1.1.0' version '1.2.0'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
...@@ -25,7 +25,9 @@ dependencies { ...@@ -25,7 +25,9 @@ dependencies {
compile 'com.ipgallery.common:utils:1.1.0' compile 'com.ipgallery.common:utils:1.1.0'
compile 'com.ipgallery.common:rabbitmq:1.0.1' compile 'com.ipgallery.common:rabbitmq:1.0.1'
compile 'com.ecwid.consul:consul-api:1.1.9' compile 'com.ecwid.consul:consul-api:1.1.9'
compile 'com.spotify:dns:3.1.1' compile 'com.github.davidb:metrics-influxdb:0.8.2'
compile 'io.dropwizard.metrics:metrics-graphite:3.1.2'
testCompile group: 'junit', name: 'junit', version: '4.11' testCompile group: 'junit', name: 'junit', version: '4.11'
} }
......
...@@ -3,6 +3,8 @@ package microservice.handlers; ...@@ -3,6 +3,8 @@ package microservice.handlers;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.databind.node.ObjectNode;
import microservice.io.impl.IMetricsFactoryImpl;
import microservice.types.BaseRestResponse; import microservice.types.BaseRestResponse;
import microservice.MicroserviceApp; import microservice.MicroserviceApp;
import microservice.RequestContext; import microservice.RequestContext;
...@@ -15,12 +17,13 @@ import microservice.io.iface.IContainer; ...@@ -15,12 +17,13 @@ import microservice.io.iface.IContainer;
*/ */
public class MonitorHandler extends BaseHandler public class MonitorHandler extends BaseHandler
{ {
private static final String RELOAD = "reload"; private static final String RELOAD = "_reload";
public static final String STAT = "_stat";
List<BaseHandler> containers = null; List<BaseHandler> containers = null;
public MonitorHandler() { public MonitorHandler() {
super(); super();
containers = new LinkedList<BaseHandler>(); containers = new LinkedList<BaseHandler>();
...@@ -30,19 +33,37 @@ public class MonitorHandler extends BaseHandler ...@@ -30,19 +33,37 @@ public class MonitorHandler extends BaseHandler
{ {
containers.add(handler); containers.add(handler);
} }
@Override @Override
public void doCreate(RequestContext reqCtx) public void doCreate(RequestContext reqCtx)
{ {
switch(reqCtx.params[0]) switch(reqCtx.params[0])
{ {
case RELOAD: case RELOAD:
reloadServiceApp(reqCtx); reloadServiceApp(reqCtx);
break; break;
case STAT:
printStats(reqCtx);
break;
} }
} }
private void printStats(RequestContext reqCtx) {
BaseRestResponse brr = new BaseRestResponse(true, null);
IMetricsFactoryImpl factoryImpl = (IMetricsFactoryImpl) IMetricsFactoryImpl.getInstance();
ObjectNode objectNode = this.objMapper.createObjectNode();
try {
objectNode.put("counters",this.objMapper.valueToTree(factoryImpl.getMetrics().getCounters()));
objectNode.put("meters",this.objMapper.valueToTree(factoryImpl.getMetrics().getMeters()));
objectNode.put("timers",this.objMapper.valueToTree(factoryImpl.getMetrics().getTimers()));
} catch (IllegalArgumentException exp){
brr.setError(exp.toString());
}
brr.objectNode = objectNode;
writeObjectToResponse(reqCtx, brr);
}
private void reloadServiceApp(RequestContext reqCtx) { private void reloadServiceApp(RequestContext reqCtx) {
this.optConfiguration.ifPresent(cfg -> cfg.reload()); this.optConfiguration.ifPresent(cfg -> cfg.reload());
...@@ -53,6 +74,13 @@ public class MonitorHandler extends BaseHandler ...@@ -53,6 +74,13 @@ public class MonitorHandler extends BaseHandler
@Override @Override
public void doRead(RequestContext reqCtx) public void doRead(RequestContext reqCtx)
{ {
switch(reqCtx.params[0])
{
case STAT:
printStats(reqCtx);
break;
}
} }
@Override @Override
......
package microservice.io.impl; package microservice.io.impl;
import com.codahale.metrics.Counter; import com.codahale.metrics.*;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Timer.Context; import com.codahale.metrics.Timer.Context;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.codahale.metrics.graphite.PickledGraphite;
import microservice.MicroserviceApp;
import microservice.io.iface.IMetricsFactory; import microservice.io.iface.IMetricsFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
public class IMetricsFactoryImpl implements IMetricsFactory public class IMetricsFactoryImpl implements IMetricsFactory
{ {
public static IMetricsFactoryImpl sInstance = null; public static IMetricsFactoryImpl sInstance = null;
final MetricRegistry metrics = new MetricRegistry(); final MetricRegistry metrics = new MetricRegistry();
JmxReporter reporter; GraphiteReporter reporter;
public class IMeterImpl implements IMetricsFactory.IMeter public class IMeterImpl implements IMetricsFactory.IMeter
{ {
...@@ -136,8 +139,17 @@ public class IMetricsFactoryImpl implements IMetricsFactory ...@@ -136,8 +139,17 @@ public class IMetricsFactoryImpl implements IMetricsFactory
@Override @Override
public void startReporting() public void startReporting()
{ {
reporter = JmxReporter.forRegistry(metrics).build(); String graphite_hostport = MicroserviceApp.getsInstance().getConfiguration().getString("graphite_hostport", "localhost:2015");
reporter.start(); Long interval = MicroserviceApp.getsInstance().getConfiguration().getLong("graphite_report_intrerval",Long.valueOf(10));
int index = graphite_hostport.indexOf(':');
final PickledGraphite pickledGraphite = new PickledGraphite(new InetSocketAddress(graphite_hostport.substring(0,index), Integer.valueOf(graphite_hostport.substring(index + 1)).intValue()));
reporter = GraphiteReporter.forRegistry(metrics)
.prefixedWith("web1.example.com")
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.build(pickledGraphite);
reporter.start(interval.intValue(), TimeUnit.SECONDS);
} }
@Override @Override
......
package microservice;
import com.spotify.dns.DnsException;
import com.spotify.dns.DnsSrvResolver;
import com.spotify.dns.DnsSrvResolvers;
import com.spotify.dns.LookupResult;
import com.spotify.dns.statistics.DnsReporter;
import com.spotify.dns.statistics.DnsTimingContext;
import org.junit.Test;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.SimpleResolver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* Created by amir on 07/06/16.
*/
public class TestDnsResolver {
private static final DnsReporter REPORTER = new StdoutReporter();
public static void main(String[] args) throws IOException {
DnsSrvResolver resolver = DnsSrvResolvers.newBuilder()
.cachingLookups(true)
.retainingDataOnFailures(true)
.metered(REPORTER)
.dnsLookupTimeoutMillis(1000)
.build();
boolean quit = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
SimpleResolver simpleResolver = new SimpleResolver("127.0.0.1");
simpleResolver.setPort(8600);
Lookup.setDefaultResolver(simpleResolver);
while (!quit) {
System.out.print("Enter a SRV name (empty to quit): ");
String line = in.readLine();
if (line == null || line.isEmpty()) {
quit = true;
} else {
try {
List<LookupResult> nodes = resolver.resolve(line);
for (LookupResult node : nodes) {
System.out.println(node);
}
} catch (DnsException e) {
e.printStackTrace(System.out);
}
}
}
}
public static class StdoutReporter implements DnsReporter {
@Override
public DnsTimingContext resolveTimer() {
return new DnsTimingContext() {
private final long start = System.currentTimeMillis();
@Override
public void stop() {
final long now = System.currentTimeMillis();
final long diff = now - start;
System.out.println("Request took " + diff + "ms");
}
};
}
@Override
public void reportFailure(Throwable error) {
System.err.println("Error when resolving: " + error);
error.printStackTrace(System.err);
}
@Override
public void reportEmpty() {
System.out.println("Empty response from server.");
}
}
}
package microservice;
/**
* Created by amir on 07/06/16.
*/
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
import java.io.*;
import java.net.*;
import org.xbill.DNS.*;
/** @author Brian Wellington &lt;bwelling@xbill.org&gt; */
public class dig {
static Name name = null;
static int type = Type.A, dclass = DClass.IN;
static void
usage() {
System.out.println("Usage: dig [@server] name [<type>] [<class>] " +
"[options]");
System.exit(0);
}
static void
doQuery(Message response, long ms) throws IOException {
System.out.println("; java dig 0.0");
System.out.println(response);
System.out.println(";; Query time: " + ms + " ms");
}
static void
doAXFR(Message response) throws IOException {
System.out.println("; java dig 0.0 <> " + name + " axfr");
if (response.isSigned()) {
System.out.print(";; TSIG ");
if (response.isVerified())
System.out.println("ok");
else
System.out.println("failed");
}
if (response.getRcode() != Rcode.NOERROR) {
System.out.println(response);
return;
}
Record [] records = response.getSectionArray(Section.ANSWER);
for (int i = 0; i < records.length; i++)
System.out.println(records[i]);
System.out.print(";; done (");
System.out.print(response.getHeader().getCount(Section.ANSWER));
System.out.print(" records, ");
System.out.print(response.getHeader().getCount(Section.ADDITIONAL));
System.out.println(" additional)");
}
public static void
main(String argv[]) throws IOException {
String server = null;
int arg;
Message query, response;
Record rec;
SimpleResolver res = null;
boolean printQuery = false;
long startTime, endTime;
if (argv.length < 1) {
usage();
}
try {
arg = 0;
if (argv[arg].startsWith("@"))
server = argv[arg++].substring(1);
if (server != null)
res = new SimpleResolver(server);
else
res = new SimpleResolver();
String nameString = argv[arg++];
if (nameString.equals("-x")) {
name = ReverseMap.fromAddress(argv[arg++]);
type = Type.PTR;
dclass = DClass.IN;
}
else {
name = Name.fromString(nameString, Name.root);
type = Type.value(argv[arg]);
if (type < 0)
type = Type.A;
else
arg++;
dclass = DClass.value(argv[arg]);
if (dclass < 0)
dclass = DClass.IN;
else
arg++;
}
while (argv[arg].startsWith("-") && argv[arg].length() > 1) {
switch (argv[arg].charAt(1)) {
case 'p':
String portStr;
int port;
if (argv[arg].length() > 2)
portStr = argv[arg].substring(2);
else
portStr = argv[++arg];
port = Integer.parseInt(portStr);
if (port < 0 || port > 65536) {
System.out.println("Invalid port");
return;
}
res.setPort(port);
break;
case 'b':
String addrStr;
if (argv[arg].length() > 2)
addrStr = argv[arg].substring(2);
else
addrStr = argv[++arg];
InetAddress addr;
try {
addr = InetAddress.getByName(addrStr);
}
catch (Exception e) {
System.out.println("Invalid address");
return;
}
res.setLocalAddress(addr);
break;
case 'k':
String key;
if (argv[arg].length() > 2)
key = argv[arg].substring(2);
else
key = argv[++arg];
res.setTSIGKey(TSIG.fromString(key));
break;
case 't':
res.setTCP(true);
break;
case 'i':
res.setIgnoreTruncation(true);
break;
case 'e':
String ednsStr;
int edns;
if (argv[arg].length() > 2)
ednsStr = argv[arg].substring(2);
else
ednsStr = argv[++arg];
edns = Integer.parseInt(ednsStr);
if (edns < 0 || edns > 1) {
System.out.println("Unsupported " +
"EDNS level: " +
edns);
return;
}
res.setEDNS(edns);
break;
case 'd':
res.setEDNS(0, 0, ExtendedFlags.DO, null);
break;
case 'q':
printQuery = true;
break;
default:
System.out.print("Invalid option: ");
System.out.println(argv[arg]);
}
arg++;
}
}
catch (ArrayIndexOutOfBoundsException e) {
if (name == null)
usage();
}
if (res == null)
res = new SimpleResolver();
rec = Record.newRecord(name, type, dclass);
query = Message.newQuery(rec);
if (printQuery)
System.out.println(query);
startTime = System.currentTimeMillis();
response = res.send(query);
endTime = System.currentTimeMillis();
if (type == Type.AXFR)
doAXFR(response);
else
doQuery(response, endTime - startTime);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment