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.