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
6cef7404
authored
Jun 08, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add prefix in method path, add interval query in metrics.html
parent
e193235b
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
120 additions
and
35 deletions
build.gradle
doc/ServicesDesign.md
doc/todo.txt
src/main/java/microservice/MicroserviceApp.java
src/main/java/microservice/defs/Constants.java
src/main/java/microservice/handlers/Reactor.java
src/main/java/microservice/services/IRestServiceHttpImpl.java
src/main/resources/microservice/services/metrics.html
src/test/java/microservice/TestMicroClient.java
src/test/java/microservice/TestMicroserviceApp.java
src/test/java/microservice/TestServicesAndMethods.java
build.gradle
View file @
6cef7404
group
'com.ipgallery.common'
version
'2.0.
0
-services'
version
'2.0.
2
-services'
apply
plugin:
'java'
apply
plugin:
'maven-publish'
...
...
doc/ServicesDesign.md
View file @
6cef7404
...
...
@@ -40,7 +40,7 @@
### Metrics
To see the Metrics go to /_mon/_stat to get in json
To see the Metrics go to /_mon/_stat
or /_mon/_stat?viewType=list
to get in json
to see in html go to /static/metrics.html
### ZMQRestService
![
alt text
](
ZMQRestService.png
)
doc/todo.txt
View file @
6cef7404
...
...
@@ -24,4 +24,19 @@
- add static page for monitor metrics that calls http://localhost:32000/_mon/_stat?viewType=list
- periodically via jquery and display the metrics in a table
- Add zipkin to trace and monitor requests across microservices
\ No newline at end of file
- Add zipkin to trace and monitor requests across microservices
- Check Elasticsearch RestClient as the http rest client
- extend the HystrixObservableCommand for the async http client:
public class ObservableHttpCommand extends HystrixObsverableCommand<BackendResponse> {
@Override
protected Observable<BackendResponse> construct() {
return httpClient.submit(HttpClientRequest.createGet("/mock.json?numItems=" + numItems))
.flatMap((HttpClientResponse<ByteBuf> r) -> r.getContent()
.map(b -> BackendResponse.fromJson(new ByteBufInputStream(b))));
}
@Override
protected Observable<BackendResponse> resumeWithFallback() {
return Observable.just(new BackendResponse(0, numItems, new String[] {}));
}}
\ No newline at end of file
src/main/java/microservice/MicroserviceApp.java
View file @
6cef7404
...
...
@@ -543,14 +543,14 @@ public class MicroserviceApp
return
this
;
}
CommonServices
.
IService
getService
(
Enums
.
EnumServiceType
serviceType
,
String
serviceKey
){
public
CommonServices
.
IService
getService
(
Enums
.
EnumServiceType
serviceType
,
String
serviceKey
){
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
[
serviceType
.
ordinal
()];
if
(
serviceMap
!=
null
)
return
serviceMap
.
get
(
serviceKey
);
return
null
;
}
CommonServices
.
IService
getService
(
String
serviceKey
){
public
CommonServices
.
IService
getService
(
String
serviceKey
){
Optional
<
CommonServices
.
IService
>
iServiceOptional
=
Arrays
.
stream
(
servicesArray
)
.
filter
(
serviceMap
->
serviceMap
!=
null
&&
serviceMap
.
containsKey
(
serviceKey
))
.
map
(
serviceMap
->
serviceMap
.
get
(
serviceKey
))
...
...
src/main/java/microservice/defs/Constants.java
View file @
6cef7404
...
...
@@ -17,6 +17,7 @@ public class Constants
public
static
final
String
INVALID_REQUEST_TOKEN
=
"invalid request/token"
;
public
static
final
String
AUTHORIZATION_HEADER
=
"Authorization"
;
public
static
final
String
TYPE_PREFIX_SEPERATOR
=
":"
;
public
static
final
String
SLASH_SEPERATOR
=
"/"
;
public
static
final
String
EXIT_MSG
=
"exit"
;
public
static
final
int
EXIT_MSG_LEN
=
EXIT_MSG
.
length
();
public
static
final
int
STRING_INITIAL_CAPACITY
=
64
;
...
...
src/main/java/microservice/handlers/Reactor.java
View file @
6cef7404
package
microservice
.
handlers
;
import
io.undertow.util.PathMatcher
;
import
io.undertow.util.PathTemplateMatcher
;
import
microservice.defs.Constants
;
import
microservice.defs.Enums
;
...
...
@@ -57,10 +58,15 @@ public class Reactor implements CommonServices.IServiceReactor {
}
}
/// the methods hashmap
private
final
PathTemplateMatcher
<
MethodMatch
>
methodsMap
=
new
PathTemplateMatcher
<>();
/**
* the methods hashmaps, we use 2 one for exact match i.e like swagger api
* the other is for prefix/wildcard use and we're using it when the first one fails
*/
///
private
final
PathTemplateMatcher
<
MethodMatch
>
exactMethodsMap
=
new
PathTemplateMatcher
<>();
private
final
PathMatcher
<
MethodMatch
>
prefixMethodsMap
=
new
PathMatcher
<>();
private
final
List
<
String
>
methodKeyList
=
new
ArrayList
<>();
//private Map<String,BiConsumer<CommonServices.IMsgContext,CommonServices.IService>> methodsMap = new HashMap<>();
private
IMetricsFactory
metricsFactory
=
null
;
/// the metrics map
private
Map
<
String
,
MethodMetrics
>
methodMetricsMap
=
null
;
...
...
@@ -74,20 +80,23 @@ public class Reactor implements CommonServices.IServiceReactor {
* build the key
*/
String
key
=
buildServiceKey
(
methodParams
);
methodsMap
.
add
(
key
,
new
MethodMatch
(
methodParams
.
getConsumerMethod
()));
final
MethodMatch
methodMatch
=
new
MethodMatch
(
methodParams
.
getConsumerMethod
());
exactMethodsMap
.
add
(
key
,
methodMatch
);
prefixMethodsMap
.
addPrefixPath
(
key
,
methodMatch
);
methodKeyList
.
add
(
key
);
}
public
static
String
buildServiceKey
(
CommonServices
.
MethodParams
methodParams
)
{
return
methodParams
.
getServiceType
().
name
()
+
Constants
.
TYPE_PREFIX_SEPERATOR
+
methodParams
.
getServiceCommand
().
toString
()
+
Constants
.
TYPE_PREFIX_SEPERATOR
+
methodParams
.
getResourceUri
();
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
(
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
(
resourceUri
).
toString
();
}
...
...
@@ -103,16 +112,34 @@ public class Reactor implements CommonServices.IServiceReactor {
public
boolean
delegate
(
CommonServices
.
IService
orgService
,
String
key
,
CommonServices
.
IMsgContext
msgContext
){
MethodMetrics
methodMetrics
=
null
;
boolean
result
=
true
;
final
PathTemplateMatcher
.
PathMatchResult
<
MethodMatch
>
match
=
methodsMap
.
match
(
key
);
MethodMatch
methodMatch
=
null
;
String
matchedTemplate
=
null
;
Map
<
String
,
String
>
exactMatchParameters
=
null
;
final
PathTemplateMatcher
.
PathMatchResult
<
MethodMatch
>
exactMatch
=
exactMethodsMap
.
match
(
key
);
if
(
exactMatch
!=
null
)
{
methodMatch
=
exactMatch
.
getValue
();
matchedTemplate
=
exactMatch
.
getMatchedTemplate
();
exactMatchParameters
=
exactMatch
.
getParameters
();
}
else
{
/**
* checking for prefix, key must start with '/'
*/
final
PathMatcher
.
PathMatch
<
MethodMatch
>
prefixMatch
=
prefixMethodsMap
.
match
(
key
);
if
(
prefixMatch
!=
null
)
{
methodMatch
=
prefixMatch
.
getValue
();
matchedTemplate
=
key
.
substring
(
0
,
key
.
length
()
-
prefixMatch
.
getRemaining
().
length
());
}
}
try
{
if
(
match
!=
null
&&
match
.
getValue
().
getMethodCallback
()
!=
null
)
{
msgContext
.
setParameters
(
match
.
getParameters
());
final
BiConsumer
<
CommonServices
.
IMsgContext
,
CommonServices
.
IService
>
methodCallback
=
match
.
getValue
().
getMethodCallback
();
if
(
methodMatch
!=
null
&&
methodMatch
.
getMethodCallback
()
!=
null
)
{
if
(
exactMatchParameters
!=
null
)
msgContext
.
setParameters
(
exactMatchParameters
);
final
BiConsumer
<
CommonServices
.
IMsgContext
,
CommonServices
.
IService
>
methodCallback
=
methodMatch
.
getMethodCallback
();
/**
* pre-handling
*/
if
(
metricsFactory
!=
null
)
{
methodMetrics
=
methodMetricsMap
.
get
(
match
.
getMatchedTemplate
()
);
if
(
metricsFactory
!=
null
&&
matchedTemplate
!=
null
)
{
methodMetrics
=
methodMetricsMap
.
get
(
match
edTemplate
);
if
(
methodMetrics
!=
null
)
methodMetrics
.
preHandle
();
}
...
...
@@ -134,8 +161,8 @@ public class Reactor implements CommonServices.IServiceReactor {
return
result
;
}
public
final
PathTemplateMatcher
<
MethodMatch
>
getMethodsMap
()
{
return
m
ethodsMap
;
public
final
PathTemplateMatcher
<
MethodMatch
>
get
Exact
MethodsMap
()
{
return
exactM
ethodsMap
;
}
public
Reactor
withMetricsFactory
(
IMetricsFactory
metricsFactory
)
{
...
...
src/main/java/microservice/services/IRestServiceHttpImpl.java
View file @
6cef7404
...
...
@@ -138,8 +138,25 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
@Override
public
boolean
init
()
{
boolean
stat
=
true
;
logger
=
MicroserviceApp
.
getsInstance
().
getLogger
();
this
.
appName
=
MicroserviceApp
.
getsInstance
().
getAppName
();
switch
(
getServiceMode
()){
case
E_CLIENT:
break
;
case
E_CLIENT_SERVER:
case
E_SERVER:
stat
=
buildRestServer
();
break
;
default
:
stat
=
false
;
logger
.
error
(
getClass
().
getName
()
+
" >> no service mode!!!"
);
break
;
}
return
stat
;
}
private
boolean
buildRestServer
()
{
String
host
=
this
.
restServerParams
.
getHost
();
if
(
host
==
null
||
Network
.
LOCALHOST
.
equals
(
host
))
host
=
Network
.
getLocalIpAddress
();
...
...
@@ -167,8 +184,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
serverBuilder
.
setHandler
(
pathHandler
);
// build
this
.
restServer
=
serverBuilder
.
build
();
return
true
;
return
(
this
.
restServer
!=
null
);
}
...
...
src/main/resources/microservice/services/metrics.html
View file @
6cef7404
...
...
@@ -7,13 +7,16 @@
<script>
// Builds the HTML Table out of myList.
function
buildHtmlTable
(
data
)
{
var
selector
=
'#meterTable'
;
$
(
selector
).
empty
()
var
list
=
data
.
objectNode
.
meters
;
var
columns
=
addAllColumnHeaders
(
list
,
selector
);
addRows
(
selector
,
columns
,
list
);
selector
=
'#timerTable'
;
$
(
selector
).
empty
()
list
=
data
.
objectNode
.
timers
;
columns
=
addAllColumnHeaders
(
list
,
selector
);
...
...
@@ -38,6 +41,7 @@ function addRows(selector,columns,myList){
// Need to do union of keys from all records as some records may not contain
// all records.
function addAllColumnHeaders(myList, selector) {
var columnSet = [];
var headerTr$ = $('
<
tr
/>
'
);
...
...
@@ -56,18 +60,32 @@ function addAllColumnHeaders(myList, selector) {
}
$
(
document
).
ready
(
function
(){
$
.
get
(
"http://localhost:32000/_mon/_stat?viewType=list"
,
function
(
data
,
status
){
/* $.get("http://localhost:32000/_mon/_stat?viewType=list", function(data, status){
buildHtmlTable(data);
});
*/
(
function
poll
()
{
$
.
ajax
({
url
:
"/_mon/_stat?viewType=list"
,
type
:
"GET"
,
success
:
function
(
data
)
{
buildHtmlTable
(
data
);
},
dataType
:
"json"
,
complete
:
setTimeout
(
function
()
{
poll
()},
5000
),
timeout
:
2000
})
})();
});
</script>
</head>
<body>
<table
id=
"meterTable"
border=
"1
"
>
<table
id=
"meterTable"
class=
"highlight striped bordered
"
>
</table>
<table
id=
"timerTable"
border=
"1"
>
<table
class=
"striped"
id=
"timerTable"
border=
"1"
>
</table>
</body>
...
...
src/test/java/microservice/TestMicroClient.java
View file @
6cef7404
...
...
@@ -24,7 +24,6 @@ public class TestMicroClient
MicroserviceClient
client
=
null
;
BaseRestResponse
brr
=
null
;
RestClientParams
clientParams
=
new
RestClientParams
(
"ds"
,
true
,
0
,
"172.16.1.244:8080"
,
null
,
100
);
final
IServiceDiscoveryConsulImpl
serDisco
=
new
IServiceDiscoveryConsulImpl
(
"localhost"
,
8500
);
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
6cef7404
...
...
@@ -123,6 +123,12 @@ public class TestMicroserviceApp {
(
msgCtx
,
orgService
)
->
{
resourceRid
((
RestContext
)
msgCtx
);
}));
methodParamsList
.
add
(
new
CommonServices
.
MethodParams
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/resource/r1"
,
(
msgCtx
,
orgService
)
->
{
resourceRid
((
RestContext
)
msgCtx
);
}));
methodParamsList
.
add
(
new
CommonServices
.
MethodParams
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_CREATE
,
"/resource/{rid}"
,
...
...
src/test/java/microservice/TestServicesAndMethods.java
View file @
6cef7404
...
...
@@ -12,6 +12,7 @@ import com.google.common.cache.Cache;
import
com.google.common.cache.CacheBuilder
;
import
common.JsonHandler
;
import
io.undertow.predicate.Predicate
;
import
io.undertow.util.PathMatcher
;
import
io.undertow.util.PathTemplateMatcher
;
import
microservice.services.CommonServices
;
import
microservice.services.IRestServiceZmqImpl
;
...
...
@@ -56,8 +57,8 @@ public class TestServicesAndMethods {
@Test
public
void
testMatcher
(){
final
PathTemplateMatcher
<
RoutingMatch
>
allMethodsMatcher
=
new
PathTemplateMatcher
<>();
//
final PathTemplateMatcher<RoutingMatch> allMethodsMatcher = new PathTemplateMatcher<>();
PathMatcher
<
RoutingMatch
>
allMethodsMatcher
=
new
PathMatcher
<>();
addMatch
(
allMethodsMatcher
,
"/baz/{foo}"
);
addMatch
(
allMethodsMatcher
,
"/baz/ok/{foo}"
);
...
...
@@ -69,8 +70,9 @@ public class TestServicesAndMethods {
findMatch
(
allMethodsMatcher
,
"/baz/ok"
);
findMatch
(
allMethodsMatcher
,
"/baz/entities"
);
findMatch
(
allMethodsMatcher
,
"/baz/entities/entity/"
);
long
start
=
System
.
currentTimeMillis
(
);
findMatch
(
allMethodsMatcher
,
"/baz/entities/entity/a/s/d"
);
long
start
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
ITERATIONS
;
i
++)
{
findMatch
(
allMethodsMatcher
,
"REST:GET:/baz/entities/entity/12345"
);
}
...
...
@@ -78,13 +80,14 @@ public class TestServicesAndMethods {
}
public
void
addMatch
(
Path
Template
Matcher
<
RoutingMatch
>
allMethodsMatcher
,
String
template1
)
{
public
void
addMatch
(
PathMatcher
<
RoutingMatch
>
allMethodsMatcher
,
String
template1
)
{
RoutingMatch
res1
=
new
RoutingMatch
();
allMethodsMatcher
.
add
(
template1
,
res1
);
allMethodsMatcher
.
add
PrefixPath
(
template1
,
res1
);
}
public
void
findMatch
(
PathTemplateMatcher
<
RoutingMatch
>
allMethodsMatcher
,
String
path
)
{
PathTemplateMatcher
.
PathMatchResult
<
RoutingMatch
>
match
=
allMethodsMatcher
.
match
(
path
);
public
void
findMatch
(
PathMatcher
<
RoutingMatch
>
allMethodsMatcher
,
String
path
)
{
//PathTemplateMatcher.PathMatchResult<RoutingMatch> match = allMethodsMatcher.match(path);
final
PathMatcher
.
PathMatch
<
RoutingMatch
>
match
=
allMethodsMatcher
.
match
(
path
);
if
(
match
==
null
)
System
.
out
.
println
(
"Failed to find handler for: "
+
path
);
// else
...
...
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