Discovering and invoking the service

Now it's time to consume our registered service. We'll discover it and then consume it with Spring's RestTemplate by providing the host and port values of the service:

@EnableEurekaClient
@SpringBootApplication
public class EurekaWeatherServiceClient {
public static void main(String... args) {
SpringApplication.run(EurekaWeatherServiceClient.class);
}

@Autowired
private EurekaClient eurekaClient;

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}

@Bean
public CommandLineRunner run(RestTemplate restTemplate)
throws Exception {
Application application = eurekaClient
.getApplication("spring-cloud-eureka-client");
InstanceInfo instanceInfo = application.getInstances().get(0);
String hostname = instanceInfo.getHostName();
int port = instanceInfo.getPort();

return args -> {
String result = restTemplate.getForObject(
"http://" + hostname +
":" + port + "/temperature", String.class);

System.out.println(result);
};
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.109.8