Discovering MongoDB in PCF

Along with a simple RabbitMQ configuration, there is a slightly more complicated configuration for Reactive Data Storages. One of the conceptual differences between non-Reactive and Reactive Data Storages is a client or a driver. Non-Reactive clients and drivers are well integrated into the Spring ecosystem and battle-tested by many solutions. In contrast, Reactive Clients are still a novelty.

At the time of writing, PCF Version 2.2 does not provide out-of-the-box configuration for Reactive MongoDB (as well as for any other DB with reactive clients). Fortunately, using the Spring Cloud Connectors module, we may access the required information and configure Reactive MongoDB Client, as shown in the following example code:

@Configuration
@Profile("cloud")
public class CloudConfig extends AbstractCloudConfig {             // (1)
  ...
  @Configuration                                                   // (2)
  @ConditionalOnClass(MongoClient.class)                           //
  @EnableConfigurationProperties(MongoProperties.class)            //
  public class MongoCloudConfig
              extends MongoReactiveAutoConfiguration {
    ...
    @Bean
    @Override
    public MongoClient reactiveStreamsMongoClient(                 // (3)
      MongoProperties properties,
      Environment environment,
      ObjectProvider<List<MongoClientSettingsBuilderCustomizer>>
         builderCustomizers
    ) {
      List<ServiceInfo> infos = cloud()                            // (3.1)
        .getServiceInfos(MongoDbFactory.class);

      if (infos.size() == 1) {
         MongoServiceInfo mongoInfo = 
            (MongoServiceInfo) infos.get(0);
         properties.setUri(mongoInfo.getUri());                    // (3.2)
      }
      return super.reactiveStreamsMongoClient(                     // (3.3)
        properties,
        environment,
        builderCustomizers
      );
    }
  }
}

The following is a description of the code sample:

  1. This extension of the AbstractCloudConfig class gives access to the MongoDB location.
  2. This is a common approach to checking that the MongoClient is present in the classpath.
  3. This is the configuration of a reactive MongoClient bean, where at point (3.1) we get information about MongoDB from the cloud connector including connection URI (3.2). At (3.3) we create a new reactive stream MongoDB client by reusing the original logic.

Here, we extended a configuration class from org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration and dynamically customized MongoProperties with regards to available cloud() configurations.

After following Cloud Foundry instructions available at https://docs.run.pivotal.io/devguide/deploy-apps/deploy-app.html and appropriately configuring the MongoDB service, Storage Service should become available and streamed data should be stored in MongoDB.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.253.223