spring cloud config

What is the purpose of config?
Externally centralize the configuration files of each microservice in the cloud and implement unified management. Especially in a service load, the centralized management of configuration will be very convenient.

use of spring cloud config
* config client
1.dependcy

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

2. The purpose of introducing the bootstrap.yml configuration file
is to load these configurations when the application starts. It is the parent context of the applicationContext, and the application.yml configuration file corresponds to the applicationContext. The configuration of bootstrap.yml will be the default configuration parameters of the application, which can be set to be overridden by the same parameters of the application. For example:

spring:
  cloud:
    config:
      allow-override: true
      name: web-demo
      uri: http://localhost:8888
      fail-fast: true
      profile: test
            retry:
        initial-interval: 1000
        max-attempts: 6
        max-interval: 2000

spring.cloud.config.allow-override is used for whether the parameter is allowed to be overridden;
spring.cloud.config.name The configuration {application} name of the microservice, if not set, the default is spring.application.name;
spring.cloud.config. uri refers to the uri of spring cloud config server, the default is localhost:8888;
spring.cloud.config.fail-fast refers to the failure to start directly if the connection fails;
spring.cloud.config.profile refers to which one is currently used Configuration file, usually dev, test, pro;
spring.cloud.config.usernam connection user name
spring.cloud.config.password connection password
spring.cloud.config.retry.inital-interval interval between failure and reconnection;
spring.cloud .config.retry.max-interval The maximum interval for failed reconnection;
spring.cloud.config.retry.max-attempts The maximum number of failed reconnections;

3. The use of remote configuration files is
very simple to use. It can be introduced in configuration files such as application.yml through ${}; configuration parameters like application.yml can also be used by java code directly through annotations. For example:
Configuration parameters of web-demo-test.yml in config server:

#sys config
server:
  port: 8081

#business config
user:
  username: yangyc
  password: 123456

Configuration parameters of application-test.yml in config client:

server:
  port: ${server.port}

#business config
user:
  username: ${user.username}
  password: ${user.password}

Note: where ${server.port} will be replaced with the parameters of config server;

* config server
1.depandcy

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

2. Add @EnableConfigServer annotation at startup

@SpringBootApplication
@EnableConfigServer
public class ConfigDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigDemoApplication.class, args);
    }
}

3. Classification of config backend storage
3.1 native local storage

spring.application.name=config-demo
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config/{application}
spring.cloud.config.server.native.add-label-locations=false
spring.cloud.config.server.accept-empty=false
logging.level.root=debug

spring.profiles.active: The type of profile. native local files; jdbc/svn/git/vault.
spring.cloud.config.server.native.search-locations: local file path. It can be a disk file or classpath;
Note: placeholders {application}, {profiles}, {label} can be used in path. That is, a corresponding directory can be created in the file directory to achieve the effect of dynamic adaptation.
spring.cloud.config.server.native.add-label-locations: Indicates whether the configuration file directory includes labels.
spring.cloud.config.server.accept-empty: Indicates whether to allow empty data to be obtained.

3.2 JDBC storage
adds dependencies

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

Configure the data source

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

configure config server

spring.profiles.active=jdbc
spring.cloud.config.server.jdbc.sql=SELECT `KEY`,`VALUE` FROM PROPERTIES WHERE APPLICATION =? AND `PROFILE`=? AND LABEL=?
spring.cloud.config.server.jdbc.order=0

Note: APPLICATION corresponds to {application}, PROFILE corresponds to {profile}, and LABLE corresponds to {label}. will be set automatically.

database table

create table PROPERTIES
(
  APPLICATION VARCHAR(100),
  `PROFILE`  VARCHAR(100),
  LABEL VARCHAR(100),
  `KEY` VARCHAR(100),
  `VALUE` VARCHAR(100)
) ENGINE=INNODB DEFAULT CHARSET=UTF8;

3.3 SVN storage
increases dependencies

         <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.9.2</version>
      </dependency>
spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=
spring.cloud.config.server.svn.username=
spring.cloud.config.server.svn.password=
spring.cloud.config.server.svn.default-label=config
spring.cloud.config.server.svn.search-paths=

spring.profiles.active=subversion indicates the backend method used by the configuration file
spring.cloud.config.server.svn.uri indicates the SVN warehouse address
spring.cloud.config.server.svn.username username
spring.cloud.config.server. svn.password password
spring.cloud.config.server.svn.default-label must be configured with Git as well, referring to the last level directory under uri, the default is trunk, otherwise it needs to be specified.
spring.cloud.config.server.svn.search-paths refers to the access address of the local warehouse

3.4 git storage

spring.profiles.active=git
spring.cloud.config.server.git.basedir=
spring.cloud.config.server.git.clone-on-start=false
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.host-key=
spring.cloud.config.server.git.host-key-algorithm=
spring.cloud.config.server.git.delete-untracked-branches=true
spring.cloud.config.server.git.ignore-local-ssh-settings=true
spring.cloud.config.server.git.known-hosts-file=
spring.cloud.config.server.git.order=1
spring.cloud.config.server.git.passphrase=
spring.cloud.config.server.git.password=
spring.cloud.config.server.git.preferred-authentications=
spring.cloud.config.server.git.private-key=
spring.cloud.config.server.git.refresh-rate=
spring.cloud.config.server.git.search-paths=
spring.cloud.config.server.git.strict-host-key-checking=true
spring.cloud.config.server.git.timeout=5
spring.cloud.config.server.git.uri=
spring.cloud.config.server.git.username=

spring.cloud.config.server.git.basedir: Like SVN/GIT, it will first check out / clone the files in the remote to the local, the default is /temp temporary directory, but some operating systems will delete temporary files, so it is necessary to Modify the corresponding directory. And this configuration file is also the address of the configuration local library.
spring.cloud.config.server.git.force-pul: Indicates whether to force the latest data from the remote repository.
spring.cloud.config.server.git.uri: remote repository address
spring.cloud.config.server.git.username: username
spring.cloud.config.server.git.password: password
spring.cloud.config.server. git.search-paths: subdirectories under uri, can use placeholder {application}

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: http://git/team-a/config-repo.git
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: http://git/team-b/config-repo.git
            team-c:
                pattern: team-c-*
                uri: http://git/team-a/config-repo.git

3.5 Vault Storage

spring.profiles.active=vault
spring.cloud.config.server.vault.host=
spring.cloud.config.server.vault.port=
spring.cloud.config.server.vault.scheme=http
spring.cloud.config.server.vault.profile-separator=,
spring.cloud.config.server.vault.order=1
spring.cloud.config.server.vault.backend=secret

3.6 Multiple types of composite use

spring:
  profiles:
    active: git, svn
  cloud:
    config:
      server:
        svn:
          uri: file:///path/to/svn/repo
          order: 2
        git:
          uri: file:///path/to/git/repo
          order: 1

3.7 By implementing EnvironmentRepository, customize configuration parameters
http://www.mamicode.com/info-detail-1784908.html

Reference documentation
http://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.0.0.RC1/single/spring-cloud-config.html
https://springcloud.cc/spring-cloud -config.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325947604&siteId=291194637