Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b3cff686
authored
Oct 23, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add /_mon/_apiList to get all rest api's and add influxdb metrics
parent
1c73cca9
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
130 additions
and
15 deletions
README.md
build.gradle
src/main/java/microservice/MicroserviceApp.java
src/main/java/microservice/handlers/MonitorHandler.java
src/main/java/microservice/handlers/Reactor.java
src/main/java/microservice/io/impl/IMetricsFactoryImpl.java
src/test/java/microservice/TestMicroserviceApp.java
README.md
View file @
b3cff686
### Microservice Framework in JAVA
## 2.0.1 add /_mon/_apiList to get all rest api's and add influxdb metrics
-
Add env params:
-
"influxdb.hostport": influxdb server ( default 'null' > no server)
-
"influx.report.intrerval": the report interval in seconds ( default 10 )
-
"influxdb.user": influxdb user ( default "root")
-
"influxdb.pass": influxdb pass( default "giptmgrr")
-
"influxdb.dbname": db name ( default "mcx_db");
## 2.0.0 merge the new microservices design in develop branch
## 1.4.x: New microservices design:
-
Truly async handling for rest commands receive and send
...
...
build.gradle
View file @
b3cff686
group
'com.ipgallery.common'
version
'2.0.
0
'
version
'2.0.
1
'
apply
plugin:
'java'
apply
plugin:
'maven-publish'
...
...
@@ -13,7 +13,7 @@ repositories {
//use mavenLocal in cases you want to create this jar on your local machine
//or to be able to use one
//mavenLocal()//defaults to ~/.m2/repository
maven
{
url
"http
://172.16.1.132
:8081/repository/internal"
}
maven
{
url
"http
s://municipalitybank.com
:8081/repository/internal"
}
}
...
...
@@ -29,8 +29,8 @@ dependencies {
compile
'com.ipgallery.common:utils:1.2.5'
compile
(
'com.ipgallery.common:rabbitmq:1.0.3'
)
compile
'com.ecwid.consul:consul-api:1.1.9'
compile
'com.github.davidb:metrics-influxdb:0.
8.2
'
compile
'io.dropwizard.metrics:metrics-graphite:3.
1.2
'
compile
'com.github.davidb:metrics-influxdb:0.
9.3
'
compile
'io.dropwizard.metrics:metrics-graphite:3.
2.5
'
compile
'io.jsonwebtoken:jjwt:0.6.0'
compile
group:
'org.zeromq'
,
name:
'jeromq'
,
version:
'0.4.0'
compile
'org.elasticsearch.client:rest:5.4.1'
...
...
@@ -58,12 +58,20 @@ publishing {
publications
{
repositories
.
maven
{
url
'http
://172.16.1.132
:8081/repository/internal'
url
'http
s://municipalitybank.com
:8081/repository/internal'
credentials
{
username
"a
dmin
"
password
"giptmgr
1
"
username
"a
mir.aharon@ipgallery.com
"
password
"giptmgr
r
"
}
}
// repositories.maven {
// url 'http://172.16.1.132:8081/repository/internal'
// credentials {
// username "admin"
// password "giptmgr1"
// }
// }
mavenJava
(
MavenPublication
)
{
//artifactId 'group-service'
from
components
.
java
...
...
src/main/java/microservice/MicroserviceApp.java
View file @
b3cff686
...
...
@@ -21,6 +21,8 @@ import rabbitmq.common.RMQId;
import
rabbitmq.server.RMQHandler
;
import
rabbitmq.server.RMQServer
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
import
java.util.*
;
import
java.util.Map.Entry
;
import
java.util.function.BiConsumer
;
...
...
@@ -48,6 +50,7 @@ public class MicroserviceApp
RMQClientParams
mbiParams
=
null
;
String
appName
;
String
id
;
String
serverName
;
Map
<
String
,
BaseHandler
>
msMap
=
null
;
// the server containers - rest and MQ
Undertow
restServer
=
null
;
...
...
@@ -68,14 +71,29 @@ public class MicroserviceApp
/////////////// new design microservice ///////////////////
Reactor
reactor
;
public
MicroserviceApp
(
String
appName
)
{
this
.
appName
=
appName
;
id
=
String
.
valueOf
(
System
.
currentTimeMillis
()
&
0xffff
);
reactor
=
new
Reactor
();
serverName
=
getMachineName
();
sInstance
=
this
;
}
private
String
getMachineName
()
{
String
machineName
=
""
;
InetAddress
localMachine
=
null
;
try
{
localMachine
=
InetAddress
.
getLocalHost
();
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
}
if
(
localMachine
!=
null
)
machineName
=
localMachine
.
getHostName
();
return
machineName
;
}
public
MicroserviceApp
(
RestServerParams
rsiParams
,
RMQClientParams
mbiParams
,
String
appName
)
{
...
...
@@ -106,6 +124,10 @@ public class MicroserviceApp
return
id
;
}
public
String
getServerName
()
{
return
serverName
;
}
public
boolean
isEnableDefaultServiceAuthorization
()
{
return
enableDefaultServiceAuthorization
;
}
public
IServiceDiscovery
getServiceDiscovery
()
{
return
serviceDiscovery
;
}
...
...
@@ -158,9 +180,8 @@ public class MicroserviceApp
}
public
MicroserviceApp
withMonitoring
()
{
// optMonitorHandler = Optional.of(new MonitorHandler());
// this.addHandler(MON_PREFIX, optMonitorHandler.get());
addMethodClass
(
new
MonitorHandler
());
optMonitorHandler
=
Optional
.
of
(
new
MonitorHandler
());
addMethodClass
(
optMonitorHandler
.
get
());
return
this
;
}
...
...
@@ -583,7 +604,7 @@ public class MicroserviceApp
logger
=
new
ILoggerConsoleImpl
(
appName
);
//new ILogger4jImpl(appName); // default logger
// some java 8 now...
optMonitorHandler
.
ifPresent
(
mon
->
m
sMap
.
forEach
((
prfx
,
handler
)
->
{
mon
.
addHandler
(
handler
);}
));
optMonitorHandler
.
ifPresent
(
mon
->
m
on
.
setReactor
(
reactor
));
/*
* checking configuration
...
...
src/main/java/microservice/handlers/MonitorHandler.java
View file @
b3cff686
...
...
@@ -2,6 +2,7 @@ package microservice.handlers;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.SortedMap
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
...
...
@@ -12,9 +13,11 @@ import com.codahale.metrics.Meter;
import
com.codahale.metrics.Timer
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ArrayNode
;
import
com.fasterxml.jackson.databind.node.JsonNodeFactory
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
microservice.MicroserviceApp
;
import
microservice.defs.Constants
;
import
microservice.defs.Enums
;
import
microservice.io.iface.ILogger
;
import
microservice.services.CommonServices
;
...
...
@@ -36,19 +39,20 @@ public class MonitorHandler implements CommonServices.IMethodClass
public
static
final
String
LOG_LEVEL
=
"_logLevel"
;
public
static
final
String
FIELD_NAME
=
"name"
;
public
static
final
String
LEVEL
=
"Level"
;
List
<
BaseHandler
>
containers
=
null
;
protected
ObjectMapper
objMapper
=
new
ObjectMapper
();
public
static
final
String
API_LIST
=
"_apiList"
;
protected
ObjectMapper
objMapper
=
new
ObjectMapper
();
private
Reactor
reactor
=
null
;
public
MonitorHandler
()
{
super
();
containers
=
new
LinkedList
<
BaseHandler
>();
}
public
void
addHandler
(
BaseHandler
handler
)
{
containers
.
add
(
handler
);
}
// @Override
...
...
@@ -199,9 +203,35 @@ public class MonitorHandler implements CommonServices.IMethodClass
(
msgCtx
,
orgService
)
->
{
setLogLevel
((
RestContext
)
msgCtx
);
}));
methodParamsList
.
add
(
new
CommonServices
.
MethodParams
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/"
+
MON_PREFIX
+
"/"
+
API_LIST
,
(
msgCtx
,
orgService
)
->
{
printApiList
((
RestContext
)
msgCtx
);
}));
}
/**
* what do YOU think it is!
* @param reqCtx
*/
private
void
printApiList
(
RestContext
reqCtx
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
ArrayNode
arrayNode
=
JsonNodeFactory
.
instance
.
arrayNode
();
if
(
reactor
!=
null
){
final
Map
<
String
,
List
<
String
>>
listMap
=
reactor
.
getMethodKeyList
().
stream
()
.
collect
(
Collectors
.
groupingBy
(
key
->
key
.
split
(
Constants
.
TYPE_PREFIX_SEPERATOR
)[
2
]));
listMap
.
entrySet
().
forEach
(
stringListEntry
->
{
final
List
<
String
>
methodList
=
stringListEntry
.
getValue
().
stream
().
map
(
api
->
api
.
split
(
Constants
.
TYPE_PREFIX_SEPERATOR
)[
1
]).
collect
(
Collectors
.
toList
());
arrayNode
.
add
(
JsonNodeFactory
.
instance
.
objectNode
().
put
(
stringListEntry
.
getKey
(),
methodList
.
toString
()));
});
}
brr
.
objectNode
=
arrayNode
;
reqCtx
.
container
.
writeObjectToResponse
(
reqCtx
.
response
,
brr
);
}
private
void
setLogLevel
(
RestContext
reqCtx
)
{
final
JsonNode
jsonNode
=
(
JsonNode
)
reqCtx
.
container
.
readObjectFromRequest
(
reqCtx
.
request
,
JsonNode
.
class
);
if
(
jsonNode
!=
null
){
...
...
@@ -220,4 +250,7 @@ public class MonitorHandler implements CommonServices.IMethodClass
}
public
void
setReactor
(
Reactor
reactor
)
{
this
.
reactor
=
reactor
;
}
}
src/main/java/microservice/handlers/Reactor.java
View file @
b3cff686
...
...
@@ -184,4 +184,8 @@ public class Reactor implements CommonServices.IServiceReactor {
}
}
}
public
List
<
String
>
getMethodKeyList
()
{
return
methodKeyList
;
}
}
src/main/java/microservice/io/impl/IMetricsFactoryImpl.java
View file @
b3cff686
...
...
@@ -5,6 +5,9 @@ import com.codahale.metrics.Timer.Context;
import
com.codahale.metrics.graphite.GraphiteReporter
;
import
com.codahale.metrics.graphite.PickledGraphite
;
import
metrics_influxdb.HttpInfluxdbProtocol
;
import
metrics_influxdb.InfluxdbReporter
;
import
metrics_influxdb.api.measurements.CategoriesMetricMeasurementTransformer
;
import
microservice.MicroserviceApp
;
import
microservice.io.iface.IMetricsFactory
;
...
...
@@ -138,6 +141,42 @@ public class IMetricsFactoryImpl implements IMetricsFactory
@Override
public
void
startReporting
(
String
appName
)
{
startInfluxDbReporting
();
//startGraphiteReporting(appName);
}
private
void
startInfluxDbReporting
()
{
String
influxdb_hostport
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getString
(
"influxdb.hostport"
,
null
);
Long
interval
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getLong
(
"influx.report.intrerval"
,
Long
.
valueOf
(
10
));
String
influxdb_user
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getString
(
"influxdb.user"
,
"root"
);
String
influxdb_pass
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getString
(
"influxdb.pass"
,
"giptmgrr"
);
String
influxdb_dbname
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getString
(
"influxdb.dbname"
,
"mcx_db"
);
if
(
influxdb_hostport
!=
null
)
{
int
port
=
8086
;
String
host
=
influxdb_hostport
;
int
index
=
influxdb_hostport
.
indexOf
(
':'
);
if
(
index
!=
-
1
){
port
=
Integer
.
valueOf
(
influxdb_hostport
.
substring
(
index
+
1
));
host
=
influxdb_hostport
.
substring
(
0
,
index
);
}
final
ScheduledReporter
reporter
=
InfluxdbReporter
.
forRegistry
(
metrics
)
.
protocol
(
new
HttpInfluxdbProtocol
(
"http"
,
host
,
port
,
influxdb_user
,
influxdb_pass
,
influxdb_dbname
))
.
convertRatesTo
(
TimeUnit
.
SECONDS
)
.
convertDurationsTo
(
TimeUnit
.
MILLISECONDS
)
.
filter
(
MetricFilter
.
ALL
)
.
skipIdleMetrics
(
true
)
.
tag
(
"service"
,
MicroserviceApp
.
getsInstance
().
getAppName
())
.
tag
(
"instance"
,
MicroserviceApp
.
getsInstance
().
getId
())
.
tag
(
"server"
,
MicroserviceApp
.
getsInstance
().
getServerName
())
.
transformer
(
new
CategoriesMetricMeasurementTransformer
(
"module"
,
"artifact"
))
.
build
();
reporter
.
start
(
interval
,
TimeUnit
.
SECONDS
);
MicroserviceApp
.
getsInstance
().
getLogger
().
info
(
"Reporting started..."
);
}
}
private
void
startGraphiteReporting
(
String
appName
)
{
String
graphite_hostport
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getString
(
"graphite_hostport"
,
null
);
Long
interval
=
MicroserviceApp
.
getsInstance
().
getConfiguration
().
getLong
(
"graphite_report_intrerval"
,
Long
.
valueOf
(
10
));
if
(
graphite_hostport
!=
null
)
{
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
b3cff686
...
...
@@ -80,6 +80,8 @@ public class TestMicroserviceApp {
public
void
testMicroServices
()
{
System
.
setProperty
(
"configFile.location"
,
"/opt/mcx/config/config.properties"
);
System
.
setProperty
(
"influxdb.hostport"
,
"172.16.1.244:8086"
);
String
appName
=
"testApp"
;
CommonServices
.
IService
httpRestService
=
ServiceBuilderFactory
.
createRestServiceHttpBuilder
(
CommonServices
.
EnumRestServiceMode
.
E_CLIENT_SERVER
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment