Sharing Configuration With All Applications

Sharing configuration between all applications varies according to which approach you take, as described in the following topics:

File Based Repositories

Vault Server

File Based Repositories

With file-based (git, svn, and native) repositories, resources with file names in application* (application.properties, application.yml, application-*.properties, and so on) are shared between all client applications. You can use resources with these file names to configure global defaults and have them be overridden by application-specific files as necessary.

The property overrides feature can also be used for setting global defaults, with placeholders applications allowed to override them locally.
With the “native” profile (a local file system backend) , you should use an explicit search location that is not part of the server’s own configuration. Otherwise, the application* resources in the default search locations get removed because they are part of the server.
Vault Server

When using Vault as a backend, you can share configuration with all applications by placing configuration in secret/application. For example, if you run the following Vault command, all applications using the config server will have the properties foo and baz available to them:

$ vault write secret/application foo=bar baz=bam

CredHub Server

When using CredHub as a backend, you can share configuration with all applications by placing configuration in /application/ or by placing it in the default profile for the application. For example, if you run the following CredHub command, all applications using the config server will have the properties shared.color1 and shared.color2 available to them:

credhub set --name “/application/profile/master/shared” --type=json
value: {“shared.color1”: “blue”, “shared.color”: “red”}

credhub set --name “/my-app/default/master/more-shared” --type=json
value: {“shared.word1”: “hello”, “shared.word2”: “world”}

JDBC Backend

Spring Cloud Config Server supports JDBC (relational database) as a backend for configuration properties. You can enable this feature by adding spring-jdbc to the classpath and using the jdbc profile or by adding a bean of type JdbcEnvironmentRepository. If you include the right dependencies on the classpath (see the user guide for more details on that), Spring Boot configures a data source.

The database needs to have a table called PROPERTIES with columns called APPLICATION, PROFILE, and LABEL (with the usual Environment meaning), plus KEY and VALUE for the key and value pairs in Properties style. All fields are of type String in Java, so you can make them VARCHAR of whatever length you need. Property values behave in the same way as they would if they came from Spring Boot properties files named {application}-{profile}.properties, including all the encryption and decryption, which will be applied as post-processing steps (that is, not in the repository implementation directly).
Redis Backend

Spring Cloud Config Server supports Redis as a backend for configuration properties. You can enable this feature by adding a dependency to Spring Data Redis.
pom.xml

org.springframework.boot spring-boot-starter-data-redis

The following configuration uses Spring Data RedisTemplate to access a Redis. We can use spring.redis.* properties to override default connection settings.

spring:
profiles:
active: redis
redis:
host: redis
port: 16379

The properties should be stored as fields in a hash. The name of hash should be the same as spring.application.name property or conjunction of spring.application.name and spring.profiles.active[n].

HMSET sample-app server.port “8100” sample.topic.name “test” test.property1 “property1”

After executing the command visible above a hash should contain the following keys with values:

HGETALL sample-app
{
“server.port”: “8100”,
“sample.topic.name”: “test”,
“test.property1”: “property1”
}

When no profile is specified default will be used.

AWS S3 Backend

Spring Cloud Config Server supports AWS S3 as a backend for configuration properties. You can enable this feature by adding a dependency to the AWS Java SDK For Amazon S3.
pom.xml

com.amazonaws aws-java-sdk-s3

The following configuration uses the AWS S3 client to access configuration files. We can use spring.awss3.* properties to select the bucket where your configuration is stored.

spring:
profiles:
active: awss3
cloud:
config:
server:
awss3:
region: us-east-1
bucket: bucket1

It is also possible to specify an AWS URL to override the standard endpoint of your S3 service with spring.awss3.endpoint. This allows support for beta regions of S3, and other S3 compatible storage APIs.

Credentials are found using the Default AWS Credential Provider Chain. Versioned and encrypted buckets are supported without further configuration.

Configuration files are stored in your bucket as {application}-{profile}.properties, {application}-{profile}.yml or {application}-{profile}.json. An optional label can be provided to specify a directory path to the file.
When no profile is specified default will be used.
CredHub Backend

Spring Cloud Config Server supports CredHub as a backend for configuration properties. You can enable this feature by adding a dependency to Spring CredHub.
pom.xml

org.springframework.credhub spring-credhub-starter

The following configuration uses mutual TLS to access a CredHub:

spring:
profiles:
active: credhub
cloud:
config:
server:
credhub:
url: https://credhub:8844

The properties should be stored as JSON, such as:

credhub set --name “/demo-app/default/master/toggles” --type=json
value: {“toggle.button”: “blue”, “toggle.link”: “red”}

credhub set --name “/demo-app/default/master/abs” --type=json
value: {“marketing.enabled”: true, “external.enabled”: false}

All client applications with the name spring.cloud.config.name=demo-app will have the following properties available to them:

{
toggle.button: “blue”,
toggle.link: “red”,
marketing.enabled: true,
external.enabled: false
}

When no profile is specified default will be used and when no label is specified master will be used as a default value. NOTE: Values added to application will be shared by all the applications.

OAuth 2.0

You can authenticate with OAuth 2.0 using UAA as a provider.
pom.xml

org.springframework.security spring-security-config org.springframework.security spring-security-oauth2-client

The following configuration uses OAuth 2.0 and UAA to access a CredHub:

spring:
profiles:
active: credhub
cloud:
config:
server:
credhub:
url: https://credhub:8844
oauth2:
registration-id: credhub-client
security:
oauth2:
client:
registration:
credhub-client:
provider: uaa
client-id: credhub_config_server
client-secret: asecret
authorization-grant-type: client_credentials
provider:
uaa:
token-uri: https://uaa:8443/oauth/token

The used UAA client-id should have credhub.read as scope. 
发布了0 篇原创文章 · 获赞 135 · 访问量 5235

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105191351