Spring Boot configuration - Consul configuration center

Keywords: Java Spring github Maven Docker

▶ Spring Boot dependency and configuration

Maven dependence

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        .....
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>

    </dependencies>

▶ instructions for use

1. Deploy Consul

  • Reference document: Previous

2. Add the configuration file bootstrap.properties under the resources path. The example is as follows:

# Basic configuration of consumer
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500

# Enable consumer configuration center
spring.cloud.consul.config.enabled=true

# Base folder, default config
spring.cloud.consul.config.prefix=config

# Application folder, the default value is application, and the default value is config / < ApplicationName > and config / < defaultcontext > two configurations will be loaded. If they are set to the same value, only one will be loaded
spring.cloud.consul.config.default-context=testApp
spring.application.name=testApp

# Environment separator, default value ","
spring.cloud.consul.config.profile-separator=-

# Configure transcoding mode, default key value, other options: yaml/files/properties
spring.cloud.consul.config.format=properties

# Configure the key value. The value corresponds to the entire configuration file
spring.cloud.consul.config.data-key=data

# Enable configuration auto refresh
spring.cloud.consul.config.watch.enabled=true

# [question] delay in requesting the consumer API, in seconds
spring.cloud.consul.config.watch.wait-time=1

# Refresh rate in milliseconds
spring.cloud.consul.config.watch.delay=10000

3. Add application configuration in Consul Key/Value

The configuration item spring.cloud.consumer.config.prefix specifies that the basic folder is config. You need to create the folder config first:

There are two types of new folders: folder and Key/Value. To create a folder, just add "/" after it

Configuration items spring.cloud.consumer.config.default-context and spring.cloud.consumer.config.profile-separator specify application name and environment separator. For example, application testApp has environment default, dev and prod. you only need to create three folders testApp, testApp dev and testApp prod in config Directory:

The configuration item spring.cloud.consumer.config.format specifies the Value conversion method. According to personal preferences, it can be configured as yaml or properties. If you choose these two methods, you need to configure spring.cloud.consumer.config.data-key, which is data by default. Example configuration:

Tips: if you need to configure each Key/Value separately, neither spring.cloud.consumer.config.format nor spring.cloud.consumer.config.data-key need to be set

4. Configuration refresh

Spring.cloud.consumer.config.watch.delay sets the refresh interval of the configuration. If the configuration is modified in Consul, it will be dynamically synchronized to the application.

▶ Github Demo URL

▶ related documents

  • Spring Cloud Consul Config

Play wechat everyday, Spring Boot develops private instant messaging system

Spring boot's correct posture for logging

Spring Boot custom parent quick build application

Spring Boot containerized deployment Docker

How to configure https in SpringBot

Spring Boot log processing are you still using Logback?

[behind the double 11 Carnival] how does the micro service registration center carry tens of millions of visits to large-scale systems?

Spring Boot new generation monitoring you should play like this

Spring Boot exception handling

Spring Boot configuration - configuration information encryption

Reject black box application - visual monitoring of Spring Boot application

There are three sources of concurrent bugs. Please open your eyes to see them

This article is based on the platform of blog one article multiple sending OpenWrite Release!

Posted by bbmak on Wed, 13 Nov 2019 10:31:26 -0800