0

I have taken a reference from http://www.javainuse.com/spring/spring_ribbon without any customization yet. I was able to run the eureka-server fine, just added below

server.port=8090
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name=eureka-server

I also have employee-producer which also runs fine

application.properties

eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka
eureka.instance.instanceId=${spring.application.name}:${random.value}

bootstrap.properties

spring.application.name=employee-producer
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

when I tried to run the employee-consumer, I am getting below error strong textConsumerControllerClient

@Controller
public class ConsumerControllerClient {

    @Autowired
    private LoadBalancerClient loadBalancer;

    public void getEmployee() throws RestClientException, IOException {

        ServiceInstance serviceInstance=loadBalancer.choose("employee-producer");

        System.out.println(serviceInstance.getUri());

        String baseUrl=serviceInstance.getUri().toString();

        baseUrl=baseUrl+"/employee";

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response=null;
        try{
        response=restTemplate.exchange(baseUrl,
                HttpMethod.GET, getHeaders(),String.class);
        }catch (Exception ex)
        {
            System.out.println(ex);
        }
        System.out.println(response.getBody());
    }

    private static HttpEntity<?> getHeaders() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
        return new HttpEntity<>(headers);
    }
}

The error which I am getting is

Exception in thread "main" java.lang.NullPointerException
    at com.javainuse.controllers.ConsumerControllerClient.getEmployee(ConsumerControllerClient.java:28)
    at com.javainuse.SpringBootHelloWorldApplication.main(SpringBootHelloWorldApplication.java:21)

SpringBootHelloWorldApplication

@SpringBootApplication
public class SpringBootHelloWorldApplication {

    public static void main(String[] args) throws RestClientException, IOException {
        ApplicationContext ctx = SpringApplication.run(SpringBootHelloWorldApplication.class, args);

        ConsumerControllerClient consumerControllerClient = ctx.getBean(ConsumerControllerClient.class);
        System.out.println(consumerControllerClient);
        consumerControllerClient.getEmployee();

    }

    @Bean
    public ConsumerControllerClient consumerControllerClient() {
        return new ConsumerControllerClient();
    }
}
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186

0 Answers0