Commit adaa72f1 by Amir Aharon

first commit with seperation of jars:

common,clients,pubsub and app
parent e667691a
Showing with 362 additions and 200 deletions
...@@ -37,13 +37,13 @@ ...@@ -37,13 +37,13 @@
- add ZeroMQ support for Rest - add ZeroMQ support for Rest
## 1.3.8: Add colorful logging ## 1.3.8: Add colorful logging
## 1.3.7: Add Pagination - PageBuilder ## 1.3.7: Add Pagination - PageBuilder
## 1.3.6: upgrade common-utils 1.2.0 ## 1.3.6: upgrade common-common.microservice.utils 1.2.0
## 1.3.2: ## 1.3.2:
- add getMD5Hash, catch unhandled exceptions, add command-params builder - add getMD5Hash, catch unhandled exceptions, add command-params builder
## 1.3.1: ## 1.3.1:
- add rabbotmq command client + change version to 1.3.1 - add rabbotmq command client + change version to 1.3.1
## 1.3.0: ## 1.3.0:
- add service authorization with jwt - add microservice.service authorization with jwt
- env param override config file - env param override config file
## 1.2.5: ## 1.2.5:
- support rabbit on server side - support rabbit on server side
...@@ -68,10 +68,10 @@ ...@@ -68,10 +68,10 @@
- Add '/_stat' in monitoring to returns stats of metrics, more to come - Add '/_stat' in monitoring to returns stats of metrics, more to come
- Change '/reload' to '/_reload' - 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 microservice.service discovery, used by http command-client to resolve the address
of the dest service of the dest microservice.service
# Env for service discovery: # Env for microservice.service discovery:
service.resolver.polling.interval (milli) default is 5000 microservice.service.resolver.polling.interval (milli) default is 5000
# Env for jwt: # Env for jwt:
jwt.token.in.authorization : true jwt.token.in.authorization : true
jwt.salt : "12345678901234567890123456789012" jwt.salt : "12345678901234567890123456789012"
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '2.2.0' version '2.3.0'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
...@@ -18,9 +18,35 @@ repositories { ...@@ -18,9 +18,35 @@ repositories {
} }
sourceSets { sourceSets {
main main {
// client java {
common srcDir 'src/main/java'
exclude '**'
}
}
common {
java.srcDir 'src/main/java/microservice/common'
resources.srcDir 'src/main/java/microservice/common/resources'
}
clients {
java.srcDir'src/main/java/microservice/clients'
resources.srcDir 'src/main/java/microservice/clients/resources'
}
servicePubsub {
java.srcDir 'src/main/java/microservice/service/pubsub'
resources.srcDir 'src/main/java/microservice/service/resources'
}
app {
java.srcDir 'src/main/java/microservice/app'
resources.srcDir 'src/main/java/microservice/resources'
compileClasspath += sourceSets.servicePubsub.runtimeClasspath
compileClasspath += sourceSets.clients.runtimeClasspath
}
} }
dependencies { dependencies {
...@@ -33,21 +59,31 @@ dependencies { ...@@ -33,21 +59,31 @@ dependencies {
'com.netflix.hystrix:hystrix-core:1.4.14' 'com.netflix.hystrix:hystrix-core:1.4.14'
) )
// clientCompile ( clientsCompile (
// files('build/common/microservice-common.jar'), files('build/common/microservice-common.jar'),
//// files('build/common/activities-common.jar'), 'com.ipgallery.common:utils:1.2.5',
// //'com.ipgallery.common:microservice:2.1.1' 'com.netflix.rxjava:rxjava-apache-http:0.20.7',
// ) // 'com.netflix.hystrix:hystrix-codahale-metrics-publisher:1.4.14',
'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.12',
'com.squareup.okhttp3:okhttp:3.8.0'
)
servicePubsubCompile (
files('build/common/microservice-common.jar'),
'org.apache.pulsar:pulsar-client:2.4.2',
'org.apache.pulsar:pulsar-client-admin:2.4.2'
)
compile ( appCompile (
files('build/common/microservice-common.jar'), files('build/common/microservice-common.jar'),
// files('build/client/microservice-client.jar'),
'io.jsonwebtoken:jjwt:0.6.0', 'io.jsonwebtoken:jjwt:0.6.0',
'io.undertow:undertow-core:2.0.28.Final', 'io.undertow:undertow-core:2.0.28.Final',
'com.fasterxml.jackson.core:jackson-databind:2.2.3', 'com.fasterxml.jackson.core:jackson-databind:2.2.3',
'io.dropwizard.metrics:metrics-core:3.1.0', 'io.dropwizard.metrics:metrics-core:3.1.0',
'com.netflix.hystrix:hystrix-codahale-metrics-publisher:1.4.14', // 'com.netflix.hystrix:hystrix-codahale-metrics-publisher:1.4.14',
'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.12', // 'com.netflix.hystrix:hystrix-metrics-event-stream:1.4.12',
'redis.clients:jedis:2.4.2', 'redis.clients:jedis:2.4.2',
'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2', 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2',
'com.ipgallery.common:utils:1.2.5', 'com.ipgallery.common:utils:1.2.5',
...@@ -58,11 +94,8 @@ dependencies { ...@@ -58,11 +94,8 @@ dependencies {
'io.dropwizard.metrics:metrics-graphite:3.2.5', 'io.dropwizard.metrics:metrics-graphite:3.2.5',
'org.zeromq:jeromq:0.4.0', 'org.zeromq:jeromq:0.4.0',
'org.elasticsearch.client:rest:5.4.1', 'org.elasticsearch.client:rest:5.4.1',
'com.netflix.rxjava:rxjava-apache-http:0.20.7', // 'com.netflix.rxjava:rxjava-apache-http:0.20.7',
'com.squareup.okhttp3:okhttp:3.8.0', // 'com.squareup.okhttp3:okhttp:3.8.0'
'org.apache.pulsar:pulsar-client:2.4.2',
'org.apache.pulsar:pulsar-client-admin:2.4.2'
) )
// compile group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.2' // compile group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.2'
...@@ -82,21 +115,51 @@ task commonJar(type: Jar) { ...@@ -82,21 +115,51 @@ task commonJar(type: Jar) {
destinationDir = file("build/common") destinationDir = file("build/common")
} }
//// client // pubsub
//task clientJar(type: Jar) { task servicePubsubJar(type: Jar) {
// dependsOn commonJar dependsOn commonJar
// mustRunAfter commonJar mustRunAfter commonJar
// from configurations.clientCompile.collect { zipTree it } from configurations.servicePubsubCompile.collect { zipTree it }
// from sourceSets.client.output from sourceSets.servicePubsub.output
// version = '1.0.0' version = '1.0.0'
// archiveName = "microservice-client.jar" archiveName = "microservice-service-pubsub.jar"
// destinationDir = file("build/client") destinationDir = file("build/servicePubsub")
//} }
jar { // client
task clientsJar(type: Jar) {
dependsOn commonJar dependsOn commonJar
mustRunAfter commonJar mustRunAfter commonJar
from configurations.clientsCompile.collect { zipTree it }
from sourceSets.clients.output
version = '1.0.0'
archiveName = "microservice-clients.jar"
destinationDir = file("build/clients")
} }
// app
task appJar(type: Jar) {
dependsOn commonJar
mustRunAfter commonJar
from configurations.appCompile.collect { zipTree it }
from sourceSets.app.output
version = version // '2.3.0'
archiveName = "microservice-app.jar"
destinationDir = file("build/app")
}
assemble {
dependsOn commonJar
dependsOn clientsJar
dependsOn servicePubsubJar
dependsOn appJar
}
//jar {
// dependsOn servicePubsubJar
// mustRunAfter servicePubsubJar
//}
// from configurations.compile.collect { zipTree it } // from configurations.compile.collect { zipTree it }
// from sourceSets.main.output // from sourceSets.main.output
// //version = '2.2.0' // //version = '2.2.0'
...@@ -116,10 +179,10 @@ publishing { ...@@ -116,10 +179,10 @@ publishing {
} }
} }
mavenJava(MavenPublication) { // mavenJava(MavenPublication) {
artifactId 'microservice' // artifactId 'microservice'
from components.java // from components.java
} // }
// client(MavenPublication) { // client(MavenPublication) {
...@@ -164,5 +227,37 @@ publishing { ...@@ -164,5 +227,37 @@ publishing {
artifact commonJar artifact commonJar
} }
clients(MavenPublication) {
artifactId 'microservice-clients'
version clientsJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
}
artifact clientsJar
}
servicePubsub(MavenPublication) {
artifactId 'microservice-service-pubsub'
version servicePubsubJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
}
artifact servicePubsubJar
} }
app(MavenPublication) {
artifactId 'microservice-app'
version appJar.version
// adding dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
}
artifact appJar
}
}
} }
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
+- counters and metrics in the reactor +- counters and metrics in the reactor
+- validateRequest in RestService , the jwt issues +- validateRequest in RestService , the jwt issues
+- Add monitoring apis +- Add monitoring apis
+- All the validation ,pre/post handling that was were done by the handler will be done by the base service +- All the validation ,pre/post handling that was were done by the handler will be done by the base microservice.service
- add Runtime Test: - add Runtime Test:
addTest(const char *testName, nsMicroservice_Iface::TestFunction testFunction); addTest(const char *testName, nsMicroservice_Iface::TestFunction testFunction);
addTest(nsMicroservice_Iface::ITest *p_testClass) addTest(nsMicroservice_Iface::ITest *p_testClass)
+- Add true async in http rest service, that we can send response after handleRequest ends. +- Add true async in http rest microservice.service, that we can send response after handleRequest ends.
// exchange.dispatch(() -> { // exchange.dispatch(() -> {
// new Timer().schedule(new TimerTask() { // new Timer().schedule(new TimerTask() {
// public void run() { // public void run() {
......
package microservice; package microservice;
import common.microservice.io.iface.*; import common.microservice.io.iface.*;
import common.microservice.io.impl.ILoggerConsoleImpl;
import io.undertow.Handlers; import io.undertow.Handlers;
import io.undertow.Undertow; import io.undertow.Undertow;
import io.undertow.Undertow.Builder; import io.undertow.Undertow.Builder;
...@@ -9,8 +10,8 @@ import io.undertow.server.handlers.PathHandler; ...@@ -9,8 +10,8 @@ import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.resource.ClassPathResourceManager; import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.util.MimeMappings; import io.undertow.util.MimeMappings;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import microservice.params.RMQClientParams; import common.microservice.params.RMQClientParams;
import microservice.params.RestServerParams; import common.microservice.params.RestServerParams;
import microservice.handlers.*; import microservice.handlers.*;
import microservice.io.iface.IRestServer; import microservice.io.iface.IRestServer;
import microservice.io.impl.*; import microservice.io.impl.*;
...@@ -42,7 +43,7 @@ import static io.undertow.Handlers.resource; ...@@ -42,7 +43,7 @@ import static io.undertow.Handlers.resource;
* @author amir * @author amir
* *
*/ */
public class MicroserviceApp public class MicroserviceApp implements IApp
{ {
private static MicroserviceApp sInstance = null; private static MicroserviceApp sInstance = null;
...@@ -630,7 +631,7 @@ public class MicroserviceApp ...@@ -630,7 +631,7 @@ public class MicroserviceApp
.filter(p -> p != null) .filter(p -> p != null)
.forEach(servicesMap -> .forEach(servicesMap ->
servicesMap.forEach((serviceKey, service) -> { servicesMap.forEach((serviceKey, service) -> {
service.init(); service.init(this);
if (enableMetrics) if (enableMetrics)
service.withMetricsFactory(IMetricsFactoryImpl.getInstance()); service.withMetricsFactory(IMetricsFactoryImpl.getInstance());
service.register(serviceDiscovery, id); service.register(serviceDiscovery, id);
...@@ -691,14 +692,14 @@ public class MicroserviceApp ...@@ -691,14 +692,14 @@ public class MicroserviceApp
} }
/** // /**
* logging utils // * logging utils
*/ // */
public void logRcid(String from, String rcid){ // public void logRcid(String from, String rcid){
getLogger().info(from + " RCID: " + rcid); // getLogger().info(from + " RCID: " + rcid);
} // }
//
public void logMcid(String from, String mcid){ // public void logMcid(String from, String mcid){
getLogger().info(from + " MCID: " + mcid); // getLogger().info(from + " MCID: " + mcid);
} // }
} }
package microservice; package microservice;
import clients.microservice.impl.IRestClientHttpImpl;
import common.microservice.defs.Constants; import common.microservice.defs.Constants;
import common.CacheClient; import common.CacheClient;
import common.JsonHandler; import common.JsonHandler;
...@@ -10,8 +11,7 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -10,8 +11,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import microservice.io.impl.IRestClientHttpImpl; import common.microservice.params.BaseClientParams;
import microservice.params.BaseClientParams;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
/** /**
......
...@@ -11,6 +11,8 @@ import common.microservice.io.iface.IMetricsFactory; ...@@ -11,6 +11,8 @@ import common.microservice.io.iface.IMetricsFactory;
import java.util.*; import java.util.*;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static common.microservice.services.CommonServices.buildServiceKey;
/** /**
* ------------ * ------------
* Services -> | | -> Methods * Services -> | | -> Methods
...@@ -86,20 +88,20 @@ public class Reactor implements CommonServices.IServiceReactor { ...@@ -86,20 +88,20 @@ public class Reactor implements CommonServices.IServiceReactor {
methodKeyList.add(key); methodKeyList.add(key);
} }
public static String buildServiceKey(CommonServices.MethodParams methodParams) { // public static String buildServiceKey(CommonServices.MethodParams methodParams) {
return Constants.SLASH_SEPERATOR + // return Constants.SLASH_SEPERATOR +
methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR + // methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR + // methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getResourceUri(); // methodParams.getResourceUri();
} // }
public static String buildServiceKey(Enums.EnumServiceType enumServiceType, // public static String buildServiceKey(Enums.EnumServiceType enumServiceType,
CommonServices.IServiceCommands serviceCommands, // CommonServices.IServiceCommands serviceCommands,
String resourceUri) { // String resourceUri) {
return new StringBuilder(32).append(Constants.SLASH_SEPERATOR).append(enumServiceType.name()).append(Constants.TYPE_PREFIX_SEPERATOR) // return new StringBuilder(32).append(Constants.SLASH_SEPERATOR).append(enumServiceType.name()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(serviceCommands.toString()).append(Constants.TYPE_PREFIX_SEPERATOR) // .append(serviceCommands.toString()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(resourceUri).toString(); // .append(resourceUri).toString();
} // }
/** /**
* delegating the msg/request from the service to the method if any * delegating the msg/request from the service to the method if any
......
...@@ -167,7 +167,7 @@ public class IMetricsFactoryImpl implements IMetricsFactory ...@@ -167,7 +167,7 @@ public class IMetricsFactoryImpl implements IMetricsFactory
.convertDurationsTo(TimeUnit.MILLISECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL) .filter(MetricFilter.ALL)
.skipIdleMetrics(true) .skipIdleMetrics(true)
.tag("service", MicroserviceApp.getsInstance().getAppName()) .tag("microservice/service/microservice/microservice", MicroserviceApp.getsInstance().getAppName())
.tag("instance", MicroserviceApp.getsInstance().getId()) .tag("instance", MicroserviceApp.getsInstance().getId())
.tag("server", MicroserviceApp.getsInstance().getServerName()) .tag("server", MicroserviceApp.getsInstance().getServerName())
.transformer(new CategoriesMetricMeasurementTransformer("module", "artifact")) .transformer(new CategoriesMetricMeasurementTransformer("module", "artifact"))
......
...@@ -4,13 +4,14 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -4,13 +4,14 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsPoller; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsPoller;
import common.microservice.io.iface.ILogger;
import http.simpleRestClient.SimpleRestResponse; import http.simpleRestClient.SimpleRestResponse;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import common.microservice.io.iface.IServiceDiscovery; import common.microservice.io.iface.IServiceDiscovery;
import microservice.params.BaseClientParams; import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import microservice.params.RMQClientParams; import common.microservice.params.RMQClientParams;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import rabbitmq.client.RMQRestClient; import rabbitmq.client.RMQRestClient;
import rabbitmq.common.RMQId; import rabbitmq.common.RMQId;
...@@ -28,6 +29,7 @@ import java.util.function.Consumer; ...@@ -28,6 +29,7 @@ import java.util.function.Consumer;
public class IRMQClientRestImpl implements IRestClient public class IRMQClientRestImpl implements IRestClient
{ {
private static int REQUEST_TIMEOUT = 30000; private static int REQUEST_TIMEOUT = 30000;
private ILogger logger;
/*********************************************************************************************/ /*********************************************************************************************/
/* JSON LISTENER /* JSON LISTENER
...@@ -112,6 +114,11 @@ public class IRMQClientRestImpl implements IRestClient ...@@ -112,6 +114,11 @@ public class IRMQClientRestImpl implements IRestClient
} }
@Override @Override
public void init(ILogger logger) {
this.logger = logger;
}
@Override
public BaseRestResponse create(CommandParams reqCtx) public BaseRestResponse create(CommandParams reqCtx)
{ {
BaseRestResponse brr = null; BaseRestResponse brr = null;
......
...@@ -13,7 +13,7 @@ import microservice.handlers.MBIHandler; ...@@ -13,7 +13,7 @@ import microservice.handlers.MBIHandler;
import common.microservice.io.iface.IPubSub; import common.microservice.io.iface.IPubSub;
import microservice.io.iface.IRestServer; import microservice.io.iface.IRestServer;
import common.microservice.io.iface.IServiceDiscovery; import common.microservice.io.iface.IServiceDiscovery;
import microservice.params.RMQClientParams; import common.microservice.params.RMQClientParams;
public class IRestServerRMQImpl implements IRestServer { public class IRestServerRMQImpl implements IRestServer {
......
...@@ -19,7 +19,7 @@ import microservice.handlers.RestHandler; ...@@ -19,7 +19,7 @@ import microservice.handlers.RestHandler;
import common.microservice.io.iface.IPubSub; import common.microservice.io.iface.IPubSub;
import microservice.io.iface.IRestServer; import microservice.io.iface.IRestServer;
import common.microservice.io.iface.IServiceDiscovery; import common.microservice.io.iface.IServiceDiscovery;
import microservice.params.RestServerParams; import common.microservice.params.RestServerParams;
import static io.undertow.Handlers.resource; import static io.undertow.Handlers.resource;
public class IRestServerUndertowImpl implements IRestServer { public class IRestServerUndertowImpl implements IRestServer {
......
package microservice.services; package microservice.service.pubsub.impl;
import common.microservice.io.iface.IApp;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import common.microservice.io.iface.IServiceDiscovery; import common.microservice.io.iface.IServiceDiscovery;
import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.*;
...@@ -46,7 +47,7 @@ public class IPubSubServiceMqttImpl extends CommonServices.IPubSubService { ...@@ -46,7 +47,7 @@ public class IPubSubServiceMqttImpl extends CommonServices.IPubSubService {
} }
@Override @Override
public boolean init() { public boolean init(IApp app) {
try { try {
......
package microservice.services; package microservice.service.pubsub.impl;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -8,6 +8,7 @@ import common.microservice.services.CommonServices; ...@@ -8,6 +8,7 @@ import common.microservice.services.CommonServices;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import common.microservice.types.UserProfile; import common.microservice.types.UserProfile;
import common.microservice.utils.IDGenerator; import common.microservice.utils.IDGenerator;
import common.microservice.utils.Logging;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.ExpiredJwtException;
import io.undertow.Handlers; import io.undertow.Handlers;
...@@ -26,16 +27,16 @@ import common.microservice.security.EncryptionUtils; ...@@ -26,16 +27,16 @@ import common.microservice.security.EncryptionUtils;
import common.microservice.defs.Constants; import common.microservice.defs.Constants;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import microservice.handlers.BaseHandler; import microservice.handlers.BaseHandler;
import microservice.handlers.Reactor;
import microservice.io.impl.IRequestRestImpl; import microservice.io.impl.IRequestRestImpl;
import microservice.io.impl.IResponseRestImpl; import microservice.io.impl.IResponseRestImpl;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import microservice.params.RestServerParams; import common.microservice.params.RestServerParams;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import static common.microservice.services.CommonServices.buildServiceKey;
import static io.undertow.Handlers.resource; import static io.undertow.Handlers.resource;
import static common.microservice.defs.Constants.*; import static common.microservice.defs.Constants.*;
...@@ -48,7 +49,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -48,7 +49,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
RestServerParams restServerParams = null; RestServerParams restServerParams = null;
Undertow restServer = null; Undertow restServer = null;
Thread restThread = null; Thread restThread = null;
MicroserviceApp msAppInstance = null;
private String appName; private String appName;
public ObjectMapper objMapper = null; public ObjectMapper objMapper = null;
protected Enums.EnumAuthenticationType authType = Enums.EnumAuthenticationType.DEFAULT; protected Enums.EnumAuthenticationType authType = Enums.EnumAuthenticationType.DEFAULT;
...@@ -80,7 +80,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -80,7 +80,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
try { try {
if (restClient != null) { if (restClient != null) {
if (cmdParams.getRcid() != null) // log rcid if (cmdParams.getRcid() != null) // log rcid
msAppInstance.logRcid("handleSyncRespCommand." + name,cmdParams.getRcid()); Logging.logRcid(logger,"handleSyncRespCommand." + name,cmdParams.getRcid());
resp = command.get(); resp = command.get();
}; };
} catch (Exception e) { } catch (Exception e) {
...@@ -96,7 +96,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -96,7 +96,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
boolean retstat; boolean retstat;
try { try {
if (cmdParams.getRcid() != null) // log rcid if (cmdParams.getRcid() != null) // log rcid
msAppInstance.logRcid("handleAsyncRespCommand." + name,cmdParams.getRcid()); Logging.logRcid(logger,"handleAsyncRespCommand." + name,cmdParams.getRcid());
retstat = command.getAsBoolean(); retstat = command.getAsBoolean();
} catch (Exception e) { } catch (Exception e) {
retstat = false; retstat = false;
...@@ -145,19 +145,21 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -145,19 +145,21 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
} }
@Override @Override
public boolean init() { public boolean init(IApp app) {
boolean stat = true; boolean stat = true;
if (MicroserviceApp.getsInstance() != null) { this.app = app;
msAppInstance = MicroserviceApp.getsInstance(); if (app != null) {
logger = msAppInstance.getLogger(); logger = app.getLogger();
this.appName = msAppInstance.getAppName(); this.appName = app.getAppName();
} }
switch (getServiceMode()){ switch (getServiceMode()){
case E_CLIENT: case E_CLIENT:
initRestClient();
break; break;
case E_CLIENT_SERVER: case E_CLIENT_SERVER:
case E_SERVER: case E_SERVER:
initRestClient();
stat = buildRestServer(); stat = buildRestServer();
break; break;
default: default:
...@@ -168,6 +170,12 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -168,6 +170,12 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
return stat; return stat;
} }
private void initRestClient() {
if (restClient != null) {
restClient.init(logger);
}
}
private boolean buildRestServer() { private boolean buildRestServer() {
String host = this.restServerParams.getHost(); String host = this.restServerParams.getHost();
if (host == null || Network.LOCALHOST.equals(host)) if (host == null || Network.LOCALHOST.equals(host))
...@@ -235,7 +243,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -235,7 +243,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
/** /**
* finally we can delegate * finally we can delegate
*/ */
String key = Reactor.buildServiceKey(Enums.EnumServiceType.E_REST,reqContext.enumRestCommands,reqContext.request.getRelativePath()); String key = buildServiceKey(Enums.EnumServiceType.E_REST,reqContext.enumRestCommands,reqContext.request.getRelativePath());
reactor.delegate(this, key ,reqContext); reactor.delegate(this, key ,reqContext);
} }
} else { } else {
...@@ -289,7 +297,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -289,7 +297,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
if (reqCtx.rcid == null) // create a new one if (reqCtx.rcid == null) // create a new one
reqCtx.rcid = IDGenerator.createUUID(); reqCtx.rcid = IDGenerator.createUUID();
else // log it else // log it
msAppInstance.logRcid("getRequestContext",reqCtx.rcid); Logging.logRcid(logger,"getRequestContext",reqCtx.rcid);
return reqCtx; return reqCtx;
} }
...@@ -345,7 +353,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -345,7 +353,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
apiDocMimeMappingsBuilder.addMapping("yaml", "text/x-yaml"); apiDocMimeMappingsBuilder.addMapping("yaml", "text/x-yaml");
apiDocMimeMappingsBuilder.addMapping("html", "text/html"); apiDocMimeMappingsBuilder.addMapping("html", "text/html");
pathHandler.addPrefixPath("/static", resource(new ClassPathResourceManager(getClass().getClassLoader(),getClass().getPackage()))//"static")) pathHandler.addPrefixPath("/static", resource(new ClassPathResourceManager(getClass().getClassLoader(),getClass().getPackage()))//"static"))
.addWelcomeFiles("microservice/services/metrics.html") .addWelcomeFiles("microservice/service/pubsub/metrics.html")
.setMimeMappings(apiDocMimeMappingsBuilder.build()) .setMimeMappings(apiDocMimeMappingsBuilder.build())
.setDirectoryListingEnabled(false)); .setDirectoryListingEnabled(false));
} }
...@@ -429,7 +437,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -429,7 +437,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
} }
else else
{ {
msAppInstance.getLogger().error(NO_TOKEN_FOR_REQUEST); logger.error(NO_TOKEN_FOR_REQUEST);
sendErrorResp(restContext.response,NO_TOKEN_FOR_REQUEST); sendErrorResp(restContext.response,NO_TOKEN_FOR_REQUEST);
valid = false; valid = false;
} }
......
package microservice.services; package microservice.service.pubsub.impl;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -8,17 +8,15 @@ import common.microservice.io.iface.*; ...@@ -8,17 +8,15 @@ import common.microservice.io.iface.*;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import common.microservice.utils.IDGenerator; import common.microservice.utils.IDGenerator;
import microservice.MicroserviceApp;
import common.microservice.context.CrudMethod; import common.microservice.context.CrudMethod;
import common.microservice.context.RestContext; import common.microservice.context.RestContext;
import common.microservice.context.RestMsg; import common.microservice.context.RestMsg;
import common.microservice.context.RestResponse; import common.microservice.context.RestResponse;
import common.microservice.defs.Constants; import common.microservice.defs.Constants;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import microservice.handlers.Reactor;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import microservice.params.ZMQParams; import common.microservice.params.ZMQParams;
import microservice.services.protocol.zmq.RestImpl; import microservice.service.pubsub.impl.protocol.zmq.RestImpl;
import common.microservice.utils.ICacheClientGuavaImpl; import common.microservice.utils.ICacheClientGuavaImpl;
import microservice.utils.ZSocketPool; import microservice.utils.ZSocketPool;
import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang.SerializationUtils;
...@@ -35,6 +33,7 @@ import java.util.stream.IntStream; ...@@ -35,6 +33,7 @@ import java.util.stream.IntStream;
import static common.microservice.defs.Constants.EXIT_MSG; import static common.microservice.defs.Constants.EXIT_MSG;
import static common.microservice.defs.Constants.EXIT_MSG_LEN; import static common.microservice.defs.Constants.EXIT_MSG_LEN;
import static common.microservice.services.CommonServices.buildServiceKey;
/** /**
* Created by amir on 14/05/17. * Created by amir on 14/05/17.
...@@ -154,15 +153,15 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -154,15 +153,15 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
private ILogger logger= null; private ILogger logger= null;
private CommonServices.ICacheClient<Long,CacheEntry> responseCacheClient = null; private CommonServices.ICacheClient<Long,CacheEntry> responseCacheClient = null;
public ClientWorker(int workerNumber, CommonServices.ICacheClient<Long, CacheEntry> responseCacheClient) { public ClientWorker(ILogger logger,int workerNumber, CommonServices.ICacheClient<Long, CacheEntry> responseCacheClient) {
this.workerNumber = workerNumber; this.workerNumber = workerNumber;
this.responseCacheClient = responseCacheClient; this.responseCacheClient = responseCacheClient;
bindAddress = ADDRESS + String.valueOf(workerNumber); bindAddress = ADDRESS + String.valueOf(workerNumber);
this.logger = logger;
} }
@Override @Override
public boolean init() { public boolean init() {
logger = MicroserviceApp.getsInstance().getLogger();
pull = new ZSocket(ZMQ.PULL); pull = new ZSocket(ZMQ.PULL);
return pull.bind(bindAddress); return pull.bind(bindAddress);
} }
...@@ -287,10 +286,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -287,10 +286,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
private ZMQParams.ServerParams zmqParams; private ZMQParams.ServerParams zmqParams;
private ZSocket pull = null; private ZSocket pull = null;
private ZSocket push = null; private ZSocket push = null;
private ILogger logger;
private int numOfServerWorkers; private int numOfServerWorkers;
public ServerReceive(ZMQParams.ServerParams zmqParams, int numOfServerWorkers) { public ServerReceive(ILogger logger,ZMQParams.ServerParams zmqParams, int numOfServerWorkers) {
this.zmqParams = zmqParams; this.zmqParams = zmqParams;
this.numOfServerWorkers = numOfServerWorkers; this.numOfServerWorkers = numOfServerWorkers;
this.logger = logger;
} }
...@@ -329,7 +330,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -329,7 +330,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
} }
}); });
runThread.start(); runThread.start();
MicroserviceApp.getsInstance().getLogger().info("ZMQ server started successfully on host: " + zmqParams.getHost() + ", and port: " + String.valueOf(zmqParams.getPort())); logger.info("ZMQ server started successfully on host: " + zmqParams.getHost() + ", and port: " + String.valueOf(zmqParams.getPort()));
} }
@Override @Override
...@@ -352,15 +353,16 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -352,15 +353,16 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
private CommonServices.IServiceReactor reactor = null; private CommonServices.IServiceReactor reactor = null;
private CommonServices.IRestService parentService = null; private CommonServices.IRestService parentService = null;
public ObjectMapper objMapper = null; public ObjectMapper objMapper = null;
private ILogger logger= null; private ILogger logger = null;
private ZSocketPool serverSendPool = null; private ZSocketPool serverSendPool = null;
private int workerNumber = 0; private int workerNumber = 0;
private String bindAddress = null; private String bindAddress = null;
public ServerWorker(CommonServices.IServiceReactor reactor, public ServerWorker(ILogger logger, CommonServices.IServiceReactor reactor,
CommonServices.IRestService parentService, CommonServices.IRestService parentService,
ZSocketPool serverSendPool, ZSocketPool serverSendPool,
int workerNumber) { int workerNumber) {
this.logger = logger;
this.reactor = reactor; this.reactor = reactor;
this.parentService = parentService; this.parentService = parentService;
this.serverSendPool = serverSendPool; this.serverSendPool = serverSendPool;
...@@ -371,7 +373,6 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -371,7 +373,6 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
@Override @Override
public boolean init() { public boolean init() {
logger = MicroserviceApp.getsInstance().getLogger();
pull = new ZSocket(ZMQ.PULL); pull = new ZSocket(ZMQ.PULL);
objMapper = new ObjectMapper(); objMapper = new ObjectMapper();
return pull.bind(bindAddress); return pull.bind(bindAddress);
...@@ -393,7 +394,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -393,7 +394,7 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
if (receiveMsg != null){ if (receiveMsg != null){
RestContext restContext = getRequestContext(receiveMsg); RestContext restContext = getRequestContext(receiveMsg);
if (restContext != null){ if (restContext != null){
String key = Reactor.buildServiceKey(Enums.EnumServiceType.E_REST, String key = buildServiceKey(Enums.EnumServiceType.E_REST,
restContext.enumRestCommands, restContext.enumRestCommands,
restContext.request.getRelativePath()); restContext.request.getRelativePath());
reactor.delegate(parentService, key ,restContext); reactor.delegate(parentService, key ,restContext);
...@@ -547,9 +548,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -547,9 +548,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
ByteBuffer respBB = ByteBuffer.allocate(CAPACITY); ByteBuffer respBB = ByteBuffer.allocate(CAPACITY);
FlatBufferBuilder respBuilder = new FlatBufferBuilder(); FlatBufferBuilder respBuilder = new FlatBufferBuilder();
public ServerReply(ILogger logger) {
this.logger = logger;
}
@Override @Override
public boolean init() { public boolean init() {
logger = MicroserviceApp.getsInstance().getLogger();
pull = new ZSocket(ZMQ.PULL); pull = new ZSocket(ZMQ.PULL);
return pull.bind(ADDRESS); return pull.bind(ADDRESS);
} }
...@@ -822,11 +826,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -822,11 +826,12 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
} }
@Override @Override
public boolean init() { public boolean init(IApp app) {
boolean retstat = true; boolean retstat = true;
if (MicroserviceApp.getsInstance() != null) { this.app = app;
this.appName = MicroserviceApp.getsInstance().getAppName(); if (app != null) {
this.logger = MicroserviceApp.getsInstance().getLogger(); this.appName = app.getAppName();
this.logger = app.getLogger();
} }
switch (getServiceMode()){ switch (getServiceMode()){
...@@ -853,24 +858,24 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements ...@@ -853,24 +858,24 @@ public class IRestServiceZmqImpl extends CommonServices.IRestService implements
public boolean allocateServer() { public boolean allocateServer() {
allWorkersList.add(new ServerReply()); allWorkersList.add(new ServerReply(logger));
/** /**
* init server send pool * init server send pool
*/ */
serverSendPool = ZSocketPool.buildPool(ServerReply.ADDRESS,ZMQ.PUSH,numOfServerWorkers); serverSendPool = ZSocketPool.buildPool(ServerReply.ADDRESS,ZMQ.PUSH,numOfServerWorkers);
for (int i = 0; i < numOfServerWorkers; i++){ for (int i = 0; i < numOfServerWorkers; i++){
allWorkersList.add(new ServerWorker(reactor,this,serverSendPool,i)); allWorkersList.add(new ServerWorker(logger,reactor,this,serverSendPool,i));
} }
// must be after the workers // must be after the workers
allWorkersList.add(new ServerReceive(serverParams,numOfServerWorkers)); allWorkersList.add(new ServerReceive(logger,serverParams,numOfServerWorkers));
return serverSendPool != null; return serverSendPool != null;
} }
public boolean allocateClient() { public boolean allocateClient() {
responseCacheClient = new ICacheClientGuavaImpl<>(EXPIRES_MILLI_SECONDS); responseCacheClient = new ICacheClientGuavaImpl<>(EXPIRES_MILLI_SECONDS);
for (int i = 0; i < numOfClientWorkers; i++){ for (int i = 0; i < numOfClientWorkers; i++){
allWorkersList.add(new ClientWorker(i,responseCacheClient)); allWorkersList.add(new ClientWorker(logger,i,responseCacheClient));
} }
// must be after workers // must be after workers
allWorkersList.add(new ClientReceive(clientReceiveParams,numOfClientWorkers)); allWorkersList.add(new ClientReceive(clientReceiveParams,numOfClientWorkers));
......
package microservice.services.protocol.zmq; package microservice.service.pubsub.impl.protocol.zmq;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.flatbuffers.FlatBufferBuilder; import com.google.flatbuffers.FlatBufferBuilder;
import common.microservice.io.iface.IApp;
import common.microservice.utils.IDGenerator; import common.microservice.utils.IDGenerator;
import microservice.MicroserviceApp;
import common.microservice.context.ParamValue; import common.microservice.context.ParamValue;
import common.microservice.context.QueueMsg; import common.microservice.context.QueueMsg;
import common.microservice.defs.Constants; import common.microservice.defs.Constants;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import microservice.handlers.Reactor;
import common.microservice.io.iface.ILogger; import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IServiceDiscovery; import common.microservice.io.iface.IServiceDiscovery;
import microservice.params.ZMQParams; import common.microservice.params.ZMQParams;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import microservice.utils.ZSocketPool; import microservice.utils.ZSocketPool;
import org.zeromq.ZMQ; import org.zeromq.ZMQ;
...@@ -24,6 +23,8 @@ import java.util.List; ...@@ -24,6 +23,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import static common.microservice.services.CommonServices.buildServiceKey;
/** /**
* MsgQueue implementation in ZMQ * MsgQueue implementation in ZMQ
*/ */
...@@ -56,7 +57,7 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService { ...@@ -56,7 +57,7 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService {
QueueMsg queueMsg = QueueMsg.getRootAsQueueMsg(bb); QueueMsg queueMsg = QueueMsg.getRootAsQueueMsg(bb);
if (queueMsg != null){ if (queueMsg != null){
MsgQContext msgQContext = getMsgQContext(queueMsg); MsgQContext msgQContext = getMsgQContext(queueMsg);
String key = Reactor.buildServiceKey(Enums.EnumServiceType.E_MSGQ, String key = buildServiceKey(Enums.EnumServiceType.E_MSGQ,
CommonServices.EnumMsgQueueCommands.E_QUEUE, CommonServices.EnumMsgQueueCommands.E_QUEUE,
msgQContext.topic); msgQContext.topic);
reactor.delegate(parentService, key ,msgQContext); reactor.delegate(parentService, key ,msgQContext);
...@@ -82,11 +83,12 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService { ...@@ -82,11 +83,12 @@ public class IMsgQServiceZmqImpl extends CommonServices.IMsgQService {
@Override @Override
public boolean init() { public boolean init(IApp app) {
boolean retstat = true; boolean retstat = true;
if (MicroserviceApp.getsInstance() != null) { this.app = app;
this.appName = MicroserviceApp.getsInstance().getAppName(); if (app != null) {
this.logger = MicroserviceApp.getsInstance().getLogger(); this.appName = app.getAppName();
this.logger = app.getLogger();
} }
if (getServiceMode() != null) { if (getServiceMode() != null) {
......
package microservice.services.protocol.zmq; package microservice.service.pubsub.impl.protocol.zmq;
import common.microservice.context.RestMsg; import common.microservice.context.RestMsg;
import common.microservice.io.iface.IRequest; import common.microservice.io.iface.IRequest;
import common.microservice.io.iface.IResponse; import common.microservice.io.iface.IResponse;
import microservice.services.IRestServiceZmqImpl; import microservice.service.pubsub.impl.IRestServiceZmqImpl;
import microservice.utils.ZSocketPool; import microservice.utils.ZSocketPool;
import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang.SerializationUtils;
import org.zeromq.ZMQ; import org.zeromq.ZMQ;
......
package microservice.services.protocol.zmq; package microservice.service.pubsub.impl.protocol.zmq;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
...@@ -6,7 +6,7 @@ import common.microservice.utils.IDGenerator; ...@@ -6,7 +6,7 @@ import common.microservice.utils.IDGenerator;
import microservice.MicroserviceApp; import microservice.MicroserviceApp;
import common.microservice.context.RestContext; import common.microservice.context.RestContext;
import common.microservice.io.iface.ILogger; import common.microservice.io.iface.ILogger;
import microservice.params.ZMQParams; import common.microservice.params.ZMQParams;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import org.zeromq.ZMQ; import org.zeromq.ZMQ;
import org.zeromq.ZSocket; import org.zeromq.ZSocket;
......
package microservice.utils; package microservice.utils;
import clients.microservice.impl.IRestClientHttpImpl;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import microservice.io.impl.IRestClientHttpImpl; import common.microservice.params.PulsarParams;
import microservice.params.PulsarParams; import microservice.service.pubsub.impl.IRestServiceHttpImpl;
import microservice.services.IPubSubServicePulsarImpl; import microservice.service.pubsub.impl.IRestServiceZmqImpl;
import microservice.services.IRestServiceHttpImpl; import common.microservice.params.RestClientParams;
import microservice.services.IRestServiceZmqImpl; import common.microservice.params.RestServerParams;
import microservice.params.RestClientParams; import common.microservice.params.ZMQParams;
import microservice.params.RestServerParams; import microservice.service.pubsub.impl.protocol.zmq.IMsgQServiceZmqImpl;
import microservice.params.ZMQParams; import service.microservice.IPubSubServicePulsarImpl;
import microservice.services.protocol.zmq.IMsgQServiceZmqImpl;
/** /**
* Created by amir on 09/05/17. * Created by amir on 09/05/17.
......
package microservice.io.impl; package clients.microservice.impl;
import clients.microservice.utils.RestHttpClient;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand; import com.netflix.hystrix.HystrixObservableCommand;
import com.netflix.hystrix.contrib.codahalemetricspublisher.HystrixCodaHaleMetricsPublisher;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import microservice.MicroserviceApp;
import common.microservice.defs.Constants; import common.microservice.defs.Constants;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import common.microservice.io.iface.ILogger;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import common.microservice.io.iface.IServiceDiscovery; import common.microservice.io.iface.IServiceDiscovery;
import microservice.params.BaseClientParams; import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import microservice.params.RestClientParams; import common.microservice.params.RestClientParams;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import microservice.utils.RestHttpClient;
import rx.Observable; import rx.Observable;
import rx.Subscriber; import rx.Subscriber;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import static common.microservice.utils.Logging.logRcid;
public class IRestClientHttpImpl implements IRestClient public class IRestClientHttpImpl implements IRestClient
{ {
public static final String HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION = "hystrix.plugin.HystrixMetricsPublisher.implementation"; public static final String HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION = "hystrix.plugin.HystrixMetricsPublisher.implementation";
private static final int POLLING_DELAY = 500; private static final int POLLING_DELAY = 500;
private ILogger logger = null;
/************************************************************************* /*************************************************************************
* COMMANDS * COMMANDS
...@@ -213,8 +213,6 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -213,8 +213,6 @@ public class IRestClientHttpImpl implements IRestClient
private static final String COMMAND_ERROR = "Command Error: "; private static final String COMMAND_ERROR = "Command Error: ";
RestClientParams clientParams = null; RestClientParams clientParams = null;
RestHttpClient httpRestClient = null; RestHttpClient httpRestClient = null;
// HystrixMetricsPoller poller = null;
// MetricJsonListener jsonListener = null;
Optional<IServiceDiscovery> serviceDiscovery = Optional.empty(); Optional<IServiceDiscovery> serviceDiscovery = Optional.empty();
private final ObjectMapper objMapper = new ObjectMapper(); private final ObjectMapper objMapper = new ObjectMapper();
...@@ -237,29 +235,26 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -237,29 +235,26 @@ public class IRestClientHttpImpl implements IRestClient
if (clientParams.isMetricsEnabled()) if (clientParams.isMetricsEnabled())
{ {
initMetricsPublisher(); // initMetricsPublisher();
// jsonListener = new MetricJsonListener();
// poller = new HystrixMetricsPoller(jsonListener, POLLING_DELAY);
// poller.start();
} }
} }
private synchronized void initMetricsPublisher() { // private synchronized void initMetricsPublisher() {
String publisherStr = System.getProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION); // String publisherStr = System.getProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION);
if(publisherStr == null) { // if(publisherStr == null) {
try { // try {
IMetricsFactoryImpl factoryImpl = (IMetricsFactoryImpl) IMetricsFactoryImpl.getInstance(); // IMetricsFactoryImpl factoryImpl = (IMetricsFactoryImpl) IMetricsFactoryImpl.getInstance();
HystrixMetricsPublisher publisher = new HystrixCodaHaleMetricsPublisher(factoryImpl.getMetrics()); // HystrixMetricsPublisher publisher = new HystrixCodaHaleMetricsPublisher(factoryImpl.getMetrics());
HystrixPlugins.getInstance().registerMetricsPublisher(publisher); // HystrixPlugins.getInstance().registerMetricsPublisher(publisher);
System.setProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION, publisher.getClass().getName()); // System.setProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION, publisher.getClass().getName());
} // }
catch (IllegalStateException ise){ // catch (IllegalStateException ise){
//
} // }
} // }
} // }
public IRestClientHttpImpl withServiceDiscovery(IServiceDiscovery servDisco) public IRestClientHttpImpl withServiceDiscovery(IServiceDiscovery servDisco)
{ {
...@@ -320,6 +315,12 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -320,6 +315,12 @@ public class IRestClientHttpImpl implements IRestClient
// } // }
@Override @Override
public void init(ILogger logger) {
this.logger = logger;
httpRestClient.setLogger(logger);
}
@Override
public BaseRestResponse create(CommandParams reqCtx) public BaseRestResponse create(CommandParams reqCtx)
{ {
BaseRestResponse brr = null; BaseRestResponse brr = null;
...@@ -413,7 +414,7 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -413,7 +414,7 @@ public class IRestClientHttpImpl implements IRestClient
if (cbFunc != null) { if (cbFunc != null) {
try { try {
if (reqCtx.getRcid() != null) if (reqCtx.getRcid() != null)
MicroserviceApp.getsInstance().logRcid("asyncCreate",reqCtx.getRcid()); logRcid(logger,"asyncCreate",reqCtx.getRcid());
resolveService(reqCtx); resolveService(reqCtx);
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_POST.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr)); retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_POST.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
...@@ -421,11 +422,11 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -421,11 +422,11 @@ public class IRestClientHttpImpl implements IRestClient
// (brr) -> cbFunc.accept(brr), // (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse // (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) { } catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncCreate: " + e.toString()); logger.error(this.getClass().getName() + " >> Exception in asyncCreate: " + e.toString());
retstat = false; retstat = false;
} }
} else { } else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncCreate"); logger.error(this.getClass().getName() + " >> null cbFunc in asyncCreate");
retstat = false; retstat = false;
} }
return retstat; return retstat;
...@@ -443,11 +444,11 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -443,11 +444,11 @@ public class IRestClientHttpImpl implements IRestClient
// (brr) -> cbFunc.accept(brr), // (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse // (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) { } catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncRead: " + e.toString()); logger.error(this.getClass().getName() + " >> Exception in asyncRead: " + e.toString());
retstat = false; retstat = false;
} }
} else { } else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncRead"); logger.error(this.getClass().getName() + " >> null cbFunc in asyncRead");
retstat = false; retstat = false;
} }
return retstat; return retstat;
...@@ -459,7 +460,7 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -459,7 +460,7 @@ public class IRestClientHttpImpl implements IRestClient
if (cbFunc != null) { if (cbFunc != null) {
try { try {
if (reqCtx.getRcid() != null) if (reqCtx.getRcid() != null)
MicroserviceApp.getsInstance().logRcid("asyncUpdate",reqCtx.getRcid()); logRcid(logger,"asyncUpdate",reqCtx.getRcid());
resolveService(); resolveService();
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_PUT.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr)); retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_PUT.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
...@@ -467,11 +468,11 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -467,11 +468,11 @@ public class IRestClientHttpImpl implements IRestClient
// (brr) -> cbFunc.accept(brr), // (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse // (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) { } catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncUpdate: " + e.toString()); logger.error(this.getClass().getName() + " >> Exception in asyncUpdate: " + e.toString());
retstat = false; retstat = false;
} }
} else { } else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncUpdate"); logger.error(this.getClass().getName() + " >> null cbFunc in asyncUpdate");
retstat = false; retstat = false;
} }
return retstat; return retstat;
...@@ -483,7 +484,7 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -483,7 +484,7 @@ public class IRestClientHttpImpl implements IRestClient
if (cbFunc != null) { if (cbFunc != null) {
try { try {
if (reqCtx.getRcid() != null) if (reqCtx.getRcid() != null)
MicroserviceApp.getsInstance().logRcid("asyncDelete",reqCtx.getRcid()); logRcid(logger,"asyncDelete",reqCtx.getRcid());
resolveService(); resolveService();
retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_DELETE.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr)); retstat = httpRestClient.executeAsync(Enums.EnumHttpMethod.E_DELETE.getStrMethod(),reqCtx, brr -> cbFunc.accept(brr));
...@@ -491,11 +492,11 @@ public class IRestClientHttpImpl implements IRestClient ...@@ -491,11 +492,11 @@ public class IRestClientHttpImpl implements IRestClient
// (brr) -> cbFunc.accept(brr), // (brr) -> cbFunc.accept(brr),
// (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse // (err) -> cbFunc.accept(new BaseRestResponse(false,err.toString()))); // create error BaseRestResponse
} catch (Exception e) { } catch (Exception e) {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> Exception in asyncDelete: " + e.toString()); logger.error(this.getClass().getName() + " >> Exception in asyncDelete: " + e.toString());
retstat = false; retstat = false;
} }
} else { } else {
MicroserviceApp.getsInstance().getLogger().error(this.getClass().getName() + " >> null cbFunc in asyncDelete"); logger.error(this.getClass().getName() + " >> null cbFunc in asyncDelete");
retstat = false; retstat = false;
} }
return retstat; return retstat;
......
package common.microservice.io.iface;
public interface IApp {
ILogger getLogger();
String getAppName();
String getServerName();
String getId();
IConfiguration getConfiguration();
}
...@@ -12,7 +12,7 @@ import java.util.function.Consumer; ...@@ -12,7 +12,7 @@ import java.util.function.Consumer;
public interface IRestClient public interface IRestClient
{ {
static final String COMMAND_ERROR = "Command Error: "; static final String COMMAND_ERROR = "Command Error: ";
void init(ILogger logger);
public abstract class Command extends HystrixCommand<BaseRestResponse> { public abstract class Command extends HystrixCommand<BaseRestResponse> {
protected CommandParams reqCtx = null; protected CommandParams reqCtx = null;
......
package microservice.io.impl; package common.microservice.io.impl;
import common.microservice.io.iface.ILogger; import common.microservice.io.iface.ILogger;
......
package microservice.params; package common.microservice.params;
public class BaseClientParams public class BaseClientParams
{ {
......
package microservice.params; package common.microservice.params;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
......
package microservice.params; package common.microservice.params;
public class RMQClientParams extends BaseClientParams public class RMQClientParams extends BaseClientParams
{ {
......
package microservice.params; package common.microservice.params;
public class RestClientParams extends BaseClientParams public class RestClientParams extends BaseClientParams
{ {
......
package microservice.params; package common.microservice.params;
import java.util.Optional; import java.util.Optional;
......
package microservice.params; package common.microservice.params;
import jdk.nashorn.internal.ir.EmptyNode; //import jdk.nashorn.internal.ir.EmptyNode;
/** /**
* Created by amir on 14/05/17. * Created by amir on 14/05/17.
......
...@@ -3,10 +3,7 @@ package common.microservice.services; ...@@ -3,10 +3,7 @@ package common.microservice.services;
import common.microservice.context.CrudMethod; import common.microservice.context.CrudMethod;
import common.microservice.defs.Constants; import common.microservice.defs.Constants;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import common.microservice.io.iface.IMetricsFactory; import common.microservice.io.iface.*;
import common.microservice.io.iface.IRequest;
import common.microservice.io.iface.IResponse;
import common.microservice.io.iface.IServiceDiscovery;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
...@@ -35,8 +32,8 @@ public class CommonServices { ...@@ -35,8 +32,8 @@ public class CommonServices {
public static abstract class IService { public static abstract class IService {
protected IServiceReactor reactor = null; protected IServiceReactor reactor = null;
protected IMetricsFactory metricsFactory = null; protected IMetricsFactory metricsFactory = null;
protected IApp app = null;
public abstract boolean init(); public abstract boolean init(IApp app);
public abstract void run(); public abstract void run();
public abstract void shutdown(); public abstract void shutdown();
public abstract void handleNotImplmented(IMsgContext msgContext); public abstract void handleNotImplmented(IMsgContext msgContext);
...@@ -339,4 +336,21 @@ public class CommonServices { ...@@ -339,4 +336,21 @@ public class CommonServices {
public abstract void stop() throws InterruptedException; public abstract void stop() throws InterruptedException;
} }
public static String buildServiceKey(CommonServices.MethodParams methodParams) {
return Constants.SLASH_SEPERATOR +
methodParams.getServiceType().name() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getServiceCommand().toString() + Constants.TYPE_PREFIX_SEPERATOR +
methodParams.getResourceUri();
}
public static String buildServiceKey(Enums.EnumServiceType enumServiceType,
CommonServices.IServiceCommands serviceCommands,
String resourceUri) {
return new StringBuilder(32).append(Constants.SLASH_SEPERATOR).append(enumServiceType.name()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(serviceCommands.toString()).append(Constants.TYPE_PREFIX_SEPERATOR)
.append(resourceUri).toString();
}
} }
package common.microservice.utils;
import common.microservice.io.iface.ILogger;
public class Logging {
/**
* logging utils
*/
public static void logRcid(ILogger logger,String from, String rcid){
logger.info(from + " RCID: " + rcid);
}
public static void logMcid(ILogger logger,String from, String mcid){
logger.info(from + " MCID: " + mcid);
}
}
...@@ -4,10 +4,10 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -4,10 +4,10 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import microservice.io.impl.IRMQClientRestImpl; import microservice.io.impl.IRMQClientRestImpl;
import microservice.params.BaseClientParams; import common.microservice.params.BaseClientParams;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import common.microservice.params.CommandParamsBuilder; import common.microservice.params.CommandParamsBuilder;
import microservice.params.RMQClientParams; import common.microservice.params.RMQClientParams;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import org.junit.Test; import org.junit.Test;
import rx.Observable; import rx.Observable;
......
...@@ -3,7 +3,7 @@ package microservice; ...@@ -3,7 +3,7 @@ package microservice;
import common.microservice.io.iface.ILogger; import common.microservice.io.iface.ILogger;
import microservice.io.impl.ILoggerConsoleImpl; import common.microservice.io.impl.ILoggerConsoleImpl;
import org.junit.Test; import org.junit.Test;
import java.time.LocalDateTime; import java.time.LocalDateTime;
......
...@@ -25,7 +25,7 @@ import org.junit.Test; ...@@ -25,7 +25,7 @@ import org.junit.Test;
import microservice.MicroserviceClient.EnumRestClientType; import microservice.MicroserviceClient.EnumRestClientType;
import common.microservice.params.CommandParams; import common.microservice.params.CommandParams;
import microservice.params.RestClientParams; import common.microservice.params.RestClientParams;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
......
...@@ -7,14 +7,13 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; ...@@ -7,14 +7,13 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import common.microservice.context.RestContext; import common.microservice.context.RestContext;
import common.microservice.defs.Enums; import common.microservice.defs.Enums;
import common.microservice.params.CommandParams; import common.microservice.params.*;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import common.microservice.io.iface.IRestClient; import common.microservice.io.iface.IRestClient;
import microservice.io.impl.*; import microservice.io.impl.*;
import microservice.services.IRestServiceHttpImpl; import services.impl.IRestServiceHttpImpl;
import microservice.params.*;
import microservice.services.IRestServiceZmqImpl; import services.impl.IRestServiceZmqImpl;
import common.microservice.types.BaseRestResponse; import common.microservice.types.BaseRestResponse;
import microservice.utils.ServiceBuilderFactory; import microservice.utils.ServiceBuilderFactory;
import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttException;
......
...@@ -14,7 +14,7 @@ import com.google.common.cache.CacheBuilder; ...@@ -14,7 +14,7 @@ import com.google.common.cache.CacheBuilder;
import common.JsonHandler; import common.JsonHandler;
import io.undertow.predicate.Predicate; import io.undertow.predicate.Predicate;
import io.undertow.util.PathMatcher; import io.undertow.util.PathMatcher;
import microservice.params.ZMQParams; import common.microservice.params.ZMQParams;
import common.microservice.services.CommonServices; import common.microservice.services.CommonServices;
import microservice.utils.ServiceBuilderFactory; import microservice.utils.ServiceBuilderFactory;
......
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