将JHipster/MongoDB应用程序部署到Heroku [英] Deploying a JHipster/MongoDB application to Heroku
问题描述
JHipster提供了用于将应用程序部署到云提供商的子生成器(例如Heroku和Openshift),但由于使用
JHipster offers sub-generators for deploying applications to cloud providers (such as Heroku and Openshift), but not for applications using MongoDB due to the fact that Mongeez requires admin privileges which of course is not possible within a PaaS environment.
但是,只要停用Mongeez,就应该可以在云提供商上运行JHipster + MongoDB.我做了以下操作(使用Heroku):
However, it should be possible to run JHipster + MongoDB on cloud providers as long as Mongeez is deactivated. I did the following (using Heroku):
- 首先,我在本地开发计算机上手动导出了由JHipster创建的MongoDB数据库,并将其导入了Mongolab实例.
- 如果使用的配置文件为
prod
,则不会实例化Mongeez. - 在对JHipster的 Heroku子生成器进行了一些修改之后,将应用程序部署到Heroku.现在,生成器将忽略
_HerokuDatabaseConfiguration.java
(用于JDBC),并在使用的Procfile中,将配置文件更改为prod
(而不是prod,heroku
),并删除了参数--spring.datasource.heroku-url=$DATABASE_URL
.
- First, I manually exported the MongoDB database that was created by JHipster on my local development machine and imported it on a Mongolab instance.
- In case the used profile is
prod
, Mongeez doesn't get instantiated. - After some modifications to JHipster's Heroku subgenerator, it was possible to deploy the app to Heroku. The generator now ignores
_HerokuDatabaseConfiguration.java
(which is for JDBC) and in the used Procfile, I changed the profile toprod
(instead ofprod,heroku
) and removed the parameter--spring.datasource.heroku-url=$DATABASE_URL
.
它工作得很好,但是我在注入MongoDB连接字符串时遇到了问题.目前,凭据已硬编码"到application-prod.yml
中,因此,在更改凭据的情况下,有必要重新部署整个应用程序.在另一种尝试中,我向Procfile添加了spring.data.mongodb.uri=$MONGOLAB_URI
,但是没有任何效果(除非我错过了什么).
It works quite well, but I have problems with injecting the MongoDB connection string. At the moment, the credentials are "hardcoded" into application-prod.yml
, so it would be necessary to redeploy the whole application in case of a change of the credentials. In another attempt, I added spring.data.mongodb.uri=$MONGOLAB_URI
to the Procfile, but there wasn't any effect (unless I missed something).
那么我该如何解决这个问题?就像我说的那样,该应用程序在Heroku上运行没有任何问题,但是最好从(例如)获取连接详细信息. MONGOLAB_URI环境变量.在其他新闻中,我有一种感觉,我不了解cloud
配置文件(目前不使用该配置文件).
So how could I solve this issue? As I said, the application is running on Heroku without any problems, but it would be nice to derive the connection details from e.g. the MONGOLAB_URI environment variable. In other news, I have a feeling that I don't understand the cloud
profile (which I do not use at the moment).
推荐答案
您可以使用 mongobee 为您的应用提供迁移逻辑.
Instead of Mongeez you can use mongobee to provide the migration logic for your app.
我已经尝试过了,并且它可以在heroku上运行.
I've tried it and it works on heroku.
这是我默认的jhipster mongobee迁移代码,其作用与mongeez相同.
This is my default mongobee migration code for jhipster that has the same effect as the mongeez one.
package your.package.name.config.dbmigrations;
import com.github.mongobee.changeset.ChangeLog;
import com.github.mongobee.changeset.ChangeSet;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Creates the initial database setup
*/
@ChangeLog(order = "001")
public class InitialSetupMigration {
private Map<String, String>[] authoritiesUser = new Map[]{new HashMap<>()};
private Map<String, String>[] authoritiesAdminAndUser = new Map[]{new HashMap<>(), new HashMap<>()};
{
authoritiesUser[0].put("_id", "ROLE_USER");
authoritiesAdminAndUser[0].put("_id", "ROLE_USER");
authoritiesAdminAndUser[1].put("_id", "ROLE_ADMIN");
}
@ChangeSet(order = "01", author = "initiator", id = "01-addAuthorities")
public void addAuthorities(DB db) {
DBCollection authorityCollection = db.getCollection("jhi_authority");
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_ADMIN")
.get());
authorityCollection.insert(
BasicDBObjectBuilder.start()
.add("_id", "ROLE_USER")
.get());
}
@ChangeSet(order = "02", author = "initiator", id = "02-addUsers")
public void addUsers(DB db) {
DBCollection usersCollection = db.getCollection("jhi_user");
usersCollection.createIndex("login");
usersCollection.createIndex("email");
usersCollection.insert(BasicDBObjectBuilder.start()
.add("_id", "user-0")
.add("login", "system")
.add("password", "$2a$10$mE.qmcV0mFU5NcKh73TZx.z4ueI/.bDWbj0T1BYyqP481kGGarKLG")
.add("first_name", "")
.add("last_name", "System")
.add("email", "system@localhost")
.add("activated", "true")
.add("lang_key", "en")
.add("created_by", "system")
.add("created_date", new Date())
.add("authorities", authoritiesAdminAndUser)
.get()
);
usersCollection.insert(BasicDBObjectBuilder.start()
.add("_id", "user-1")
.add("login", "anonymousUser")
.add("password", "$2a$10$j8S5d7Sr7.8VTOYNviDPOeWX8KcYILUVJBsYV83Y5NtECayypx9lO")
.add("first_name", "Anonymous")
.add("last_name", "User")
.add("email", "anonymous@localhost")
.add("activated", "true")
.add("lang_key", "en")
.add("created_by", "system")
.add("created_date", new Date())
.add("authorities", new Map[]{})
.get()
);
usersCollection.insert(BasicDBObjectBuilder.start()
.add("_id", "user-2")
.add("login", "admin")
.add("password", "$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC")
.add("first_name", "admin")
.add("last_name", "Administrator")
.add("email", "admin@localhost")
.add("activated", "true")
.add("lang_key", "en")
.add("created_by", "system")
.add("created_date", new Date())
.add("authorities", authoritiesAdminAndUser)
.get()
);
usersCollection.insert(BasicDBObjectBuilder.start()
.add("_id", "user-3")
.add("login", "user")
.add("password", "$2a$10$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K")
.add("first_name", "")
.add("last_name", "User")
.add("email", "user@localhost")
.add("activated", "true")
.add("lang_key", "en")
.add("created_by", "system")
.add("created_date", new Date())
.add("authorities", authoritiesUser)
.get()
);
}
@ChangeSet(author = "initiator", id = "03-addSocialUserConnection", order = "03")
public void addSocialUserConnection(DB db) {
DBCollection socialUserConnectionCollection = db.getCollection("jhi_social_user_connection");
socialUserConnectionCollection.createIndex(BasicDBObjectBuilder
.start("user_id", 1)
.add("provider_id", 1)
.add("provider_user_id", 1)
.get(),
"user-prov-provusr-idx", true);
}
}
在DatabaseConfiguration.java
文件中,删除mongeez并添加
In the DatabaseConfiguration.java
file remove mongeez and add
@Bean
public Mongobee mongobee() {
log.debug("Configuring Mongobee");
Mongobee mongobee = new Mongobee(mongo);
mongobee.setDbName(mongoProperties.getDatabase());
mongobee.setChangeLogsScanPackage(
"de.shaere.sharecore.config.dbmigrations"); // package to scan for changesets
mongobee.setEnabled(true);
return mongobee;
}
最后更新您的gradle文件,删除mongeez依赖项并添加:
And finally update your gradle file removing the mongeez dependency and adding:
compile group: 'com.github.mongobee', name: 'mongobee', version: mongobee_version
我还打开了拉请求,以更新JHipster生成器.现在,我们拭目以待,看看这些家伙是否同意:)
I've also opened a pull request to update the JHipster generator. Now we wait to see if the guys agree :)
这篇关于将JHipster/MongoDB应用程序部署到Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!