1

I am trying to deploy a spring boot application connected to a mongodb instance to cloud foundry.

cf create-service MongoService default my-mongo
cf push myapp --no-start
cf bind-service myapp my-mongo
cf start myapp

The connection details to the mongodb instance are in the VCAP_SERVICES environment variable. When deploying my application to cloudfoundry spring boot is trying to access mongodb on localhost:27017 and obviously fails.

I would like to parse the VCAP_SERVICES environment variable, construct some mongodb connection details from it and provide this as a spring bean. Which class should I use for these configuration details?

2
  • I have solved the issue by registering a Configuration class that extends AbstractMongoConfiguration. In this class the vcap_services environment variable is parsed, and inserted into the Mongo object returned by the override mongo() method. Commented Jun 2, 2016 at 12:30
  • Please show more details how you fixed it, I am having the same problem. Commented Dec 21, 2016 at 16:10

2 Answers 2

1

With Spring Boot, you don't need to manually parse VCAP_SERVICES. If you are using MongoTemplate or MongoRepository, it will automatically connect to the bound instance.

Make sure that you have spring-boot-starter-parent identified as the parent artifact in your pom.xml.

You can add the following to your pom.xml to ensure that the cloud connector code is getting picked up:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cloud-connectors</artifactId>
    </dependency>

Of course, you also need the MongoDB Spring Data dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried exactly this, but the app still tries to connect to localhost:27017.
When adding this to the POM, do I need to add any other annotations to make this work? I am having the same problem as user152469
Be sure you have included the mongodb data dependency. groupId:org.springframework.boot artifactId:spring-boot-starter-data-mongodb
0

You may want to try setting up the configuration parameters, like a workaround I had to implement with MySQL, check this question.

For me it all came down to properly defining following properties:

spring:
  datasource:
    url: jdbc:mysql://${vcap.services.mydb.credentials.host}:${vcap.services.mydb.credentials.port}/${vcap.services.mydb.credentials.name}
    driver-class-name: com.mysql.jdbc.Driver
    username: ${vcap.services.mydb.credentials.user}
    password: ${vcap.services.mydb.credentials.password}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.