Commit 710b205d by amir

change rest http client to okhttp and support real async client request, add…

change rest http client to okhttp and support real async client request, add scheme to command-params
parent ca53eff4
...@@ -33,6 +33,10 @@ dependencies { ...@@ -33,6 +33,10 @@ dependencies {
compile 'io.dropwizard.metrics:metrics-graphite:3.1.2' compile 'io.dropwizard.metrics:metrics-graphite:3.1.2'
compile 'io.jsonwebtoken:jjwt:0.6.0' compile 'io.jsonwebtoken:jjwt:0.6.0'
compile group: 'org.zeromq', name: 'jeromq', version: '0.4.0' compile group: 'org.zeromq', name: 'jeromq', version: '0.4.0'
compile 'org.elasticsearch.client:rest:5.4.1'
compile 'com.netflix.rxjava:rxjava-apache-http:0.20.7'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
// compile group: 'org.apache.httpcomponents', name: 'httpasyncclient', version: '4.1.2'
testCompile group: 'junit', name: 'junit', version: '4.11' testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.zeromq', name: 'jeromq', version: '0.4.0' testCompile group: 'org.zeromq', name: 'jeromq', version: '0.4.0'
......
...@@ -44,3 +44,6 @@ ...@@ -44,3 +44,6 @@
to see in html go to /static/metrics.html to see in html go to /static/metrics.html
### ZMQRestService ### ZMQRestService
![alt text](ZMQRestService.png) ![alt text](ZMQRestService.png)
## Env for http client
"http.maxConnections" (default: 50) defines max per route, total is twice this size
...@@ -668,4 +668,10 @@ public class MicroserviceApp ...@@ -668,4 +668,10 @@ public class MicroserviceApp
} }
/**
* logging utils
*/
public void logRcid(String from, String rcid){
getLogger().info(from + " RCID: " + rcid);
}
} }
...@@ -23,4 +23,5 @@ public class Constants ...@@ -23,4 +23,5 @@ public class Constants
public static final int STRING_INITIAL_CAPACITY = 64; public static final int STRING_INITIAL_CAPACITY = 64;
public static final String METER = "Meter:"; public static final String METER = "Meter:";
public static final String TIMER = "Timer:"; public static final String TIMER = "Timer:";
public static final String HTTP_SCHEME = "http";
} }
...@@ -62,6 +62,22 @@ public class Enums ...@@ -62,6 +62,22 @@ public class Enums
} }
return enumCrudMethod; return enumCrudMethod;
} }
public static String resolveHttpMethod(EnumCrudMethod enumCrudMethod){
switch (enumCrudMethod){
case E_CREATE:
return EnumHttpMethod.E_POST.getStrMethod();
case E_READ:
return EnumHttpMethod.E_GET.getStrMethod();
case E_UPDATE:
return EnumHttpMethod.E_PUT.getStrMethod();
case E_DELETE:
return EnumHttpMethod.E_DELETE.getStrMethod();
}
return null;
}
} }
public enum EnumProtocol public enum EnumProtocol
......
...@@ -4,13 +4,15 @@ import java.util.Map; ...@@ -4,13 +4,15 @@ import java.util.Map;
public class CommandParams public class CommandParams
{ {
String entity; String scheme = null;
String entity = null;
String[] params; String[] params;
String paramsString; // params as a continues string "p1/p2/p3" String paramsString; // params as a continues string "p1/p2/p3"
String requestParams; String requestParams;
String content; String content;
Map<String,String> headersMap = null; Map<String,String> headersMap = null;
int cmndId = 0; Map<String,String> requestParamsMap = null;
String rcid = null;
public CommandParams() { public CommandParams() {
} }
...@@ -39,6 +41,15 @@ public class CommandParams ...@@ -39,6 +41,15 @@ public class CommandParams
this.headersMap = headersMap; this.headersMap = headersMap;
} }
public String getScheme() {
return scheme;
}
public CommandParams setScheme(String scheme) {
this.scheme = scheme;
return this;
}
public String getEntity() public String getEntity()
{ {
return entity; return entity;
...@@ -59,6 +70,10 @@ public class CommandParams ...@@ -59,6 +70,10 @@ public class CommandParams
return requestParams; return requestParams;
} }
public Map<String, String> getRequestParamsMap() {
return requestParamsMap;
}
public String getContent() public String getContent()
{ {
return content; return content;
...@@ -88,6 +103,11 @@ public class CommandParams ...@@ -88,6 +103,11 @@ public class CommandParams
return this; return this;
} }
public CommandParams setRequestParamsMap(Map<String, String> requestParamsMap) {
this.requestParamsMap = requestParamsMap;
return this;
}
public CommandParams setContent(String content) { public CommandParams setContent(String content) {
this.content = content; this.content = content;
return this; return this;
...@@ -98,12 +118,12 @@ public class CommandParams ...@@ -98,12 +118,12 @@ public class CommandParams
return this; return this;
} }
public CommandParams setCmndId(int cmndId) { public CommandParams setRcid(String rcid) {
this.cmndId = cmndId; this.rcid = rcid;
return this; return this;
} }
public int getCmndId() { public String getRcid() {
return cmndId; return rcid;
} }
} }
package microservice.params; package microservice.params;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
...@@ -7,6 +8,7 @@ import java.util.Map; ...@@ -7,6 +8,7 @@ import java.util.Map;
*/ */
public class CommandParamsBuilder { public class CommandParamsBuilder {
CommandParams commandParams = new CommandParams(); CommandParams commandParams = new CommandParams();
Map<String,String> requestParamsMap = null;
public CommandParamsBuilder setEntity(String entity) { public CommandParamsBuilder setEntity(String entity) {
commandParams.setEntity(entity); commandParams.setEntity(entity);
...@@ -40,6 +42,20 @@ public class CommandParamsBuilder { ...@@ -40,6 +42,20 @@ public class CommandParamsBuilder {
return this; return this;
} }
public CommandParamsBuilder setRequestParamsMap(Map<String, String> requestParamsMap) {
this.commandParams.setRequestParamsMap(requestParamsMap);
return this;
}
public CommandParamsBuilder addRequestParam(String key, String value) {
if (requestParamsMap == null)
requestParamsMap = new HashMap<>();
requestParamsMap.put(key,value);
this.commandParams.setRequestParamsMap(requestParamsMap);
return this;
}
public CommandParams build(){ public CommandParams build(){
return commandParams; return commandParams;
} }
......
...@@ -46,6 +46,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -46,6 +46,7 @@ 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;
...@@ -72,11 +73,14 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -72,11 +73,14 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
public void setAuthType(Enums.EnumAuthenticationType authType) { this.authType = authType; } public void setAuthType(Enums.EnumAuthenticationType authType) { this.authType = authType; }
public BaseRestResponse handleSyncRespCommand(Supplier<BaseRestResponse> command) { public BaseRestResponse handleSyncRespCommand(String name, CommandParams cmdParams, Supplier<BaseRestResponse> command) {
BaseRestResponse resp = null; BaseRestResponse resp = null;
try { try {
if (restClient != null) if (restClient != null) {
if (cmdParams.getRcid() != null) // log rcid
msAppInstance.logRcid("handleSyncRespCommand." + name,cmdParams.getRcid());
resp = command.get(); resp = command.get();
};
} catch (Exception e) { } catch (Exception e) {
resp = new BaseRestResponse(false,e.toString()); resp = new BaseRestResponse(false,e.toString());
} finally { } finally {
...@@ -86,9 +90,11 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -86,9 +90,11 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
return resp; return resp;
} }
public boolean handleAsyncRespCommand(BooleanSupplier command) { public boolean handleAsyncRespCommand(String name, CommandParams cmdParams, BooleanSupplier command) {
boolean retstat; boolean retstat;
try { try {
if (cmdParams.getRcid() != null) // log rcid
msAppInstance.logRcid("handleAsyncRespCommand." + name,cmdParams.getRcid());
retstat = command.getAsBoolean(); retstat = command.getAsBoolean();
} catch (Exception e) { } catch (Exception e) {
retstat = false; retstat = false;
...@@ -98,49 +104,50 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -98,49 +104,50 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
@Override @Override
public BaseRestResponse create(CommandParams cmdParams) { public BaseRestResponse create(CommandParams cmdParams) {
return handleSyncRespCommand(() -> restClient.create(cmdParams) ); return handleSyncRespCommand(Enums.EnumCrudMethod.E_CREATE.name(),cmdParams,() -> restClient.create(cmdParams) );
} }
@Override @Override
public BaseRestResponse read(CommandParams cmdParams) { public BaseRestResponse read(CommandParams cmdParams) {
return handleSyncRespCommand(() -> restClient.read(cmdParams) ); return handleSyncRespCommand(Enums.EnumCrudMethod.E_READ.name(), cmdParams, () -> restClient.read(cmdParams) );
} }
@Override @Override
public BaseRestResponse update(CommandParams cmdParams) { public BaseRestResponse update(CommandParams cmdParams) {
return handleSyncRespCommand(() -> restClient.update(cmdParams) ); return handleSyncRespCommand(Enums.EnumCrudMethod.E_UPDATE.name(), cmdParams, () -> restClient.update(cmdParams) );
} }
@Override @Override
public BaseRestResponse delete(CommandParams cmdParams) { public BaseRestResponse delete(CommandParams cmdParams) {
return handleSyncRespCommand(() -> restClient.delete(cmdParams) ); return handleSyncRespCommand(Enums.EnumCrudMethod.E_DELETE.name(), cmdParams, () -> restClient.delete(cmdParams) );
} }
@Override @Override
public boolean asyncCreate(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) { public boolean asyncCreate(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) {
return handleAsyncRespCommand(() -> restClient.asyncCreate(reqCtx,cbFunc)); return handleAsyncRespCommand(Enums.EnumCrudMethod.E_CREATE.name(),reqCtx, () -> restClient.asyncCreate(reqCtx,cbFunc));
} }
@Override @Override
public boolean asyncRead(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) { public boolean asyncRead(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) {
return handleAsyncRespCommand(() -> restClient.asyncRead(reqCtx,cbFunc)); return handleAsyncRespCommand(Enums.EnumCrudMethod.E_READ.name(), reqCtx, () -> restClient.asyncRead(reqCtx,cbFunc));
} }
@Override @Override
public boolean asyncUpdate(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) { public boolean asyncUpdate(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) {
return handleAsyncRespCommand(() -> restClient.asyncUpdate(reqCtx,cbFunc)); return handleAsyncRespCommand(Enums.EnumCrudMethod.E_UPDATE.name(), reqCtx, () -> restClient.asyncUpdate(reqCtx,cbFunc));
} }
@Override @Override
public boolean asyncDelete(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) { public boolean asyncDelete(CommandParams reqCtx, Consumer<BaseRestResponse> cbFunc) {
return handleAsyncRespCommand(() -> restClient.asyncDelete(reqCtx,cbFunc)); return handleAsyncRespCommand(Enums.EnumCrudMethod.E_DELETE.name(), reqCtx, () -> restClient.asyncDelete(reqCtx,cbFunc));
} }
@Override @Override
public boolean init() { public boolean init() {
boolean stat = true; boolean stat = true;
logger = MicroserviceApp.getsInstance().getLogger(); msAppInstance = MicroserviceApp.getsInstance();
this.appName = MicroserviceApp.getsInstance().getAppName(); logger = msAppInstance.getLogger();
this.appName = msAppInstance.getAppName();
switch (getServiceMode()){ switch (getServiceMode()){
case E_CLIENT: case E_CLIENT:
break; break;
...@@ -276,7 +283,8 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -276,7 +283,8 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
reqCtx.rcid = requestHeaders.getFirst(Constants.RCID_HEADER); reqCtx.rcid = requestHeaders.getFirst(Constants.RCID_HEADER);
if (reqCtx.rcid == null) // create a new one if (reqCtx.rcid == null) // create a new one
reqCtx.rcid = new UUID().toString(); reqCtx.rcid = new UUID().toString();
else // log it
msAppInstance.logRcid("getRequestContext",reqCtx.rcid);
return reqCtx; return reqCtx;
} }
...@@ -416,7 +424,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements ...@@ -416,7 +424,7 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
} }
else else
{ {
MicroserviceApp.getsInstance().getLogger().error(NO_TOKEN_FOR_REQUEST); msAppInstance.getLogger().error(NO_TOKEN_FOR_REQUEST);
sendErrorResp(restContext.response,NO_TOKEN_FOR_REQUEST); sendErrorResp(restContext.response,NO_TOKEN_FOR_REQUEST);
valid = false; valid = false;
} }
......
...@@ -178,7 +178,11 @@ public class TestMicroserviceApp { ...@@ -178,7 +178,11 @@ public class TestMicroserviceApp {
CommonServices.IRestService inRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"undertowRestService"); CommonServices.IRestService inRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"undertowRestService");
RestContext restContext = (RestContext)msgCtx; RestContext restContext = (RestContext)msgCtx;
String query = restContext.getPathParameter("query"); String query = restContext.getPathParameter("query");
CommandParams cmdParams = new CommandParams().setEntity("172.16.1.132:5000").setParamsString("/v1/search").setRequestParams("q=" + query); CommandParams cmdParams = new CommandParams()
.setRcid(restContext.rcid)
.setEntity("172.16.1.132:5000")
.setParamsString("/v1/search")
.setRequestParams("q=" + query);
BaseRestResponse brr = inRestService.read(cmdParams); BaseRestResponse brr = inRestService.read(cmdParams);
inRestService.writeObjectToResponse(restContext.response,brr); inRestService.writeObjectToResponse(restContext.response,brr);
} }
......
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