您的位置:首页 > 运维架构 > Docker

使用Docker创建Hyperf项目

2020-01-15 09:09 375 查看

文章目录

  • 数据表设计
  • 安装Hyperf开发容器

    docker run -d --name user_center \
    --restart=always \
    #映射到宿主机目录,这样我们就直接在/home/wwwroot/user_center开发
    -v /home/wwwroot/user_center:/hyperf-skeleton \
    # 9501提供http服务,9504提供json-rpc服务
    -p 9501:9501 -p 9504:9504 \
    -it --entrypoint /bin/sh \
    hyperf/hyperf:7.3-alpine-cli

    安装Composer

    docker exec -it user_center bash #进入容器
    #下载COMPOSER
    wget https://github.com/composer/composer/releases/download/1.9.0/composer.phar
    #修改为可执行
    chmod u+x composer.phar
    #复制到/usr/local/bin/ 这样就可以直接运行composer 命令
    mv composer.phar /usr/local/bin/composer
    #修改仓库地址为阿里云
    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer

    创建项目,以下命令都是在容器内部执行

    cd /hyperf-skeleton
    composer create-project hyperf/hyperf-skeleton=1.1.* #直接一路回车不安装附加组件

    安装需要用到的组件

    composer require illuminate/hashing gregwar/captcha \ hyperf/validation hyperf/translation hyperf/constants phper666/jwt-auth:~2.0.1 hyperf/config-aliyun-acm hyperf/json-rpc hyperf/rpc-server

    illuminate/hashing laravel的hash组件
    gregwar/captcha 验证码组件
    hyperf/validation 官方验证组件
    hyperf/translation 多种语言组件
    hyperf/translation 官方枚举类
    phper666/jwt-auth JWT组件
    hyperf/config-aliyun-acm 阿云配置中心组件
    hyperf/json-rpc hyperf/rpc-server 官方RPC服务组件

    发布组件配置

    php bin/hyperf.php jwt:publish --config
    php bin/hyperf.php vendor:publish hyperf/translation
    php bin/hyperf.php vendor:publish hyperf/validation

    配置项目

    编辑项目根目录的.env文件,配置好数据库和redis

    APP_NAME=user_center
    
    DB_DRIVER=mysql
    DB_HOST=192.168.137.200
    DB_PORT=3306
    DB_DATABASE=user_center
    DB_USERNAME=root
    DB_PASSWORD=123456
    DB_CHARSET=utf8mb4
    DB_COLLATION=utf8mb4_unicode_ci
    DB_PREFIX=
    
    REDIS_HOST=192.168.137.200
    REDIS_AUTH=(null)
    REDIS_PORT=6379
    REDIS_DB=0

    开发环境下的热更新

    下载https://github.com/ha-ni-cc/hyperf-watch里的watch文件到项目根目录,然后启动只需要执行php watch,这样我们修改文件,就会自动重启进程

    数据表设计

    创建表

    目前我们只创建了两个表user和wx_user详见迁移文件
    user表
    https://github.com/donjan-deng/la-user-center/blob/master/migrations/2019_10_31_100142_create_user_table.php
    wx_user表
    https://github.com/donjan-deng/la-user-center/blob/master/migrations/2019_10_31_100153_create_wx_user_table.php

    数据填充

    我们初始化了一个管理员帐号
    https://github.com/donjan-deng/la-user-center/blob/master/seeders/user_table_seeder.php

    运行迁移

    创建数据库

    php bin/hyperf.php migrate

    填充数据

    php bin/hyperf.php db:seed

    有关数据迁移的使用方法可参阅官方文档https://hyperf.wiki/#/zh/db/migration

    项目源码

    项目源码已发布到github https://github.com/donjan-deng/la-user-center

    《PHP微服务练兵》系列索引:https://www.geek-share.com/detail/2790050496.html

    • 点赞
    • 收藏
    • 分享
    • 文章举报
    Donjan 发布了26 篇原创文章 · 获赞 0 · 访问量 2046 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: