Below is the instruction to setup Postgres, Mongodb and Redis on Heroku:
Setup Postgres
heroku addons:add heroku-postgresql:dev
package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; import java.net.URI; /** * @author Thys Michels */ @Configuration @Profile("default") public class LocalDataSourceConfiguration implements DataSourceConfiguration { @Bean public DataSource dataSource() throws Exception { final URI dbUrl = new URI(System.getenv("DATABASE_URL")); final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://" + dbUrl.getHost() + dbUrl.getPath()); dataSource.setUsername(dbUrl.getUserInfo().split(":")[0]); dataSource.setPassword(dbUrl.getUserInfo().split(":")[1]); return dataSource; } }
Setup MongoDB
heroku addons:add mongohq:sandbox
package com.example.mongo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.core.env.Environment; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import com.mongodb.Mongo; import com.mongodb.MongoURI; @Configuration @Profile("default") public class LocalMongoConfiguration { @Bean public MongoDbFactory mongoDbFactory1(Environment environment) throws Exception { MongoURI mongoURI = new MongoURI(System.getenv("MONGOHQ_URL")); Mongo mongo = new Mongo(mongoURI); return new SimpleMongoDbFactory(mongo, mongoURI.getDatabase()); } }
Setup Redis
heroku addons:add rediscloud:20
package com.example.redis; import java.net.URI; import java.net.URISyntaxException; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import redis.clients.jedis.Protocol; @Configuration @Profile("default") public class LocalRedisConfiguration { @Bean public RedisConnectionFactory redisConnectionFactory() { try { URI redisUri = new URI(System.getenv("REDISCLOUD_URL")); JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(redisUri.getHost()); redisConnectionFactory.setPort(redisUri.getPort()); redisConnectionFactory.setTimeout(Protocol.DEFAULT_TIMEOUT); redisConnectionFactory.setPassword(redisUri.getUserInfo().split(":",2)[1]); return redisConnectionFactory; } catch (URISyntaxException e) { e.printStackTrace(); return null; } } }