Files
spring-cloud-bindings/README.md
Ben Hale aa6f50e5c3 Global Guard
Previously, whenever the library was included on a classpath it would attempt
to auto-configure Spring Boot.  Since part of the API can be used as a
straight library, and in that usage, auto-configuration would not be
desirable, the auto-configuration needed a global off switch.  This change
adds a global guard, disabled by default, in the
org.springframework.cloud.bindings.boot.enable System Property.

[resolves #25]

Signed-off-by: Ben Hale <bhale@vmware.com>
2020-05-11 10:42:47 -07:00

4.8 KiB

Spring Cloud Bindings

The Spring Cloud Bindings library exposes a rich Java language binding for the Cloud Native Buildpacks Binding Specification. In addition, if opted-in, it configures Spring Boot application configuration properties appropriate for the kind of binding encountered.

Library Usage

While less used, explicit binding access can be achieved through the Bindings type.

List<Binding> bindings = new Bindings().filterBindings("MySQL");
if (bindings.size() > 0) {
    Map<String, String> secret = bindings.get(0).getSecret();

    MariaDbDataSource dataSource = new MariaDbDataSource();
    dataSource.setServerName(secret.get("host"));
    dataSource.setPort(Integer.parseInt(secret.get("port")));
    dataSource.setDatabaseName(secret.get("database"));
    dataSource.setUserName(secret.get("username"));
    dataSource.setPassword(secret.get("password"));

    return dataSource;
}

Spring Boot Configuration

The more common usage of the library is opt-in automatic Spring Boot configuration. Setting the org.springframework.cloud.bindings.boot.enabled=true System Property results in the following:

  • Adds a PropertySource with a flattened representation (cnb.bindings.{name}.{metadata,secret}.*) of the bindings.
  • Adds a PropertySource with binding-specific Spring Boot configuration properties.

Auto-Configurations

Each auto-configuration is triggered by the kind of binding. Each auto-configuration can be disabled using a System Property specific to that kind and defaults to enable. Auto-configuration is disabled by default and can be enabled by setting the org.springframework.cloud.bindings.boot.enable System Property to true.

Cassandra

Kind: cassandra Disable Property: org.springframework.cloud.bindings.boot.cassandra.enable

Property Value
spring.data.cassandra.contact-points {secret.node_ips}
spring.data.cassandra.password {secret.password}
spring.data.cassandra.port {secret.port}
spring.data.cassandra.username {secret.username}

DB2 RDBMS

Kind: DB2 Disable Property: org.springframework.cloud.bindings.boot.db2.enable

Property Value
spring.datasource.driver-class-name com.ibm.db2.jcc.DB2Driver
spring.datasource.password {secret.password}
spring.datasource.url jdbc:db2://{secret.host}:{secret.port}/{secret.database}
spring.datasource.username {secret.username}

MongoDB

Kind: MongoDB Disable Property: org.springframework.cloud.bindings.boot.mongodb.enable

Property Value
spring.mongodb.uri {secret.uri}

MySQL RDBMS

Kind: MySQL Disable Property: org.springframework.cloud.bindings.boot.mysql.enable

Property Value
spring.datasource.driver-class-name org.mariadb.jdbc.Driver or com.mysql.cj.jdbc.Driver depending on classpath
spring.datasource.password {secret.password}
spring.datasource.url jdbc:mysql://{secret.host}:{secret.port}/{secret.database}
spring.datasource.username {secret.username}

Oracle RDBMS

Kind: Oracle Disable Property: org.springframework.cloud.bindings.boot.oracle.enable

Property Value
spring.datasource.driver-class-name oracle.jdbc.OracleDriver
spring.datasource.password {secret.password}
spring.datasource.url jdbc:oracle://{secret.host}:{secret.port}/{secret.database}
spring.datasource.username {secret.username}

PostgreSQL RDBMS

Kind: PostgreSQL Disable Property: org.springframework.cloud.bindings.boot.postgresql.enable

Property Value
spring.datasource.driver-class-name org.postgresql.Driver
spring.datasource.password {secret.password}
spring.datasource.url jdbc:postgres://{secret.host}:{secret.port}/{secret.database}
spring.datasource.username {secret.username}

Redis RDBMS

Kind: Redis Disable Property: org.springframework.cloud.bindings.boot.redis.enable

Property Value
spring.datasource.host {secret.host}
spring.datasource.password {secret.password}
spring.datasource.port {secret.port}

Oracle RDBMS

Kind: SQLServer Disable Property: org.springframework.cloud.bindings.boot.sqlserver.enable

Property Value
spring.datasource.driver-class-name com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.password {secret.password}
spring.datasource.url jdbc:sqlserver://{secret.host}:{secret.port}/{secret.database}
spring.datasource.username {secret.username}

License

This buildpack is released under version 2.0 of the Apache License.