使用docker搭建可扩展的swoft协程框架微服务

编程

Docker组建微服务架构。通过多个容器,一台机器可以跑多个服务,因此在本机就可以模拟出微服务架构。

安装docker、composer、git。

 

1、在宿主机创建 swoft 项目(宿主机需实安装基础的 php 环境来使用 composer)

git clone git@github.com:swoft-cloud/swoft.git && cd swoft && composer update && cd swoft

2  在swoft目录创建 nginx.conf ,配置nginx作多个swoft的分发!

worker_processes 1;

events {

worker_connections 1024;

}

http{

upstream swoft_server {

server 139.199.68.125:18306;

server 139.199.68.125:18307;

}

server {

listen 80;

access_log /var/log/nginx/access.log;

error_log /var/log/nginx/error.log;

index index.html index.php;

server_name localhost;

location / {

index index.html index.php;

proxy_pass http://swoft_server/;

}

}

}

 

3、使用swoft目录下的 docker-compose 来编排启动容器

编辑 docker-compose.yaml 文件 给容器自定义个名字

version: "3.4"

networks:

my-network:

driver: bridge

services:

swoft_1:

image: swoft/swoft

container_name: swoft-srv-1

environment:

- APP_ENV=dev

- TIMEZONE=Asia/Shanghai

restart: always

depends_on:

- nginx

stdin_open: true

tty: true

networks:

- "my-network"

privileged: true

entrypoint: ["php", "/var/www/swoft/bin/swoft", "http:start"]

ports:

- "18306:18306"

volumes:

- ./:/var/www/swoft

swoft_2:

image: swoft/swoft

container_name: swoft-srv-2

environment:

- APP_ENV=dev

- TIMEZONE=Asia/Shanghai

restart: always

depends_on:

- nginx

stdin_open: true

tty: true

networks:

- "my-network"

privileged: true

entrypoint: ["php", "/var/www/swoft/bin/swoft", "http:start"]

ports:

- "18307:18306"

volumes:

- ./:/var/www/swoft

nginx:

image: nginx:latest

networks:

- "my-network"

ports:

- "80:80"

container_name: nginx-node

restart: always

volumes:

- "/data/www/swoft/nginx.conf:/etc/nginx/nginx.conf"

- "/var/log/nginx/access.log:/var/log/nginx/access.log"

- "/var/log/nginx/error.log:/var/log/nginx/error.log"

# 启动容器

docker-compose up -d

# 查看容器是否成功运行

docker ps

# 进入容器shell (swoft-srv-1)为容器名称

docker exec -it swoft-srv-1 bash

# 运行容器

docker-compose up -d swoft

# 停止容器

docker-compose stop swoft

 

 

 

 

 

 

以上是 使用docker搭建可扩展的swoft协程框架微服务 的全部内容, 来源链接: utcz.com/z/516890.html

回到顶部