Updated on 2023-10-18 GMT+08:00

Using the Configuration Center in Java Chassis

  • Java chassis uses the configuration center named config-center.

    You need to add the following dependencies to the project:

    <dependency>
      <groupId>org.apache.servicecomb</groupId>
      <artifactId>config-cc</artifactId>
    </dependency>

    If the preceding dependencies have been directly or indirectly included in the project, you do not need to add them. Java chassis contains the configuration items listed in Table 1. The values of these configuration items specify the identity of microservices in the configuration center and the interaction between microservices and the configuration center.

    Table 1 Common configuration items of Java chassis

    Item

    Description

    Default Value

    Remarks

    servicecomb.service.application

    Application

    default

    -

    servicecomb.service.name

    Microservice name

    defaultMicroservice

    -

    servicecomb.service.version

    Microservice version

    1.0.0.0

    -

    servicecomb.service.environment

    Environment

    -

    The value can be production, development, etc.

    servicecomb.config.client.serverUri

    Access address. The format is http(s)://{ip}:{port}. Use commas (,) to separate multiple addresses.

    http://127.0.0.1:30103

    config-center

    servicecomb.config.client.tenantName

    Tenant name of the application

    default

    config-center

  • Java chassis uses the configuration center named kie.
    You need to add the following dependencies to the project:
    <dependency>
      <groupId>org.apache.servicecomb</groupId>
      <artifactId>config-kie</artifactId>
    </dependency>

    If the preceding dependencies have been directly or indirectly included in the project, you do not need to add them. Java chassis contains the configuration items listed in Table 2. The values of these configuration items specify the identity of microservices in the configuration center and the interaction between microservices and the configuration center.

    Table 2 Common configuration items of Java chassis

    Item

    Description

    Default Value

    Remarks

    servicecomb.service.application

    Application

    default

    -

    servicecomb.service.name

    Microservice name

    defaultMicroservice

    -

    servicecomb.service.version

    Microservice version

    1.0.0.0

    -

    servicecomb.service.environment

    Environment

    -

    The value can be production, development, etc.

    servicecomb.kie.serverUri

    Address for accessing kie. The format is http(s)://{ip}:{port}. Use commas (,) to separate multiple addresses.

    -

    kie

    servicecomb.kie.firstRefreshInterval

    Interval for updating configuration items for the first time (ms)

    3000

    kie

    servicecomb.kie.refresh_interval

    Interval for updating configuration items (ms)

    3000

    kie

    servicecomb.kie.domainName

    Tenant name of the application

    default

    kie

Java chassis provides multiple methods to read dynamic configurations.
  • The first method is to use the archaius API, for example,
    DynamicDoubleProperty myprop = DynamicPropertyFactory.getInstance()
      .getDoubleProperty("trace.handler.sampler.percent", 0.1);
    

    The archaius API supports callback to process configuration change:

    myprop.addCallback(new Runnable() {
          public void run() {
              // When the value of a configuration item changes, the callback method is invoked.
              System.out.println("trace.handler.sampler.percent is changed!");
          }
      });
    
  • The second method is to use the configuration injection mechanism provided by Java chassis. This method can easily handle complex configurations and configuration priorities. For example,
      @InjectProperties(prefix = "jaxrstest.jaxrsclient")
      public class Configuration {
        /*
         * The prefix attribute override of a method will overwrite @InjectProperties defined in the class.
         * The prefix attribute of an annotation.
         *
         * The keys attribute can be a string array. A smaller subscript indicates a higher priority.
         *
         * The system searches for configuration attributes in the following sequence until the configured configuration attributes are found:
         * 1) jaxrstest.jaxrsclient.override.high
         * 2) jaxrstest.jaxrsclient.override.low
         *
         * Test case:
         * jaxrstest.jaxrsclient.override.high: hello high
         * jaxrstest.jaxrsclient.override.low: hello low
         * Expected result:
         * hello high
         */
        @InjectProperty(prefix = "jaxrstest.jaxrsclient.override", keys = {"high", "low"})
        public String strValue;
    

    Inject configurations.

     ConfigWithAnnotation config = SCBEngine.getInstance().getPriorityPropertyManager()
          .createConfigObject(Configuration.class,
                "key", "k");
    
  • The third method is used when Spring and Spring Boot are integrated. The configuration can be read in the native mode of Spring and Spring Boot, for example, @Value and @ConfigurationProperties. Java chassis applies configuration hierarchy to Spring Environment. Spring and Spring Boot can also read the dynamically configured values and the values in the microservice.yaml file.

    For more information about the read configurations of Java chassis, see the developer guide of the community.