Section 3: Integrate Microservices
Integration with other DIGIT services
Overview
A separate class should be created for integrating with each dependent microservice. Only one method from that class should be called from the main service class for integration.
This guide showcases the steps to integrate our microservices with other microservices like
Common Resources
Code for developer guide till this stage is available here. Postman collection corresponding to this stage is available here.
For interacting with other microservices, we can create and implement the following ServiceRequestRepository
class under repository
package -
package digit.repository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.egov.tracer.model.ServiceCallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Repository
@Slf4j
public class ServiceRequestRepository {
private ObjectMapper mapper;
private RestTemplate restTemplate;
@Autowired
public ServiceRequestRepository(ObjectMapper mapper, RestTemplate restTemplate) {
this.mapper = mapper;
this.restTemplate = restTemplate;
}
public Object fetchResult(StringBuilder uri, Object request) {
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Object response = null;
try {
response = restTemplate.postForObject(uri.toString(), request, Map.class);
}catch(HttpClientErrorException e) {
log.error("External Service threw an Exception: ",e);
throw new ServiceCallException(e.getResponseBodyAsString());
}catch(Exception e) {
log.error("Exception while fetching from searcher: ",e);
}
return response;
}
}
Last updated
Was this helpful?