Service virtualization with spring boot microservices

  • Service Virtualization mimics or simulates behaviors of components that are unavailable or difficult to access while testing
  • Microservices applications are generally composed of multiple services. During the testing phase when the dependent services are not available, service virtualization technique allows to mimic the unavailable service and continue the tests even when the actual service is unavailable.
  • During the development/testing phase of microservices based applications, unavailability of dependent service can impact the productivity of the team because this prevents the system from being tested end to end or proceed with the use case which expects data from another service.
  • Typical solutions to deal with such scenarios are (1) mocking service using some popular mocking frameworks , but this needs lot of effort to cover all the applicable scenarios. (2) Mock the service using hardcoded value .
  • Both the above options are not effective and usually takes up lot of effort and maintenance. Service virtualization helps to solve the problem by mocking or simulating as the unavailable service.
  • It is a technique used to emulate the behavior of dependencies of component-based applications.
  • In a microservices architecture, these dependencies are usually REST APIā€“based services, but the concept can also be applied to other kinds of dependencies such as databases, enterprise service buses (ESBs), web services, Java Message Service (JMS), or any other system that communicates using messaging protocols.
  • Hoverfly is an open source, lightweight, service virtualization API simulation tool written in the Go programming language and tightly integrated with Java.
  • We will demonstrate service virtualization using Hoverfly and below use case.
    • Student Service expose an API which will give student details by fetching data from a H2 DB
    • Client Service invokes Student Service and gets the result
  • We will simulate student service by bringing it down and see how can we test Client Service
  • Student Service controller
@GetMapping("/students")
public List<Student> findAllStudents() {
return studentSvc.findAllStudents();
}
  • Student Service Implementation
@Autowired
StudentRepository studentRepository;

public List<Student> findAllStudents(){

return studentRepository.findAll();
}
  • Response from Student service. (Data is loaded at the initilization of the service)
  • You can start Hoverfly by the command – hoverctl start
  • Before proceeding further let us understand , how will Hovefly simulate our service
  • Hoverfly offers 4 different modes, capturesimulatemodify and synthesize. We will only look for capture and simulate mode in this demo.
  • In Capture mode, Hoverfly (running as a proxy server) intercepts communication between the client application and the external service. It transparently records outgoing requests from the client and the incoming responses from the service API.
  • Capture mode is used as the starting point in the process of creating an API simulation. Captured data is then exported and modified before being re-imported into Hoverfly for use as a simulation.
  • So, we will start the services and hoverfly. Then we will enable capture mode this will capture the requests and response.
  • When student service is down , we will run this flow in simulate mode and the response captured at hoverfly will be served

Client Service Controller

@GetMapping("/students-client")
public String invoke() {

System.out.println("inside TestController::invoke()-->"+ Arrays.toString(env.getActiveProfiles()));
String url = "http://localhost:8080/api/students";
String response = restTemplate.exchange(url, HttpMethod.GET, null,
new ParameterizedTypeReference<String>() {
}).getBody();

System.out.println("Actual Response : " + response);
return response;
}

We will have 2 modes in Client Application – TEST and PROD

In the Test mode, response will be served from Hoverfly and PROD mode will be a normal flow without virtualization

@SpringBootApplication
public class HoverflyClientApplication {

    private static final int HOVERFLY_PORT = 8500;
    private static final String HOVERFLY_HOST = "localhost";
    private static final String PROXY = "proxy";

    public static void main(String[] args) {

        SpringApplication.run(HoverflyClientApplication.class, args);
    }

    @Bean
    @Profile("TEST")
    public RestTemplate getHoverflyProxiedRestTemplate() {

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(HOVERFLY_HOST, HOVERFLY_PORT));
        requestFactory.setProxy(proxy);
        return new RestTemplate(requestFactory);
    }

    @Bean
    @Profile({"PROD","default"})
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

  • Start the Client service in TEST mode – mvn spring-boot:run -Dspring-boot.run.profiles=TEST
  • Start the Student Service
  • Hoverfly will capture the request as its running in capture mode and you can see the count in capture section
  • This flow has been captured by Hoverfly .
  • Now Click on simulate in Hoverfly dashboard and bring student service down
  • If we access student client service, without service virtualization we would have got 500 internal server error , but Hoverfly will virtualize it for us
  • In Hoverfly dashboard you will see the count of simulate has also increased to 1 as it simulated the service
  • Hoverfly documentation has details on how to export the data in json and import it again to simulate , so that every time we don’t have to run this in capture mode –
  • With Service Virtualization, teams can now use virtual services instead of production services, so they can test the system even when key services are not ready.
  • The above examples explain the basic concepts of using Hoverfly with two microservices, but Hoverfly was designed to provide you with the means to create your own ā€œdependency sandboxā€: a simulated development and test environment that you control.

Thanks.. Hope you enjoyed reading, share your comments / feedback

2 thoughts on “Service virtualization with spring boot microservices

Leave a Reply