Put your application in production

Here some simple tips to optimize your application for production.

Configure your application.conf

First off, the best way to specify production mode is to give a specific ID to your production framework. Let’s pick production as an example. Refer manage application.conf in several environments to see how.

Set the framework in prod mode:

%production.application.mode=prod

In this mode, the framework will pre-compile all Java sources and templates. If an error is found at this step, the application will not start. Source modifications will not be hot reloaded.

Define a real database:

If you have used a development database (either db=mem or db=fs), you should configure a more robust database engine:

%production.db.url=jdbc:mysql://localhost/prod
%production.db.driver=com.mysql.jdbc.Driver
%production.db.user=root
%production.db.pass=1515312

Disable JPA automatic schema update:

If you have used the automatic schema update feature provided by Hibernate, you should disable this feature for production.

For your production server, it’s usually a bad idea to let Hibernate automatically ALTER your production database’s schema and data…

The initial deployment is potentially a different issue. In this case only specify:

%production.jpa.ddl=create

Define a secure secret key:

The Play secret key is used to secure cryptographic functions, like the session signature. Your application must keep this key very secret.

%production.application.secret=c12d1c59af499d20f4955d07255ed8ea333

You can use the play secret command to generate a new secure and random key (at least on a ‘real’ OS). If you plan to distribute your application to several servers, remember to use the same key for all application instances!

Configure logging

For production it’s a good idea to use rolling log files. Do not send logging to the Console, since it will be written to the logs/system.out file and it will grow without bound!

Create a custom log4j.properties in the conf/ directory:

log4j.rootLogger=ERROR, Rolling

log4j.logger.play=INFO

# Rolling files
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=application.log
log4j.appender.Rolling.MaxFileSize=1MB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n

Set-up a front-end HTTP server

You can easily deploy your application as a stand-alone server by setting the application HTTP port to80:

%production.http.port=80

But if you plan to host several applications in the same server or load balance several instances of your application for scalability or fault tolerance, you can use a front-end HTTP server.

Note that using a front-end HTTP server will never give you better performance than using Play server directly!

Set-up with lighttpd

This example shows you how to configure lighttpd as a front-end web server. Note that you can do the same with Apache, but if you only need virtual hosting or load balancing, lighttpd is a very good choice and much easier to configure!

The /etc/lighttpd/lighttpd.conf file should define things like this:

server.modules = (
      "mod_access",
      "mod_proxy",
      "mod_accesslog"
)
...
$HTTP["host"] =~ "www.myapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" =>
        ( ( "host" => "127.0.0.1", "port" => 9000 ) ) )
}

$HTTP["host"] =~ "www.loadbalancedapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" => (
          ( "host" => "127.0.0.1", "port" => 9000 ),
          ( "host" => "127.0.0.1", "port" => 9001 ) )
    )
}

Set-up with Apache

The example below shows a simple set-up with Apache httpd server running in front of a standard Play configuration.

LoadModule proxy_module modules/mod_proxy.so
...
<VirtualHost *:80>
  ProxyPreserveHost On
  ServerName www.loadbalancedapp.com
  ProxyPass / http://127.0.0.1:9000/
  ProxyPassReverse / http://127.0.0.1:9000/
</VirtualHost>

Apache as a front proxy to allow transparent upgrade of your application

The basic idea is to run 2 Play instances of your web application and let the front-end proxy load-balance them. In case one is not available, it will forward all the requests to the available one.

Let’s start the same Play application two times: one on port 9999 and one on port 9998.

Copy the application 2 times and edit the application.conf in the conf directory to change the port numbers.

For each web application directory:

play start mysuperwebapp

Now, let’s configure our Apache web server to have a load balancer.

In Apache, I have the following configuration:

<VirtualHost mysuperwebapp.com:80>
  ServerName mysuperwebapp.com
  <Location /balancer-manager>
    SetHandler balancer-manager
    Order Deny,Allow
    Deny from all
    Allow from .mysuperwebapp.com
  </Location>
  <Proxy balancer://mycluster>
    BalancerMember http://localhost:9999
    BalancerMember http://localhost:9998 status=+H
  </Proxy>
  <Proxy *>
    Order Allow,Deny
    Allow From All
  </Proxy>
  ProxyPreserveHost On
  ProxyPass /balancer-manager !
  ProxyPass / balancer://mycluster/
  ProxyPassReverse / http://localhost:9999/
  ProxyPassReverse / http://localhost:9998/
</VirtualHost>

