简体   繁体   English

如果我不知道服务器名称和端口,如何在企业环境中设置grails.serverURL?

[英]How do I set grails.serverURL in an enterprise environment when I don't know the server name and port ahead of time?

We have an application in Grails 2.0 that we have working when we run on localhost:8080 . 我们在Grails 2.0中有一个可以在localhost:8080上运行的应用程序。 In Config.groovy , the application has a grails.serverURL property that has to be set for the current host and current app name. Config.groovy ,应用程序具有grails.serverURL属性,必须为当前主机和当前应用程序名称设置grails.serverURL属性。 However, in our QA environment, the middleware team takes the war we give them and deploys it to a server whose name and port we don't know ahead of time. 但是,在我们的QA环境中,中间件团队承受了我们给予他们的战争,并将其部署到我们事先不知道其名称和端口的服务器上。

It seems like Grails assumes that you are always going to register a domain name and then have full control over that name and the port the app is running, which is not the case at all. 看来Grails假设您总是要注册一个域名,然后完全控制该名称和应用程序正在运行的端口,而事实并非如此。

What is the best way to address this problem? 解决此问题的最佳方法是什么? We have tried simply removing grails.serverURL , but Spring Security appears to use it for building up the url for redirects on login success and logout success. 我们只是尝试删除grails.serverURL ,但是Spring Security似乎使用它来构建用于成功登录和注销成功的重定向的url。

development { grails.serverURL = "http://www.notused.com/${appName}" }
production { grails.serverURL = "<we don't know yet>" }

And before anyone asks, I have read numerous posts on StackOverflow and elsewhere that relate to this issue but do not answer this specific question. 在有人问之前,我已经阅读了关于StackOverflow和与该问题有关的其他地方的大量文章,但没有回答这个特定问题。

No, Spring Security doesn't use it. 不,Spring Security不使用它。 It should always be safe to remove the serverURL property and let Grails generate urls relative to the current running app. 删除serverURL属性并让Grails生成相对于当前正在运行的应用程序的url应该总是安全的。

Typically the only place you need this property is when you're generating emails and want to embed the server url so users can click back to your site. 通常,您唯一需要此属性的位置是在生成电子邮件并希望嵌入服务器URL时,以便用户可以单击返回到您的站点。 Since these are often sent asynchronously and not during a server request, there's no way to know the server address without a config option. 由于这些通常是异步发送的,而不是在服务器请求期间发送的,因此如果没有config选项,就无法知道服务器地址。 But if you're not doing that, it's safe to omit. 但是,如果您不这样做,则可以忽略。

Grails has a built-in mechanism called Externalized Configuration that allows you to one or more optional Config.groovy files outside your application. Grails具有一种称为“ 外部化配置”的内置机制,该机制使您可以在应用程序之外访问一个或多个可选Config.groovy文件。

You add something like this to your Config.groovy : 您可以在Config.groovy添加以下内容:

grails.config.locations = [
    "classpath:${appName}-config.properties",
    "classpath:${appName}-config.groovy",
    "file:${userHome}/.grails/${appName}-config.properties",
    "file:${userHome}/.grails/${appName}-config.groovy" ]

Then include in any one of those files on your server this content: 然后,在服务器上的任何一个文件中包含以下内容:

grails.serverURL = "http://example.com/"

The external config will override the base config. 外部配置将覆盖基本配置。

Externalize the serverURL into a property that can be set by your Middleware team. 将serverURL外部化为可由您的中间件团队设置的属性。 For example, place in a config.properties file and have it as part of your property config for Spring. 例如,放置在config.properties文件中,并将其作为Spring的属性配置的一部分。 It's generally a best practice to have properties externalized so you can pass the WAR around and have it deployed and configured without needing to recompile and repackage. 通常,将属性外部化是最佳实践,因此您可以传递WAR并进行部署和配置,而无需重新编译和重新打包。

See here for better example: https://stackoverflow.com/a/973375/463226 参见此处以获得更好的示例: https : //stackoverflow.com/a/973375/463226

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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