本文的示例代码参考env-springboot&env-laravel
目录
Spring Boot
- Startup
spring init -b 2.1.3.RELEASE -dweb --build gradle env-springboot && cd env-springboot
vim com/example/envspringboot/HelloWorldController.java
package com.example.envspringboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/helloWorld")
public String helloWorld() {
return "hello world";
}
}
./gradlew bootrun
curl localhost:8080/helloWorld
# hello world
- Dev
vim application.properties
# spring.profiles.active=dev
./gradlew bootrun
curl localhost:8080/helloWorld
# hello world
vim application-dev.properties
# server.port=5000
./gradlew bootrun
curl localhost:5000/helloWorld
# hello world
- Test
vim application.properties
# spring.profiles.active=test
vim application-test.properties
# server.port=5001
./gradlew bootrun
curl localhost:5001/helloWorld
# hello world
- Prod
vim application.properties
# spring.profiles.active=prod
vim application-prod.properties
# server.port=5002
./gradlew bootrun
curl localhost:5002/helloWorld
# hello world
application.properties用于存放公共配置 application-{stage}.properties用于存放环境配置
Laravel
- Startup
composer create-project laravel/laravel env-laravel --prefer-dist "5.5.*" && cd env-laravel
vim routes/web.php
<?php
Route::get('/helloWorld', function () {
return env('APP_ENV') . '=>' . env('DB_HOST') . ':' . env('DB_PORT');
});
vim .env
# DB_HOST=192.168.2.200
php artisan serve
curl localhost:8000/helloWorld
# local=>192.168.2.200:3306
- Local
cp .env .env.local
# DB_PORT=3307
php artisan serve --env=local
curl localhost:8000/helloWorld
# local=>192.168.2.200:3307
- Staging
cp .env .env.staging
# DB_PORT=3308
php artisan serve --env=staging
curl localhost:8000/helloWorld
# local=>192.168.2.200:3308
- Production
cp .env .env.production
# DB_PORT=3309
php artisan serve --env=production
curl localhost:8000/helloWorld
# local=>192.168.2.200:3309
.env.{stage}需要包含所有配置
说明
- Laravel Serve
# vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php
class LoadEnvironmentVariables
{
public function bootstrap(Application $app)
{
if ($app->configurationIsCached()) {
return;
}
$this->checkForSpecificEnvironmentFile($app);
(new Dotenv($app->environmentPath(), $app->environmentFile()))->load();
}
protected function checkForSpecificEnvironmentFile($app)
{
if ($app->runningInConsole() && ($input = new ArgvInput)->hasParameterOption('--env')) {
if ($this->setEnvironmentFilePath(
$app, $app->environmentFile().'.'.$input->getParameterOption('--env')
)) {
return;
}
}
if (! env('APP_ENV')) {
return;
}
$this->setEnvironmentFilePath(
$app, $app->environmentFile().'.'.env('APP_ENV')
);
}
}
# vendor/vlucas/phpdotenv/src/Loader.php
class Loader
{
public function load()
{
$this->ensureFileIsReadable();
$filePath = $this->filePath;
$lines = $this->readLinesFromFile($filePath);
foreach ($lines as $line) {
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
$this->setEnvironmentVariable($line);
}
}
return $lines;
}
}
- Laravel Cache
php artisan config:cache --env=production
php artisan serve --env=production
curl localhost:8000/helloWorld
# production.ERROR: No application encryption key has been specified.
- Laravel Nginx
vim nginx/conf/env-laravel.conf
server {
location / {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param APP_ENV production;
fastcgi_param SCRIPT_FILENAME /home/ubuntu/sites/laravel-tutorial/env-laravel/public/index.php;
}
}
curl 122.51.81.66/helloWorld
# production=>192.168.2.200:3309
总结
- Spring Boot
application.properties用于存放公共配置 application-{stage}.properties用于存放环境配置
application.properties中spring.profiles.active配置成相应{stage}
- Laravel
.env.{stage}需要包含所有配置
生成配置缓存添加--env={stage} nginx添加环境变量fastcgi_param APP_ENV {stage}
网友评论