docker-compose之.netcore+mysql+Ocelot多容器部署

2022/5/24 2:19:50

本文主要是介绍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

 

 

 

 



这篇关于docker-compose之.netcore+mysql+Ocelot多容器部署的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程