Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring cloud config

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

What is the purpose of config?

The configuration files of microservices in cloud are centralized externally and managed uniformly. Especially in a service load, the centralized management of configuration will be very convenient.

The use of spring cloud config

* config client

1.dependcy

Org.springframework.cloud spring-cloud-starter-config 1.3.1.RELEASE

two。 Import bootstrap.yml profile _ specify config_server_ip

The purpose of this file is to load these configurations when application starts. It is the parent context of applicationContext, while the application.yml configuration file corresponds to applicationContext. The configuration of bootstrap.yml will be the default configuration parameter of application, and can be set to be override by the same parameter of 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 to determine whether the parameter is allowed to be overridden.

The configuration {application} name of the spring.cloud.config.name microservice. If it is not set, it defaults to 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 means failure to start directly if the connection is not available.

Spring.cloud.config.profile refers to which profile is currently in use, usually dev, test, pro

Spring.cloud.config.usernam connection user name

Spring.cloud.config.password connection password

Interval between spring.cloud.config.retry.inital-interval failed reconnection

Maximum interval between spring.cloud.config.retry.max-interval failed reconnection

Maximum number of failed reconnections of spring.cloud.config.retry.max-attempts

2.2 introduction of bootstrap.yml profile _ configure configserver through Eureka service discovery

Spring.application.name=spring-data-demoserver.port=8081spring.cloud.config.name=spring-data-demospring.cloud.config.profile=devspring.cloud.config.label=masterspring.cloud.config.fail-fast=truespring.cloud.config.allow-override=true#spring.cloud.config.uri= http://localhost:18001spring.cloud.config.retry.initial-interval=1000spring.cloud.config.retry.max-attempts=6spring.cloud.config.retry.max-interval=2000spring.cloud.config.retry.multiplier=1.1spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=spring- Config-demoeureka.instance.appname=spring-data-demoeureka.instance.app-group-name=dataeureka.instance.prefer-ip-address=trueeureka.instance.ip-address=127.0.0.1eureka.instance.instance-id= ${spring.application.name}: ${server.port} # eureka.instance.home-page-url-path=/#eureka.instance.health-check-url-path=/health#eureka.instance.status-page-url-path=/statuseureka.instance.lease-expiration-duration-in-seconds=30eureka.instance.lease-renewal- Interval-in-seconds=10eureka.client.serviceUrl.defaultZone= http://localhost:19002/eureka/, Http://localhost:19001/eureka/

Note: since bootstrap.yml starts before application.properties, that's why configserver is configured here (files introduced through application can directly use the parameters of configserver). Because configserver is discovered through the Eureka service, you also need to write the configuration information for Eureka here. If you want to configure the above, you can put the configuration information of eureka independently.

3. Use of remote profile

It is easy to use, it can be introduced through ${} in configuration files such as application.yml, or the configuration parameters of application.yml can be used by java code directly through annotations. For example:

Configuration parameters of web-demo-test.yml in config server:

# sys configserver: port: 8081#business configuser: username: yangyc password: 123456

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

Server: port: ${server.port} # business configuser: username: ${user.username} password: ${user.password}

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

* config server

1.depandcy

Org.springframework.cloud spring-cloud-config-server

two。 Add @ EnableConfigServer annotation at startup

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

Classification of 3.config backend storage

3.1 native local storage

Spring.application.name=config-demoserver.port=8888spring.profiles.active=nativespring.cloud.config.server.native.search-locations=classpath:/config/ {application} spring.cloud.config.server.native.add-label-locations=falsespring.cloud.config.server.accept-empty=falselogging.level.root=debug

Spring.profiles.active: type of configuration file. Native local file; jdbc/svn/git/vault.

Spring.cloud.config.server.native.search-locations: local file path. It can be a disk file or a classpath.

Note: placeholders {application}, {profiles}, {label} can be used in path. That is, the corresponding directory can be created in the file directory, so as to achieve the effect of dynamic adaptation.

Spring.cloud.config.server.native.add-label-locations: indicates whether the configuration file directory includes label.

Spring.cloud.config.server.accept-empty: indicates whether getting empty data is allowed.

3.2 JDBC Stora

Increase dependence

Org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java

Configure the data source

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

Configure config server

Spring.profiles.active=jdbcspring.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 building

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

3.3 SVN Stora

Increase dependence

Org.tmatesoft.svnkit svnkit 1.9.2 spring.profiles.active=subversionspring.cloud.config.server.svn.uri=spring.cloud.config.server.svn.username=spring.cloud.config.server.svn.password=spring.cloud.config.server.svn.default-label=configspring.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 address of the SVN warehouse

Spring.cloud.config.server.svn.username user name

Spring.cloud.config.server.svn.password password

Spring.cloud.config.server.svn.default-label must also be configured with Git, which refers 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 acquisition address of the local warehouse

3.4 git Stora

Spring.profiles.active=gitspring.cloud.config.server.git.basedir=spring.cloud.config.server.git.clone-on-start=falsespring.cloud.config.server.git.force-pull=truespring.cloud.config.server.git.host-key=spring.cloud.config.server.git.host-key-algorithm=spring.cloud.config.server.git.delete-untracked-branches=truespring.cloud.config.server.git.ignore-local-ssh-settings=truespring.cloud.config.server.git.known-hosts-file=spring.cloud.config.server.git. Order=1spring.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=truespring.cloud.config.server.git.timeout=5spring.cloud.config.server.git.uri=spring.cloud.config.server.git.username=

Spring.cloud.config.server.git.basedir: for example, SVN/GIT will first transfer the file check out / clone in remote to local, default is / temp temporary directory, but some operating systems will delete temporary files, so you need to modify the corresponding directory. And this configuration file is the address of the configuration local library.

Spring.cloud.config.server.git.force-pul: refers to 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: user name

Spring.cloud.config.server.git.password: password

Under the spring.cloud.config.server.git.search-paths:uri subdirectory, you can use the 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 Stora

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

3.6 multiple types of compound 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 Custom get configuration parameters by implementing EnvironmentRepository

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report