Skip to content

Latest commit

 

History

History
217 lines (181 loc) · 6.77 KB

0.init.md

File metadata and controls

217 lines (181 loc) · 6.77 KB

环境准备

启动项目前, 我默认你已准备好(部分软件按建议方式安装即可):

  • go1.18+(建议使用g)
    # sudo apt update
    # sudo apt install -y curl
    curl -sSL https://raw.githubusercontent.com/voidint/g/master/install.sh | bash
    source "$HOME/.g/env"
    # g --version
    # g version 1.5.0
    
    g install 1.20.5
    # go version
    # go version go1.20.5 linux/amd64
    
    echo "export GOPATH=/home/ubuntu/go" >> ~/.bashrc 
    # 设置go/bin目录到PATH, 若不设置, go安装的一些文件无法识别
    echo "export PATH=$PATH:/home/ubuntu/.g/go/bin:/home/ubuntu/go/bin" >> ~/.bashrc
    source ~/.bashrc
  • 开启go modules
  • mysql(本地测试建议使用docker-compose搭建)
  • redis(本地测试建议使用docker-compose搭建)
    # 安装docker
    sudo apt-get update
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo apt-key fingerprint 0EBFCD88
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
    sudo apt-get update
    sudo apt-get install -y docker-ce
    # sudo docker -v
    # Docker version 23.0.1, build a5ee5b1
    
    # 国内加速安装
    sudo apt-get update
    sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    sudo apt-get update
    sudo apt-get install -y docker-ce
    # sudo docker -v
    # Docker version 23.0.1, build a5ee5b1
    
    # 去除docker sudo
    sudo groupadd docker
    sudo gpasswd -a ${USER} docker
    sudo systemctl restart docker
    sudo chmod a+rw /var/run/docker.sock
    # docker -v
    # Docker version 23.0.1, build a5ee5b1
    
    # docker-compose
    sudo curl -L https://github.com/docker/compose/releases/download/v2.19.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    # docker-compose -v
    # Docker Compose version v2.19.1
    
    # 简单启动一个单机版mysql和redis
    git clone https://github.com/go-cinch/compose
    cd compose/single
    # 修改默认密码
    source myenv
    docker-compose -f docker-compose.db.yml up -d redis mysql
    # docker ps
    # CONTAINER ID  IMAGE         COMMAND                 CREATED            STATUS         PORTS                                                 NAMES
    # 918328d0aae1  mysql:8.0.19  "docker-entrypoint.s…"  About an hour ago  Up 59 minutes  0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp  mysql
    # 918b2cfcd72e  redis:7.0     "docker-entrypoint.s…"  About an hour ago  Up 59 minutes  0.0.0.0:6379->6379/tcp, :::6379->6379/tcp             redis
  • protoc
    curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protoc-3.20.3-`uname -s`-`uname -m`.zip
    # apt install -y unzip
    sudo unzip protoc-3.20.3-`uname -s`-`uname -m`.zip -d /usr
    # protoc --version
    # libprotoc 3.20.3
  • git
    sudo apt update
    sudo apt install -y git
  • cinch cli工具
    go install github.com/go-cinch/cinch/cmd/cinch@latest
    # cinch -v
    cinch version v1.1.2

权限认证服务无需再开发, 下载开箱即用

git clone https://github.com/go-cinch/auth
# 可以指定tag
# git clone -b v1.1.0 https://github.com/go-cinch/auth

创建Game服务

# 1.通过模板创建项目
cinch new game
# -r 指定仓库 -b 指定分支
# cinch new game -r https://github.com/go-cinch/layout.git -b dev
# cinch new game -r https://github.com/go-cinch/layout.git -b v1.1.3

# 2. 进入项目
cd game
# 创建个人开发分支
git init -b mark/dev
# 如果你的git版本较低
# git init
# git checkout -b mark/dev

# 3. 初始化submodule(不想使用可忽略此步骤)
# -b指定分支 --name指定submodule名称
git submodule add -b master --name api/auth-proto https://github.com/go-cinch/auth-proto.git ./api/auth-proto

# -b指定分支 --name指定submodule名称
git submodule add -b master --name api/reason-proto https://github.com/go-cinch/reason-proto.git ./api/reason-proto

# 这里用game作为示例, 按需修改
# -b指定分支 --name指定submodule名称
git submodule add -b master --name api/game-proto https://github.com/go-cinch/game-proto.git ./api/game-proto

# 删除一个已经存在的submodule
# git submodule deinit api/game-proto
# git rm --cached api/game-proto
# rm -rf .git/modules/api/game-proto
# rm -rf api/game-proto

# 4. 初始化依赖项(需确保已经安装make)
# sudo apt install -y make
make init

# 5. 做启动前的所有检查
make all

启动

配置文件

# 修改auth项目配置
cd auth
# 将mysql/redis的配置修改成你本地配置
vim configs/config.yml

# 修改game项目配置
cd game
# 将mysql/redis的配置修改成你本地配置
vim game/configs/config.yml
# 将auth服务host和端口修改成你本地配置
vim game/configs/client.yml

# 启动auth
cd auth
cinch run

# 启动game
cd game
cinch run

环境变量

# 启动auth
# 如果你用的是compose/single
export SERVICE_DATA_DATABASE_DSN='root:mysqlrootpwd@tcp(127.0.0.1:3306)/auth?parseTime=True'
export SERVICE_DATA_REDIS_DSN='redis://:[email protected]:6379/0'
# 其他按需修改
export SERVICE_DATA_DATABASE_DSN='root:root@tcp(127.0.0.1:3306)/auth?parseTime=True'
export SERVICE_DATA_REDIS_DSN='redis://127.0.0.1:6379/0'
cd auth
cinch run

# 启动game
# 如果你用的是compose/single
export SERVICE_DATA_DATABASE_DSN='root:mysqlrootpwd@tcp(127.0.0.1:3306)/game?parseTime=True'
export SERVICE_DATA_REDIS_DSN='redis://:[email protected]:6379/0'
# 其他按需修改
export SERVICE_DATA_DATABASE_DSN='root:root@tcp(127.0.0.1:3306)/game?parseTime=True'
export SERVICE_DATA_REDIS_DSN='redis://127.0.0.1:6379/0'
# 设置auth服务
export SERVICE_CLIENT_AUTH='127.0.0.1:6160'
cd game
cinch run

?> Tip: 环境变量前缀可在cmd/xxx/main.go中修改, 参见环境变量前缀

测试访问

auth服务:

curl http://127.0.0.1:6060/idempotent
# 输出如下说明服务通了只是没有权限, 出现其他说明配置有误
# {"code":401, "reason":"UNAUTHORIZED", "message":"token is missing", "metadata":{}}

game服务:

curl http://127.0.0.1:8080/game/list
# 输出如下说明服务通了只是没有权限, 出现其他说明配置有误
# {"code":401, "reason":"UNAUTHORIZED", "message":"token is missing", "metadata":{}}

?> 至此, 微服务已启动完毕, auth以及game, 接下来可以自定义你的game啦~