docker-compose之.netcore+mysql+Ocelot多容器部署
作者:互联网
继上节.NetCoreApi容器与MySql容器互联继续讲解,主要增加了Ocelot网关。
项目结构
编写 docker-compose.yml 文件
version: '3' services: db: image: mysql:5.5 command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci container_name: db restart: always ports: - "3306:3306" environment: - MYSQL_PASSWORD=123456 - MYSQL_ROOT_PASSWORD=123456 - MYSQL_USER=yak networks: - dev volumes: - "./db:/var/lib/mysql" - "./conf/my.cnf:/etc/my.cnf" - "./init:/docker-entrypoint-initdb.d/" gateway: container_name: gateway build: context: ./OcelotGateway dockerfile: Dockerfile ports: - 80:80 environment: TZ: "Asia/Shanghai" ASPNETCORE_ENVIRONMENT: "Production" links: - a - b - c - d networks: - dev a: container_name: businessa hostname: Business_A build: context: ./BusinessProjectA dockerfile: Dockerfile ports: - 81:80 environment: TZ: "Asia/Shanghai" ASPNETCORE_ENVIRONMENT: "Production" networks: - dev b: container_name: businessb hostname: Business_B build: context: ./BusinessProjectA dockerfile: Dockerfile ports: - 82:80 environment: TZ: "Asia/Shanghai" ASPNETCORE_ENVIRONMENT: "Production" networks: - dev c: container_name: businessc hostname: Business_C build: context: ./BusinessProjectA dockerfile: Dockerfile ports: - 83:80 environment: TZ: "Asia/Shanghai" ASPNETCORE_ENVIRONMENT: "Production" networks: - dev - pro d: container_name: businessd hostname: Business_D build: context: ./Yak.Docker.Test dockerfile: Dockerfile ports: - 84:80 environment: TZ: "Asia/Shanghai" ASPNETCORE_ENVIRONMENT: "Production" networks: - dev - pro e: container_name: businesse hostname: Business_E build: context: ./Yak.DockerCompose.Api dockerfile: Dockerfile ports: - 85:80 environment: TZ: "Asia/Shanghai" ASPNETCORE_ENVIRONMENT: "Production" networks: - dev - pro depends_on: - db networks: dev: driver: bridge pro: driver: bridge
编写 ocelot.json 文件
{ "Routes": [ { "UpstreamPathTemplate": "/business/{url}", "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Patch", "Options", "Head", "Trace", "Connect", "Purge", "None" ], "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Port": "80", "Host": "businessa" }, { "Port": "80", "Host": "businessb" }, { "Port": "80", "Host": "businessc" } ], "LoadBalancerOptions": { "Type": "LeastConnection", "Exact": true } }, { "UpstreamPathTemplate": "/weather/{url}", "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Patch", "Options", "Head", "Trace", "Connect", "Purge", "None" ], "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Port": "80", "Host": "businessd" } ], "LoadBalancerOptions": { "Type": "LeastConnection", "Exact": true } }, { "UpstreamPathTemplate": "/user/{url}", "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete", "Patch", "Options", "Head", "Trace", "Connect", "Purge", "None" ], "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Port": "80", "Host": "businesse" } ], "LoadBalancerOptions": { "Type": "LeastConnection", "Exact": true } } ], "DynamicReRoutes": [], "Aggregates": [], "GlobalConfiguration": { "BaseUrl": "http://localhost" } }
代码拷贝到Linux系统中
启动 docker-compose
docker-compose up -d --build
查看
docker-compose ps
访问 http://192.168.192.138/user/User/Get
标签:compose,name,netcore,dev,ports,environment,Ocelot,80,networks 来源: https://www.cnblogs.com/yakniu/p/16303873.html