Search code examples
jhipstermicroservices

How can microservice can talk to other microservice in JHipster


I am planning to create a microservice aplication with a dedicated service for dealing with data (mostly a Mongodb based service). I am wondering if there is a way using which my other microservices will be able to communicate with this service to make use of the shared data. Is it possible with JHipster API Gateway ? If not how can I achieve this. I dont want to keep multiple copies of the same data within each microservice.


Solution

  • You can register your microservices to the same registry and then they can call each other.

    UPDATE : Here is how I made it work. In the microservice consuming the data one, use RestTemplate with the current user's jwt-token in the Authorization-header for the 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 for 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 the resource controller where your are making the call for data:

    @RestController
    @RequestMapping("/api")
    public class DataResource {    
        @Autowired
        RestTemplate restTemplate;
    
                @PostMapping("/hello")
                @Timed
                public ResponseEntity<Hello> createHello(@RequestBody Hello Hello) throws URISyntaxException {
                    
        //The name your data micro service registrated in the Jhipster Registry
                    String dataServiceName = "data_micro_service";
                
                    URI uri = UriComponentsBuilder.fromUriString("//" + dataServiceName + "/api/datas")
                        .build()
                        .toUri();
                
                    //call the 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);
            
            }
    
    }