I am using RestTemplate with Ribbon Client @LoadBalanced. When I am calling my service - time-service(logical identifier) using Discovery Server, which is running on two instances, it is throwing null pointer exception.
My two instances of time-service are running properly. Also, in discovery server both are registered along with this Ribbon client.
Below is the code for same -
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class RibbonTimeAppApplication {
@Inject
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(RibbonTimeAppApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@GetMapping
public String getTime()
{
String response = "";
try {
response = restTemplate.getForEntity("http://time-service", String.class).getBody();
}
catch(Exception e)
{
e.printStackTrace();
}
return response;
}
}
Here is the stacktrace I am getting when calling this service -
java.lang.NullPointerException
at com.javatechstack.RibbonTimeAppApplication.getTime(RibbonTimeAppApplication.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at .......
.........................
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
This is the screenshot of Eureka dashboard -