美文网首页
项目多环境配置

项目多环境配置

作者: 诺之林 | 来源:发表于2020-04-29 19:01 被阅读0次

本文的示例代码参考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}

References

相关文章

  • 项目多环境配置

    本文的示例代码参考env-springboot&env-laravel 目录 Spring Boot Larave...

  • 使用IntelliJ IDEA构建Spring Boot项目示例

    我们介绍新建Spring Boot项目的流程, 主要内容包含 创建项目 配置日志环境 使用配置文件 多环境下的配置...

  • Maven环境隔离

    maven环境隔离用于多环境配置问题,解决不同环境配置信息不同的问题,利于部署! 1、pom.xml配置 2、项目...

  • git配置多环境

    配置git多环境用户xxx: github上项目A用户yyy: gitlab上项目B

  • 项目中实现多环境配置+fastlane+fir+Jenkins总

    一、实现多环境配置 概述 在项目中,分了开发、测试、预生产、生产四个环境,之前切换环境,是在项目中的配置类中,用一...

  • vue-cli 多环境配置

    vue-cli 多环境配置 打包命令--mode xxx 用来区别是环境image 基本配置项目根目录下创建 .e...

  • Windows下CMD常用命令汇总

    控制台运行JAVA项目的方法(先将项目打包) 单环境时, 只有一个配置文件 运行命令: 多环境时, 有多个配置文件...

  • .NET Core配置多环境

    配置多环境是日常开发经常需要用到的操作,实现多环境配置后可以规避生产测试环境混合带来的麻烦和风险,减少项目风险,并...

  • VUE项目多环境配置.md

    web项目开发,通常需要有开发环境、测试环境和生产环境,用于配置不同的环境变更,如调用本地接口、测试接口和生产接口...

  • Spring Boot项目多环境配置

    任何项目都有多环境配置的需求,Spring Boot + maven的项目配置起来也很简单首先按照在resourc...

网友评论

      本文标题:项目多环境配置

      本文链接:https://www.haomeiwen.com/subject/rqigwhtx.html