1

I have an application with eureka, ribbon and feign. I have a feign RequestInterceptor, but the problem is that I need to know which is the host where I'm making the call. So far, with my current code I just can get the path with the RequestTemplate, but not the host.

Any idea?

nole
  • 1,422
  • 4
  • 20
  • 32

1 Answers1

-1

I'm new to this as well, but I believe I've just learned something relevant to your question.

In the service you're creating, each instance can be given some unique identifier tied to a property included in its instance profile. Some .properties examples below:

application.properties (shared properties between instances)

spring.application.name=its-a-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

application-instance1.properties

server.port=5678
instance.uid=some-unique-property

application-instance2.properties

server.port=8765
instance.uid=other-unique-property

This service, as an extremely contrived example will show, can send out @Value annotated attributes to be consumed by the Ribbon app:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ItsAServiceApplication {

    @Value("${server.port}")
    private int port;

    @Value("${instance.uid}")
    private String instanceIdentifier;

    public static void main(String[] args) {
        SpringApplication.run(ItsAServiceApplication.class, args);
    }

    @RequestMapping
    public String identifyMe() {
        return "Instance: " + instanceIdentifier + ". Running on port: " + port + ".";
    }
}

And just to complete the example, the Ribbon app that might consume these properties could look like this:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceIdentifierAppApplication {

    @Autowired
    private RestTemplate restTemplate;

    public static void main(String[] args) {
        SpringApplication.run(ServiceIdentifierAppApplication.class, args);
    }

    @GetMapping
    public String identifyMe() {
        return restTemplate.getForEntity("http://its-a-service", String.class).getBody();
    }

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

Results: Reloading the rest template created by the services

As I said earlier, I'm pretty new to this and have just been learning myself. Hopefully this has given you an idea for how you might send these properties! I would imagine creating a dynamic property identifier through Spring Cloud Config would be ideal here.

samtietze
  • 1
  • 1
  • 2
  • As I said in my last comment I resolved my issue with a new class that extend from LoadBalancerFeignClient. Anyway I am using a feign client instead of rest template. When you are working with feign and ribbon, your host is removed from your url – nole Mar 05 '18 at 07:22
  • Well you win some, you lose more. – samtietze Mar 05 '18 at 16:17