2
votes

spring cloud eureka question: there are 3 microservices, 1 eureka server, 2 eureka client;

  1. microservice A,B use annotation @EnableEurekaClient;

  2. microservice A have a RESTful api "http://localhost:8080/hi". the api return "hello".

  3. now, I call the api , use url "http://client/hi", but it doesn't work.

  4. how use application name replace ip:port about spring cloud eureka?

the bootstrap.yml content:

spring:
  application:
    name: client

eureka:
  client:
    service-url:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
1

1 Answers

1
votes

There are many ways to do that and it depends on how you call REST API in your code.

If you are using RestTemplate to call the API, you can do that with @LoadBalanced RestTemplate

In your code that wants to invoke REST api, please define RestTemplate with @LoadBalanced like below.

@LoadBalanced
@Bean
RestTemplate restTemplate(){
  return new RestTemplate();
}

And when you call API, just use application name instead of host:port like below.

this.restTemplate.getForObject("http://client/hi", String.class)

If you are using SpringCloud Feign, you can define the interface to call your REST api like below (without URL)

@FeignClient(name="client")
public interface ProductResource {
   : 
}

And add annotation @EnableFeignClients in your spring boot application like below.

@SpringBootApplication
@EnableFeignClients
     : // other annotations you need.
public class YourAPIInvokerApplication {

In both ways, you need to add a few dependencies.