How can microservice can talk to other microservice in JHipster -
i planning create microservice aplication dedicated service dealing data (mostly mongodb based service). wondering if there way using other microservices able communicate service make use of shared data. possible jhipster api gateway ? if not how can achieve this. dont want keep multiple copies of same data within each microservice.
you can make microservices registred same registry , can call each other.
update : here how make works. in microservice consuming data one, use resttemplate current user jwt token authorization in header make api calls :
@component public class authenticateclienthttprequestinterceptor implements clienthttprequestinterceptor { @override public clienthttpresponse intercept(httprequest httprequest, byte[] bytes, clienthttprequestexecution clienthttprequestexecution) throws ioexception { string token = securityutils.getcurrentuserjwt(); httprequest.getheaders().add("authorization","bearer "+token); return clienthttprequestexecution.execute( httprequest, bytes ); } }
my custom resttemplate using clienthttprequestinterceptor adding token in header.
@configuration public class custombean { @autowired authenticateclienthttprequestinterceptor interceptor; @bean @loadbalanced public resttemplate resttemplate() { resttemplate resttemplate = new resttemplate(); resttemplate.setinterceptors(collections.singletonlist(interceptor)); return resttemplate; } }
and in resource controller making call data:
@restcontroller @requestmapping("/api") public class dataresource { @autowired resttemplate resttemplate; @postmapping("/hello") @timed public responseentity<hello> createhello(@requestbody hello hello) throws urisyntaxexception { //the name data micro service registrated in jhipster registry string dataservicename = "data_micro_service"; uri uri = uricomponentsbuilder.fromuristring("//" + dataservicename + "/api/datas") .build() .touri(); //call data microservice apis list<data> result = resttemplate.getforobject(uri, data[].class); return responseentity.created(new uri("/api/hellos/" + result.getid())) .headers(headerutil.createentitycreationalert(entity_name, result.getid().tostring())) .body(result); } }
Comments
Post a Comment