The important part is balancer://mycluster. This declares a load balancer. The +H option means that the second Play application is on stand-by. But you can also instruct it to load-balance.

Every time you want to upgrade mysuperwebapp, here is what you need to do:

play stop mysuperwebapp1

The load-balancer then forwards everything to mysuperwebapp2. In the meantime update mysuperwebapp1. Once you are done:

play start mysuperwebapp1

You can now safely update mysuperwebapp2.

Apache also provides a way to view the status of your cluster. Simply point your browser to /balancer-manager to view the current status of your clusters.

Because Play is completely stateless you don’t have to manage sessions between the 2 clusters. You can actually easily scale to more than 2 Play instances.

Advanced proxy settings

When using an HTTP frontal server, request addresses are seen as coming from the HTTP server. In a usual set-up, where you both have the Play app and the proxy running on the same machine, the Play app will see the requests coming from 127.0.0.1.

Proxy servers can add a specific header to the request to tell the proxied application where the request came from. Most web servers will add an X-Forwarded-For header with the remote client IP address as first argument. If you enable the forward support in your Play app configuration:

XForwardedSupport=127.0.0.1,10.0.0.25

Play will change the request.remoteAddress from the proxy’s IP to the client’s IP. You have to list the IP addresses of your proxy servers for this to work.

However, the host header is untouched, it’ll remain issued by the proxy. If you use Apache 2.x, you can add a directive like:

ProxyPreserveHost on

The host: header will be the original host request header issued by the client. By combining theses two techniques, your app will appear to be directly exposed.

Continuing the discussion

Next: Deployment options.

时间: 2024-10-21 16:25:42

Put your application in production的相关文章

[React] Configure a React &amp; Redux Application For Production Deployment and Deploy to Now

In this lesson, we’ll make a few small changes to our scripts and add some environment variables that will be used at build time to get our application ready to be deployed to a production environment using the now service. Once properly configured,

Manage application.conf in several environments

When you work in a team, different developers will use different configuration keys in theirapplication.conf. For example, the log level, some database configuration… This generally leads to recurrent conflicts when you commit the file using your VCS

技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)

一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这种自动配置的机制是怎样的呢?其实也并不复杂,让我们先以一种最简单的情况来了解实现原理和实现流程,然后就可以轻松对各种情况作出配置. 如果我们使用SAP系统,初始化库存一定必不可少.大家都知道初始化库存使用移动类型(movement type) 561/562.我们先以561 / 562的配置方法为例

angular2 step by step #1

Very general steps for new angualr2 developers: install node.js\npm check version node -v (7.2.0) npm -v (4.0.2) install angular-cli (npm install) latest: https://github.com/angular/angular-cli/releases check version ng -v (cli: 1.0.0-beta.21) Create

No-SQL之Redis

No-SQL之Redis 介绍 redis是一种基于内存存储的key-value高性能存储系统,类似memcached,但是redis支持丰富的数据结构类型,并且其还支持数据持久化到磁盘. Redis is a data structure server. It is open-source, networked, in-memory, and stores keys with optional durability. The development of Redis has been spon

WCF学习系列二---【WCF Interview Questions – Part 2 翻译系列】

http://www.topwcftutorials.net/2012/09/wcf-faqs-part2.html WCF Interview Questions – Part 2 This WCF tutorial is part-2 in series of WCF Interview Questions. Other parts in this series can be found here. 这是WCF问答系列教程中的第二部分,其他部分可以在下面的链接中找到: WCF Service

每天laravel-20160715|ConfirmableTrait

<?php namespace Illuminate\Console; use Closure; // namespace trait ConfirmableTrait {     /**      * Confirm before proceeding with the action.      *      * @param  string    $warning      * @param  \Closure|bool|null  $callback      * @return bool

SAP MM/FI 自动过账实现 OBYC 接口执行

一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这种自动配置的机制是怎样的呢?其实也并不复杂,让我们先以一种最简单的情况来了解实现原理和实现流程,然后就可以轻松对各种情况作出配置. 如果我们使用SAP系统,初始化库存一定必不可少.大家都知道初始化库存使用移动类型(movement type) 561/562.我们先以561 / 562的配置方法为例

Playframework项目启动后自动停止问题记录

问题:Playframework项目activator start启动后会自动停止 解决:activator start 添加签名(application.secret="oschina") 如:activator start -Dapplication.secret="oschina" 官网描述段落: The application secret Before you run your application in production mode, you nee