简体   繁体   中英

Spring Boot and how to configure connection details to MongoDB on cloud foundry?

I am currently creating a spring boot web application using mongodb. Locally I am able to run my application and data are being retrieved and stored. but not on cloud foundry. Any tips, links or anything to help me will be greatly appreciated!

Problem

When I pushed it into cloud foundry, I am unable to connect to my mongodb. I know that we have to configure something on cloud foundry to make it work. I tried using the mongodb PCF, even when I bind it to my application it is still not running my application. I have been scouring through the internet trying to find the correct configurations to do so.

Other thoughts

I never set up an username and password when I was using MongoDb. I am currently just using localhost with port27017. Maybe I need to configure the credentials on my V_CAP or env varibles, But I cannot edit those on the UI of cloudfoundry.com. Would there be a command line prompt to configure those varibles into cf? I check out several documents but I am unclear where to add these beans into which configuration file or would it help get mongodb running on the cloud foundry Doc1

Error of connection on cloud foundry

OUT org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexController': Unsatisfied dependency expressed through field 'apL'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appPortList': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]

Theoretically, Spring boot cloud connectors should do this for you, but I have had problems with it in the past.

Alternatively, you need to add a spring bean extending AbstractMongoConfiguration. In this configuration you must insert the host, port, username, database name, and password you find in the VCAP_SERVICES environment variable.

You could automatically insert this by getting the value of the VCAP_SERVICES variable using System.getEnv("VCAP_SERVICES"), and parse it with JACKSON, or some other JSON library.

@Configuration
@Profile("cloud")
public class DatabaseConfiguration extends AbstractMongoConfiguration {

    @Bean
    public ValidatingMongoEventListener validatingMongoEventListener() {
        return new ValidatingMongoEventListener(validator());
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

    @Override
    public String getDatabaseName() {
        return this.databaseName;
    }

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
        ServerAddress address = new ServerAddress("my_host", 7777);
        serverAddresses.add(address);
        return new MongoClient(serverAdresses,
                singletonList(
                        MongoCredential.createCredential(
                                "my_user_name",
                                "my_database_name",
                                "my_password".toCharArray()
                        )
                )
        );
    }
}

Regarding your "other thoughts". There is no way of editing the VCAP_SERVICES variable provided by CloudFoundry. Instead it should be read by your application, such that your application knows where to find the mongodb instance that CF has provided for it.

You can, however, add more environment variables to your application using 'cf set-env'. Yet this will not help to solve your problem as far as I can see.

There is a really good blog post about binding to data services on CF on the Spring Framework blog: https://spring.io/blog/2015/04/27/binding-to-data-services-with-spring-boot-in-cloud-foundry .

This post uses relational with JPA in its examples, but everything shown works for MongoDB also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM