使用distillery 构建 phoenix 项目软件包

备注:

    首先需要安装 elixir 环境
 
1. 基本项目说明
参考项目:
https://github.com/rongfengliang/phoenix-rest-demo
2. 项目说明
备注: 主要是插件的配置

 mix.exs 

  defp deps do
    [
      {:phoenix, "~> 1.3.2"},
      {:phoenix_pubsub, "~> 1.0"},
      {:phoenix_ecto, "~> 3.2"},
      {:postgrex, ">= 0.0.0"},
      {:mariaex, "~> 0.8.4"},
      {:phoenix_html, "~> 2.10"},
      {:phoenix_live_reload, "~> 1.0", only: :dev},
      {:gettext, "~> 0.11"},
      {:cowboy, "~> 1.0"},
      {:distillery, "~> 1.5", runtime: false}  # 需要添加的构建包
    ]
  end
 
3. 使用
a. 初始化

首次使用需要进行环境的初始化

mix release.init

修改config/prod.exs

config :hello, Hello.Endpoint,
  # http: [:inet6, port: {:system, "PORT"}],
  http: [port: 4000],
  cache_static_manifest: "priv/static/cache_manifest.json",
  server: true  # 比较重要的参数,启动的时候需要
  
b. 构建打包


mix  release  or MIX_ENV=prod mix release (生产环境建议)

备注: 可以指定环境 dev、 prod (默认为dev),实际中我们需要添加 MIX_ENV=prod mix release

c. 使用生成的软件包

  Interactive: _build/prod/rel/hello/bin/hello console
  Foreground: _build/prod/rel/hello/bin/hello foreground
  Daemon: _build/prod/rel/hello/bin/hello start
4. 生成项目结构
生成的项目在 _build/prod/rel/hello ,实际使用docker 或者类似的工具进行运行

备注:默认prod 版本,运行时环境也会为我们打包,运行是比较简单的,可以参考3.c


.
├── bin  # 应用启动目录
├── erts-9.3  # 运行是环境
│   ├── bin
│   ├── include
│   │   └── internal
│   │       ├── gcc
│   │       ├── i386
│   │       ├── libatomic_ops
│   │       ├── ppc32
│   │       ├── pthread
│   │       ├── sparc32
│   │       ├── sparc64
│   │       ├── tile
│   │       ├── win
│   │       └── x86_64
│   ├── lib
│   │   └── internal
│   └── src
├── lib  # 依赖的包
│   ├── asn1-5.0.5
│   │   ├── ebin
│   │   └── priv
│   │       └── lib
│   ├── compiler-7.1.5
│   │   └── ebin
│   ├── connection-1.0.4
│   │   └── ebin
│   ├── cowboy-1.1.2
│   │   └── ebin
│   ├── cowlib-1.0.2
│   │   ├── ebin
│   │   └── include
│   ├── crypto-4.2.1
│   │   ├── ebin
│   │   └── priv
│   │       ├── lib
│   │       └── obj
│   ├── db_connection-1.1.3
│   │   └── ebin
│   ├── decimal-1.5.0
│   │   └── ebin
│   ├── ecto-2.2.10
│   │   └── ebin
│   ├── eex-1.6.4
│   │   └── ebin
│   ├── elixir-1.6.4
│   │   └── ebin
│   ├── gettext-0.15.0
│   │   └── ebin
│   ├── hello-0.0.1
│   │   ├── consolidated
│   │   ├── ebin
│   │   └── priv
│   │       ├── gettext
│   │       │   └── en
│   │       │       └── LC_MESSAGES
│   │       ├── repo
│   │       │   └── migrations
│   │       └── static
│   │           ├── css
│   │           ├── images
│   │           └── js
│   ├── iex-1.6.4
│   │   └── ebin
│   ├── kernel-5.4.3
│   │   ├── ebin
│   │   └── include
│   ├── logger-1.6.4
│   │   └── ebin
│   ├── mariaex-0.8.4
│   │   └── ebin
│   ├── mime-1.2.0
│   │   └── ebin
│   ├── phoenix-1.3.2
│   │   ├── ebin
│   │   └── priv
│   │       ├── static
│   │       └── templates
│   │           ├── phoenix.gen.channel
│   │           ├── phoenix.gen.html
│   │           ├── phoenix.gen.json
│   │           ├── phoenix.gen.model
│   │           ├── phx.gen.channel
│   │           ├── phx.gen.context
│   │           ├── phx.gen.embedded
│   │           ├── phx.gen.html
│   │           ├── phx.gen.json
│   │           ├── phx.gen.presence
│   │           └── phx.gen.schema
│   ├── phoenix_ecto-3.3.0
│   │   └── ebin
│   ├── phoenix_html-2.11.2
│   │   ├── ebin
│   │   └── priv
│   │       └── static
│   ├── phoenix_pubsub-1.0.2
│   │   └── ebin
│   ├── plug-1.5.0
│   │   └── ebin
│   ├── poison-3.1.0
│   │   └── ebin
│   ├── poolboy-1.5.1
│   │   └── ebin
│   ├── postgrex-0.13.5
│   │   └── ebin
│   ├── public_key-1.5.2
│   │   ├── ebin
│   │   └── include
│   ├── ranch-1.3.2
│   │   └── ebin
│   ├── runtime_tools-1.12.5
│   │   ├── ebin
│   │   ├── include
│   │   └── priv
│   │       └── lib
│   ├── sasl-3.1.1
│   │   └── ebin
│   ├── ssl-8.2.5
│   │   └── ebin
│   └── stdlib-3.4.5
│       ├── ebin
│       └── include
└── releases  # release 版本
    └── 0.0.1
        ├── commands
        ├── hooks
        │   ├── post_configure.d
        │   ├── post_start.d
        │   ├── post_stop.d
        │   ├── post_upgrade.d
        │   ├── pre_configure.d
        │   ├── pre_start.d
        │   ├── pre_stop.d
        │   └── pre_upgrade.d
        └── libexec
            └── commands
            
同时在  releases/0.0.1  会有一个tar.gz 的包,这个是直接可以拷贝运行的软件包
 
5. 总结
总的来说,还是比较简单的,实际使用中我们可以集成docker,很方便
6 . 运行截图
 
7 . 参考资料
https://github.com/bitwalker/distillery
https://hexdocs.pm/phoenix/deployment.html
https://github.com/rongfengliang/phoenix-rest-demo
posted @ 2022-06-16 09:20  孤独信徒  阅读(39)  评论(0编辑  收藏  举报