Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of spring application index

Can someone please clarify what is the usage of spring.application.index property and why do we need it?

Application.yml:

spring:
    application:
        name: ServiceName
        index: 
like image 206
CleverDev Avatar asked May 16 '19 07:05

CleverDev


2 Answers

As far as I see, the spring.application.index has been considered deprecated since the version 2.0.0.RC1. I judge from comparing the following these appendices:

  • Spring Boot 2.0.0.M7: Common application properties where the spring.application.index is mentioned.
  • Spring Boot 2.0.0.RC1: Common application properties where the spring.application.index is not mentioned.

The previous statement is proved by inspecting the source codes of the ContextIdApplicationContextInitializer across these versions:

  • Version 2.0.0.M7 ContextIdApplicationContextInitializer. These versions provide more variability in customization the application index used for ApplicationContextID creation.

    /**
     * Placeholder pattern to resolve for application index. The following order is used
     * to find the name:
     * <ul>
     * <li>{@code vcap.application.instance_index}</li>
     * <li>{@code spring.application.index}</li>
     * <li>{@code server.port}</li>
     * <li>{@code PORT}</li>
     * </ul>
     * This order favors a platform defined index over any user defined value.
     */`"${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}"`
    
  • Version 2.0.0.RC1 ContextIdApplicationContextInitializer. There could be find the index is incremented using automatically using AtomicLong, which also assures its uniqueness. See the inner ContextIdApplicationContextInitializer$ContextId class for more detail in the source. The key method is its constructor:

    ContextId createChildId() {
        return new ContextId(this.id + "-" + this.children.incrementAndGet());
    }
    
like image 185
Nikolas Charalambidis Avatar answered Sep 18 '22 17:09

Nikolas Charalambidis


You can find this property in Spring sources:

private static final String INDEX_PATTERN = 
"${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}";

As the Javadoc of this class says, it is used to create ApplicationContextID which is a unique id of an application context.

like image 21
mate00 Avatar answered Sep 18 '22 17:09

mate00