Spring Cloud Config Learning

Keywords: Java git Spring github Docker

Learning Notes for "Spring Cloud and Docker Micro Service Architecture Actual Warfare"

Config Client

In the previous article, we have written how that client of Config Server accesses Config Server and gets the corresponding configuration.
Let's take a look

Integrate

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

Configure the port in application.properties:

server.port=8081

Create the configuration file bootstrap.yml and add something to it

spring:
  application:
    # Corresponds to {application} in Config Server
    name: microservice-foo
  cloud:
    config:
      # Config Server Address
      uri: http://localhost:8080/
      # Profile corresponds to {profile} in Config Server
      profile: dev
      # Label corresponds to the {label} Git branch in Config Server
      label: master

Only the specified profile name can be used here, other file names are not valid

As described in the book, Spring Cloud has the concept of a "boot context", which is the parent context of the main application.The boot context is responsible for loading configuration properties from the configuration server and decrypting properties in the external configuration file.

Unlike the properties in the main application load application.*(yml or properties), the boot context loads the properties in bootstrap. *.Attributes configured in bootstrap. * have higher priority, so they cannot be local by default
Configuration Override

As I understand it, bootstrap. * is simply like a parent of application. * but one difference is that properties in bootstrap are not overridden by properties in application. *

With this simple configuration, you can pull properties from Config Service

Verification

Verify

@RestController
public class ConfigClientController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String hello() {
        return this.profile;
    }

}

Start Config Service Start Config Client, access
http://localhost : 8081/profile, showing:

Config Server Git Repository Configuration

Placeholder support

Placeholder support for Config Service {application}, {profile}, and {label}

For example, modify the configuration of the original Config Service:

spring.application.name=microservice-config-server

# This uri uses clone-enabled paths
spring.cloud.config.server.git.uri=https://github.com/wkkdhr/{application}.git

# github's account password
spring.cloud.config.server.git.username=***
spring.cloud.config.server.git.password=***

Add the following configuration file to Git: Dome1-dev.properties:

profile=dome1-dev-1.0

Then visit http://localhost : 8080/Dome1-dev.properties yields the following results:

profile: dome1-dev-1.0

It says this can support an application of a Git repository.It should be understood here that the placeholders {application}, {profile}, and {label} mentioned in the book refer to accessing the corresponding content in the url.For example, the access path is http://localhost : 8080/Dome1-dev.properties, where Dome1 is {application}, dev is {profile}, {label} is omitted, default master.The https://github.com/wkkdhr/{application}.git system will go as configured in the configuration file https://github.com/wkkdhr/Dom... The configuration file Dome1-dev.properties was found in this Git repository.

Match pattern

Configuration mode is to access different git repositories by matching the name of the configuration file to be accessed.If not, the warehouse defined by spring.cloud.config.server.git.uri is accessed

The configuration mode is {application}/{profile}.Multiple matching rules separated by commas

For example:

spring.cloud.config.server.git.uri=https://github.com/wkkdhr/Dome1.git

spring.cloud.config.server.git.repos.test.pattern=test*/dev*
spring.cloud.config.server.git.repos.test.uri=https://github.com/wkkdhr/configTest.git

As an example of the above configuration, the test in repos.test.pattern can be a name that starts with itself.test*/dev* is a matching rule that means configuring {application} to start with test and {profile} to start with dev.If matching rules are met, go back to the repository https://github.com/wkkdhr/configTest.git.

http://localhost 8080/test-dev.properties Here, the path, {application} is test, {profile} is dev, conforms to the rules defined above, so you will visit the warehouse defined by spring.cloud.config.server.git.repos.test.uri.

If it is http://localhost : 8080/test-d1.properties.If it doesn't conform to the rules, it will visit the repository https://github.com/wkkdhr/Dome1.git.

spring.cloud.config.server.git.repos.simple=https://github.com/wkkdhr/simple.git

If you configure it as above, only the configuration file that starts with test will be matched.

This destination address can also be configured with a cost-local path:

spring.cloud.config.server.git.repos.local.pattern=test*
spring.cloud.config.server.git.repos.local.uri=file:/D:\\project\\demo\\github\\configTest

search for directory

As you can see, you can search the subdirectories of the target Git repository.If it is a subdirectory of a subdirectory, you need to fill in the path

spring.cloud.config.server.git.uri=https://github.com/wkkdhr/Dome1.git
spring.cloud.config.server.git.search-paths=test1,test1*,file/test1,file/test*

Load profile at startup

spring.cloud.config.server.git.clone-on-start=true

Configure clone-on-start to allow the Git repository specified for clone at Config Service startup

Or configure the Git repository separately:

spring.cloud.config.server.git.repos.test.uri=https://github.com/wkkdhr/configTest.git
spring.cloud.config.server.git.repos.test.clone-on-start=true

The following configurations can print related logs, but they will also print many unrelated configuration information for your own consideration.

logging.level.org.springframework.cloud=debug
logging.level.org.springframework.boot=debug

Config Service Health Monitoring

Config Service integrates Actuator.You can query the current health status by accessing the health port.

# Configure to have health display details
management.endpoint.health.show-details=always

By default, {application} requested by the health indicator to the EnvironmentRepository is app,{profile} and {lable} are default values for the corresponding EnvironmentRepository implementation, according to the book.For Git, {profile} is default, {ablel} is master.
However, I do not have a corresponding configuration file in the configuration repository, and the result still shows the UP.No further study.

You can check the specified configuration file by configuring:

spring.cloud.config.server.health.repositories.test.name=test
spring.cloud.config.server.health.repositories.test.label=master
spring.cloud.config.server.health.repositories.test.profiles=dev

Visit http://localhost : 8080/actuator/health results are as follows:

{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 214752784384,
                "free": 135328772096,
                "threshold": 10485760
            }
        },
        "refreshScope": {
            "status": "UP"
        },
        "configServer": {
            "status": "UP",
            "details": {
                "repositories": [
                    {
                        "name": "test",
                        "profiles": [
                            "dev"
                        ],
                        "label": "master"
                    }
                ]
            }
        }
    }
}

You can disable health checks by setting spring.cloud.config.server.health.enabled=false.

spring.cloud.config.server.health.enabled=false

One thing you've noticed is that in the spring configuration file, there are many configurations that are not prompted, like this property above.It shows a yellow background similar to the user-defined properties.This means that this is not a property of the system, but is valid.At first I thought it was obsolete. Probably the spring family had too many buckets, and idea couldn't give me all the tips.

Posted by Pixelsize on Sat, 10 Aug 2019 21:06:43 -0700