跳至正文

如何手动部署到容器

内容纲要

前言

    当今是容器的时代,不管是否开发分布式/微服务架构的系统都建议大家使用docker部署。linux+dokcer是最好的搭配,如果您只安装了windows,可以考虑安装linux虚拟机。Adnc部署到服务器,需要先安装mysqlredisrabbitmqmongogdbskywalkingconsulnginx
    如何安装mysqlredisrabbitmqmongogdb请参考 如何快速跑起来,里面有详细介绍。
下面介绍如何安装consul,skywalking,nginx

  • 10.0.8.15 是我的部署服务器内网IP,需要替换成你自己的地址。

安装consul

consul是微服务的中转中心(注册中心/配置中心),最重要的组件。如果consul服务器挂了,系统也会奔溃。因为重要,所有我们必须要部署一个集群。
consul分为server节点与client节点,server节点负责存储数据;client节点负责注册、发现、读写配置、健康监测。
如果直接在linux环境部署consul集群,配置很麻烦,使用docker虽简单了很多,但操作步骤还是很多的。

#拉取镜像
docker pull consul:1.8.0
#自定义网络,自定义网络可以指定容器IP,这样服务器重启consul集群也可以正常运行。
docker network create --driver bridge --subnet=172.21.0.0/16 --gateway=172.21.0.16 adnc_consul
#启动容器consul_server_1
docker run -d -p 8590:8500 --restart=always -v /root/data/consul/server1/data:/consul/data -v /root/data/consul/server1/config/:/consul/config --network=adnc_consul --ip 172.21.0.1 --privileged=true --name=consul_server_1 consul:1.8.0 agent -server -bootstrap-expect=3 -ui -node=consul_server_1 -client='0.0.0.0' -bind='172.21.0.1' -data-dir /consul/data -config-dir /consul/config -datacenter=adnc_dc

-d 后台运行
-p 8590:8500 主机8590端口映射到容器8500端口。
-v 挂载文件,-v 参数可以出现多次,我们分别将data数据与config存放在主机。
-name=consul_server_1容器的名称,这里不要用重复的名称,要有一定规则。
-server 表示server节点
-bootstrap-expect=3:表示期望提供的SERVER节点数目,数目一达到,它就会被激活。我们设定是3个服务节点,部署consul集成server节点至少3个。
-ui 开启ui界面,部署成功后我们可以通过http://你的主机Ip:8590,访问这个节点的ui界面。
-node=consul_server_1 节点名字,与容器名称一样,方便运维。
-datacenter=adnc_dc 集群数据中心名称

#为了让其他两个server节点加入集群,首先保存一下consul_server_1的IP地址。
JOIN_IP="172.21.0.1"
#启动容器consul_server_2并加入集群
docker run -d -p 8520:8500 --restart=always -v /root/data/consul/server2/data:/consul/data -v /root/data/consul/server2/config/:/consul/config --network=adnc_consul --ip 172.21.0.2 --privileged=true --name=consul_server_2 consul:1.8.0 agent -server -ui -node=consul_server_2 -client='0.0.0.0' -bind='172.21.0.2' -datacenter=adnc_dc -data-dir /consul/data -config-dir /consul/config -join=$JOIN_IP

-join=$JOIN_IP $JOIN_IP是上面获取到的consul_server_1节点的ip, join表示加入集群。

#启动容器consul_server_3并加入集群
docker run -d -p 8530:8500 --restart=always -v /root/data/consul/server3/data:/consul/data -v /root/data/consul/server3/config/:/consul/config --network=adnc_consul --ip 172.21.0.3 --privileged=true --name=consul_server_3 consul:1.8.0 agent -server -ui -node=consul_server_3 -client='0.0.0.0' -bind='172.21.0.3' -datacenter=adnc_dc -data-dir /consul/data -config-dir /consul/config -join=$JOIN_IP
#验证2个server节点是否加入集群
docker exec consul_server_1 consul operator raft list-peers

显示信息如下,表示集群搭建成功。集群有3个server节点,consul_server_1是leader。

Node             ID                                    Address          State     Voter  RaftProtocol
consul_server_2  fc655a2d-556b-f6aa-1b17-d189da0081b4  172.21.0.2:8300  follower  true   3
consul_server_3  3cc9fc4e-a65c-1666-ab4c-d6cb5cfefd8a  172.21.0.3:8300  leader    true   3
consul_server_1  0026bb2d-d2e8-5681-a3ad-ada57036e2e1  172.21.0.1:8300  follower  false  3
#启动容器consul_client_1并加入集群
docker run -d -p 8500:8500 --restart=always -v /data/consul/client1/config:/consul/config --network=adnc_consul --ip 172.21.0.4 --name=consul_client_1 consul:1.8.0 agent -node=consul_client_1   -client='0.0.0.0' -bind='172.21.0.4' -datacenter=adnc_dc -config-dir /consul/config -join=$JOIN_IP

client节点可以N多个,一般来说每台服务器上都需要部署一个client节点。

# 将consul的4个容器连接到bridge网桥(docker默认的网络)。主要目的是联通两个网段,这样consul就能访问docker默认网段的实例。
docker network connect bridge consul_server_3
docker network connect bridge consul_server_2
docker network connect bridge consul_server_1
docker network connect bridge consul_client_1

consul集群安装完成,我们在浏览器输入http://服务器IP:8590/ui/adnc_dc/nodes,应该可以看到4个节点。


安装SkyWalking

SkyWalking是观察性分析平台和应用性能管理系统,提供分布式追踪、服务网格遥测分析、度量聚合和可视化一体化解决方案。 SkyWalking需要安装三个软件elasticsearchskywalking-oap-serverskywalking-ui

elasticsearch 一个分布式、高扩展、高实时的搜索与数据分析存储引擎
skywalking-oap-server 可观测性分析平台,可以以从多种数据源接收数据,分析Tracing和Metrics
skywalking-ui UI界面
下面的安装方式没有安装ES,使用H2数据库存储数据,真实生产环境请安装ES版本。
skywalking系列软件非常消耗内存,您的服务器配置内存需要大于4G。

#安装skywalking-oap-server,根据服务器实际情况设置JAVA_OPTS(内存)大小
docker pull apache/skywalking-oap-server:9.1.0
docker run --name oap --restart always -e TZ=Asia/Shanghai  -e JAVA_OPTS='-Xmx512m' -d -p 12800:12800 -p 11800:11800 apache/skywalking-oap-server:9.1.0

#安装skywalking-ui,SW_OAP_ADDRESS是上面安装的skywalking-oap-server的地址。
docker pull apache/skywalking-ui:9.1.0
docker run --name skyapm-ui --restart always -d  -e TZ=Asia/Shanghai -e JAVA_OPTS='-Xmx512m' -p 18886:8080 -e SW_OAP_ADDRESS=http://10.0.8.15:12800 apache/skywalking-ui:9.1.0

skywalking安装完成,我们访问http://服务器IP:18886/,会出现登录页面,默认账号/密码 都是 admin 。
到此为止,serverapi需要的软件都已经安装完成(除nginxngnix的安装会在部署前端的时候介绍)。


下面正式进入主题,配置与部署。微服务的第一次部署是比较繁琐的,很多事情要做,往后就简单方便了。

配置consul的Key/Value

Adnc会从consul读取appsettings的配置,并填充configuration对象。
consul任意节点配置的key/value都会自动同步到其他节点

if (env.IsProduction() || env.IsStaging())
{
    var configuration = cb.Build();
    var consulOption = configuration.GetSection(ConsulConfig.Name).Get<ConsulConfig>();
    cb.AddConsulConfiguration(consulOption, true);
}
  • consul配置key/vaule配置有3种方法

    1、通过类似postman工具直接调用api配置。
    2、创建配置文件上传到节点目录配置。
    3、直接登录服务节点UI界面配置。

我们采用第3种最简单的方式,直接登录UI界面,配置key/value,Adnc有4个服务需要部署对应consul的5个key(5个appsettings)。

服务名 描述 key path
Adnc.Gateway.Ocelot Ocelot网关 /adnc/production/gateway/appsettings
Adnc.Usr.WebApi 用户中心服务 /adnc/production/usr/appsettings
Adnc.Main.WebApi 运维中心服务 /adnc/production/maint/appsettings
Adnc.Cus.WebApi 客户中心服务 /adnc/production/cus/appsettings
服务公共配置 /adnc/production/shared/appsettings
  • 配置key/adnc/production/gateway/appsettings,请将下面的内容copy到key的value中。
{
  "Logging": {
    "LogLevel": {
      "Default": "Error",
      "Adnc": "Information",
      "Microsoft": "Error",
      "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Error"
    }
  },
  "ThreadPoolSettings": {
    "MinThreads": 8,
    "MinCompletionPortThreads": 8,
    "MaxThreads": 32767,
    "MaxCompletionPortThreads": 1000
  },
  "AllowedHosts": "*",
  "CorsHosts": "http://114.132.157.167",
  "JWT": {
    "ValidateIssuer": true,
    "ValidIssuer": "adnc",
    "ValidateIssuerSigningKey": true,
    "SymmetricSecurityKey": "alphadotnetcoresecurity2020",
    "ValidateAudience": true,
    "ValidAudience": "manager",
    "ValidateLifetime": true,
    "RequireExpirationTime": true,
    "ClockSkew": 1,
    "RefreshTokenAudience": "manager",
    "Expire": 6000,
    "RefreshTokenExpire": 10080
  },
  "SkyWalking": {
    "ServiceName": "adnc-gateway-ocelot",
    "Namespace": "adnc",
    "HeaderVersions": [
      "sw8"
    ],
    "Sampling": {
      "SamplePer3Secs": -1,
      "Percentage": -1.0,
      "IgnorePaths": [ "/*/health-24b01005-a76a-4b3b-8fb1-5e0f2e9564fb", "http://**/appsettings", "/**/swagger.json" ]
    },
    "Logging": {
      "Level": "Error",
      "FilePath": "logs\\skyapm-{Date}.log"
    },
    "Transport": {
      "Interval": 3000,
      "ProtocolVersion": "v8",
      "QueueSize": 30000,
      "BatchSize": 3000,
      "gRPC": {
        "Servers": "10.0.8.15:11800",
        "Timeout": 10000,
        "ConnectTimeout": 10000,
        "ReportTimeout": 600000,
        "Authentication": ""
      }
    }
  },
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "10.0.8.15",
      "Port": 8500,
      "Type": "Consul"
    },
    "RequestIdKey": "RequestId",
    "RateLimitOptions": {
      "ClientWhitelist": [],
      "EnableRateLimiting": true,
      "Period": "1s",
      "Limit": 3000,
      "PeriodTimespan": 10
    },
    "QoSOptions": {
      "ExceptionsAllowedBeforeBreaking": 5,
      "DurationOfBreak": 3000,
      "TimeoutValue": 3000
    }
  },
  "Routes": [
    {
      "DownstreamPathTemplate": "/usr/health-{guid}",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-usr-webapi",
      "UpstreamPathTemplate": "/usr/health-{guid}",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/usr/index.html",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-usr-webapi",
      "UpstreamPathTemplate": "/usr/index.html",
      "UpstreamHttpMethod": [ "Get" ]
    },    
    {
      "DownstreamPathTemplate": "/usr/swagger{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-usr-webapi",
      "UpstreamPathTemplate": "/usr/swagger{everything}",
      "UpstreamHttpMethod": [ "Get" ]
    },      
   {
      "DownstreamPathTemplate": "/maint/health-{guid}",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-maint-webapi",
      "UpstreamPathTemplate": "/maint/health-{guid}",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/maint/index.html",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-maint-webapi",
      "UpstreamPathTemplate": "/maint/index.html",
      "UpstreamHttpMethod": [ "Get" ]
    },    
    {
      "DownstreamPathTemplate": "/maint/swagger{everything}",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-maint-webapi",
      "UpstreamPathTemplate": "/maint/swagger{everything}",
      "UpstreamHttpMethod": [ "Get" ]
    },      
     {
      "DownstreamPathTemplate": "/maint/session",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-maint-webapi",
      "UpstreamPathTemplate": "/maint/session",
      "UpstreamHttpMethod": [ "Post" ]
    },     
    {
      "DownstreamPathTemplate": "/cus/health-{guid}",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-cus-webapi",
      "UpstreamPathTemplate": "/cus/health-{guid}",
      "UpstreamHttpMethod": [ "Get" ]
    },
    {
      "DownstreamPathTemplate": "/auth/session",
      "DownstreamScheme": "http",
      "ServiceName": "adnc-usr-webapi",
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      },   
      "UpstreamPathTemplate": "/auth/session",
      "UpstreamHttpMethod": [ "Post", "Put" ],
      "UpstreamHeaderTransform": {
          "From": "gateway",
          "X-Forwarded-For": "{RemoteIpAddress}"
         },
         "DownstreamHeaderTransform": {
            "X-Forwarded-For": "{RemoteIpAddress}"
       }   
     },
    {
      "DownstreamPathTemplate": "/auth/session",
      "DownstreamScheme": "http",
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "mgmt",
        "AllowedScopes": []
      },
      "ServiceName": "adnc-usr-webapi",  
      "UpstreamPathTemplate": "/auth/session",
      "UpstreamHttpMethod": [ "Get", "Delete" ]
    },
    {
      "DownstreamPathTemplate": "/auth/session/password",
      "DownstreamScheme": "http",
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "mgmt",
        "AllowedScopes": []
      },
      "ServiceName": "adnc-usr-webapi",
      "UpstreamPathTemplate": "/auth/session/password",
      "UpstreamHttpMethod": [ "Put" ]
    },
    {
      "UpstreamPathTemplate": "/usr{everything}",
      "UpstreamHttpMethod": [
        "Get",
        "Put",
        "Post",
        "Delete"
      ],
      "DownstreamScheme": "http",
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "mgmt",
        "AllowedScopes": []
      },
      "DownstreamPathTemplate": "/usr{everything}",
      "ServiceName": "adnc-usr-webapi"
    },
    {
      "UpstreamPathTemplate": "/maint{everything}",
      "UpstreamHttpMethod": [
        "Get",
        "Put",
        "Post",
        "Delete"
      ],
      "DownstreamScheme": "http",
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "mgmt",
        "AllowedScopes": []
      },
      "DownstreamPathTemplate": "/maint{everything}",
      "ServiceName": "adnc-maint-webapi"
    },
    {
      "UpstreamPathTemplate": "/cus/cap{everything}",
      "UpstreamHttpMethod": ["Get"],
      "DownstreamScheme": "http",
      "DownstreamPathTemplate": "/cus/cap{everything}",
      "ServiceName": "adnc-cus-webapi"
    },
    {
      "UpstreamPathTemplate": "/cus/swagger{everything}",
      "UpstreamHttpMethod": ["Get"],
      "DownstreamScheme": "http",
      "DownstreamPathTemplate": "/cus/swagger{everything}",
      "ServiceName": "adnc-cus-webapi"
    },
    {
      "UpstreamPathTemplate": "/cus/index.html",
      "UpstreamHttpMethod": ["Get"],
      "DownstreamScheme": "http",
      "DownstreamPathTemplate": "/cus/index.html",
      "ServiceName": "adnc-cus-webapi"
    },
    {
      "UpstreamPathTemplate": "/cus/customers",
      "UpstreamHttpMethod": ["Post"],
      "DownstreamScheme": "http",
      "DownstreamPathTemplate": "/cus/customers",
      "ServiceName": "adnc-cus-webapi"
    },  
    {
      "UpstreamPathTemplate": "/cus{everything}",
      "UpstreamHttpMethod": [
        "Get",
        "Put",
        "Delete",
        "Patch"
      ],
      "DownstreamScheme": "http",
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "mgmt",
        "AllowedScopes": []
      },
      "DownstreamPathTemplate": "/cus{everything}",
      "ServiceName": "adnc-cus-webapi"
    }
  ]
}
  • 配置/adnc/production/shared/appsettings,请将下面的内容copy到key的value中并修改节点IP、用户名与密码信息。
{
  //RegisteredType = consul 表示容器启动时会自动注册到consul
  "RegisteredType": "consul",
  "Consul": {
    "ServiceName": "$SERVICENAME",
    "ServerTags": [ "urlprefix-/$SHORTNAME" ],
    "HealthCheckUrl": "$SHORTNAME/health-24b01005-a76a-4b3b-8fb1-5e0f2e9564fb",
    "HealthCheckIntervalInSecond": 6,
    "DeregisterCriticalServiceAfter": 20,
    "Timeout": 6
  },
  "ThreadPoolSettings": {
    "MinThreads": 8,
    "MinCompletionPortThreads": 8,
    "MaxThreads": 32767,
    "MaxCompletionPortThreads": 1000
  },
 "Loki": {
    "Endpoint": "http://10.0.8.15:3100",
    "UserName": "",
    "Password": ""
  },
  "Logging": {
    "IncludeScopes": true,
    "LogContainer": "console",
    "LogLevel": {
      "Default": "Error",
      "Adnc": "Information",
      "Microsoft": "Error",
      "Microsoft.Hosting.Lifetime": "Error"
    }
  },
 "Caching": {
    "MaxRdSecond": 30,
    "LockMs": 6000,
    "SleepMs": 300,
    "EnableLogging": true,
    "PollyTimeoutSeconds": 11,
    "PenetrationSetting": {
      "Disable": false,
      "BloomFilterSetting": {
        "Name": "adnc:$SHORTNAME:bloomfilter:cachekeys",
        "Capacity": 10000000,
        "ErrorRate": 0.001
      }
    }
  },
  "MongoDb": {
    "ConnectionString": "mongodb://alpha:football@10.0.8.15:13017/logs?authSource=logs",
    "CollectionNamingConvention": 2,
    "PluralizeCollectionNames": true
  },
  "RabbitMq": {
    "HostName": "10.0.8.15",
    "VirtualHost": "vhost.adnc.prod",
    "Port": "13572",
    "UserName": "adncprod",
    "Password": "adncprod.123"
  },
  "AllowedHosts": "*",
  "CorsHosts": "http://10.0.8.15:8888",
  "SSOAuthentication": false,
  "JWT": {
    "ValidateIssuer": true,
    "ValidIssuer": "adnc",
    "ValidateIssuerSigningKey": true,
    "SymmetricSecurityKey": "alphadotnetcoresecurity2020",
    "ValidateAudience": true,
    "ValidAudience": "manager",
    "ValidateLifetime": true,
    "RequireExpirationTime": true,
    "ClockSkew": 1,
    "RefreshTokenAudience": "manager",
    "Expire": 6000,
    "RefreshTokenExpire": 10080
  },
  "SkyWalking": {
    "ServiceName": "$SERVICENAME",
    "Namespace": "adnc",
    "HeaderVersions": [
      "sw8"
    ],
    "Sampling": {
      "SamplePer3Secs": -1,
      "Percentage": -1.0,
      "IgnorePaths": [ "/*/health-24b01005-a76a-4b3b-8fb1-5e0f2e9564fb", "http://**/appsettings", "/**/swagger.json","http://**/loki/api/v1/push" ]
    },
    "Logging": {
      "Level": "Error",
      "FilePath": "txtlogs\\skyapm-{Date}.log"
    },
    "Transport": {
      "Interval": 3000,
      "ProtocolVersion": "v8",
      "QueueSize": 30000,
      "BatchSize": 3000,
      "gRPC": {
        "Servers": "10.0.8.15:11800",
        "Timeout": 10000,
        "ConnectTimeout": 10000,
        "ReportTimeout": 600000,
        "Authentication": ""
      }
    }
  },
 "Kestrel": {
    "Endpoints": {
      "Default": {
        "Url": "http://0.0.0.0:80"
      },
      "Grpc": {
        "Url": "http://0.0.0.0:81",
        "Protocols": "Http2"
      }
    }
  },
 "RpcPartners": [
    {
      "UserName": "internalcaller",
      "AppId": "88888888",
      "SecurityKey": "f37a1c7a5c959e25804ab33111b1cc9f"
    }
  ],
  "RpcAddressInfo": [
    {
      "Service": "adnc-usr-webapi",
      "Direct": "http://localhost:50010",
      "Consul": "http://adnc-usr-webapi",
      "CoreDns": "http://adnc-usr-webapi.default.svc.cluster.local"
    },
    {
      "Service": "adnc-maint-webapi",
      "Direct": "http://localhost:50020",
      "Consul": "http://adnc-maint-webapi",
      "CoreDns": "http://adnc-maint-webapi.default.svc.cluster.local"
    },
    {
      "Service": "adnc-cus-webapi",
      "Direct": "http://localhost:50030",
      "Consul": "http://adnc-cus-webapi",
      "CoreDns": "http://adnc-cus-webapi.default.svc.cluster.local"
    },
    {
      "Service": "adnc-ord-webapi",
      "Direct": "http://localhost:50040",
      "Consul": "http://adnc-ord-webapi",
      "CoreDnsDomain": "http://adnc-ord-webapi.default.svc.cluster.local"
    },
    {
      "Service": "adnc-whse-webapi",
      "Direct": "http://localhost:50050",
      "Consul": "http://adnc-whse-webapi",
      "CoreDns": "http://adnc-whse-webapi.default.svc.cluster.local"
    }
  ]
}
  • 依次配置以下节点,请将下面的内容copy到key的value中并修改节点IP、用户名与密码信息。
    /adnc/production/usr/appsettings
    /adnc/production/maint/appsettings
    /adnc/production/cus/appsettings
{
  "Mysql": {
    "ConnectionString": "Server=10.0.8.15;Port=13308;database=adnc_usr;uid=root;pwd=alpha.netcore;connection timeout=30;"
  },
  "Redis": {
    "Provider": "StackExchange",
    "EnableBloomFilter": false,
    "EnableLogging": true,
    "SerializerName": "binary",
    "Dbconfig": {
      //每个服务可以设置不同defaultDatabase,也可以用同一个
      "ConnectionString": "10.0.8.15:13379,password=football,defaultDatabase=7,ssl=false,sslHost=null,connectTimeout=4000,allowAdmin=true"
    }
  }
}

修改配置文件

  • 修改所有服务appsetting.Production.json,替换consul服务器IP地址即可。
{
  "Consul": {
    "ConsulUrl": "http://10.0.8.15:8500",
    "ConsulKeyPath": "adnc/production/shared/appsettings,adnc/production/$SHORTNAME/appsettings"
  }
}

发布/部署

  1. 在服务器上建wwwroot目录,并且在该目录下新建adnc_gatewayadnc_usradnc_cusadnc_maintnginx 5个子目录。

    mkdir -p /wwwroot/adnc_gateway
    mkdir /wwwroot/adnc_usr
    mkdir /wwwroot/adnc_maint
    mkdir /wwwroot/adnc_cus
    mkdir /wwwroot/nginx

    依次发布adnc.gateway.ocelotadnc.cus.webapiadnc.usr.webapiadnc.maint.webapi
    部署模式:框架依赖
    目标运行时:不要选择“可移植”,要选择与服务器配套的运行时,如linux-x64。

  2. 发布成功后分别上传到服务器各自对应目录里面(不包括前端)。


Adnc.Usr.WebApi

  • 进入adnc_usr的目录,新建一个Dockerfile文件。内容如下
    #使用asp.net 6作为基础镜像,起一个别名为base
    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    #设置容器的工作目录为/app
    WORKDIR /app
    #COPY 文件
    COPY . /app
    #设置ASPNETCORE_ENVIRONMENT
    ENV ASPNETCORE_ENVIRONMENT Production
    #skywalking服务注册 
    ENV ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=SkyAPM.Agent.AspNetCore
    ENV SKYWALKING__SERVICENAME=adnc-usr-webapi
    #设置时区为东八区
    ENV TZ Asia/Shanghai
    #启动服务
    ENTRYPOINT ["dotnet", "Adnc.Usr.WebApi.dll"]
  • adnc_usr目录下打包镜像文
    cd /wwwroot/adnc_usr
    #注意最后有一个点,表示打包dockerfile文件所在的目录
    docker build -t adnc-usr-webapi .
  • 启动adnc-usr-webapi容器
    #容器正常启动后,会自动注册到consul。你可以启动多个容器。
    docker run --name adnc-usr-webapi-01 -d adnc-usr-webapi
  • 进入consulUI管理界面http://consul服务器ip:8500,查看是否注册成功。

Adnc.Maint.WebApi

  • 进入adnc_maint的目录,新建一个Dockerfile文件。内容如下
#使用asp.net 6作为基础镜像,起一个别名为base
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
#设置容器的工作目录为/app
WORKDIR /app
#COPY 文件
COPY . /app
#设置ASPNETCORE_ENVIRONMENT
ENV ASPNETCORE_ENVIRONMENT Production
#skywalking服务注册 
ENV ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=SkyAPM.Agent.AspNetCore
ENV SKYWALKING__SERVICENAME=adnc-maint-webapi
#设置时区为东八区
ENV TZ Asia/Shanghai
#启动服务
ENTRYPOINT ["dotnet", "Adnc.Maint.WebApi.dll"]
  • adnc_maint目录下打包镜像文

    cd /wwwroot/adnc_maint
    #注意最后有一个点,表示打包dockerfile文件所在的目录
    docker build -t adnc-maint-webapi .
  • 启动adnc-maint-webapi容器

    #容器正常启动后,会自动注册到consul。你可以启动多个容器。
    docker run --name adnc-maint-webapi-01 -d adnc-maint-webapi
  • 进入consulUI管理界面http://consul服务器ip:8500,查看是否注册成功。


Adnc.Cus.WebApi

  • 进入adnc_cus的目录,新建一个Dockerfile文件。内容如下
#使用asp.net 6作为基础镜像,起一个别名为base
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
#设置容器的工作目录为/app
WORKDIR /app
#COPY 文件
COPY . /app
#设置ASPNETCORE_ENVIRONMENT
ENV ASPNETCORE_ENVIRONMENT Production
#skywalking服务注册 
ENV ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=SkyAPM.Agent.AspNetCore
ENV SKYWALKING__SERVICENAME=adnc-cus-webapi
#设置时区为东八区
ENV TZ Asia/Shanghai
#启动服务
ENTRYPOINT ["dotnet", "Adnc.Cus.WebApi.dll"]
  • adnc_cus目录下打包镜像文

    cd /wwwroot/adnc_cus
    #注意最后有一个点,表示打包dockerfile文件所在的目录
    docker build -t adnc-cus-webapi .
  • 启动adnc-cus-webapi容器

    #容器正常启动后,会自动注册到consul
    docker run --name adnc-cus-webapi-01 -d adnc-cus-webapi
  • 进入consulUI管理界面http://consul服务器ip:8500,查看是否注册成功。


Adnc.Gateway.Ocelot

  • 进入adnc_gateway的目录,新建一个Dockerfile文件,内容如下

    #使用asp.net 6作为基础镜像,起一个别名为base
    FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
    #设置容器的工作目录为/app
    WORKDIR /app
    #COPY 文件
    COPY . /app
    #设置ASPNETCORE_ENVIRONMENT
    ENV ASPNETCORE_ENVIRONMENT Production
    #skywalking服务注册 
    ENV ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=SkyAPM.Agent.AspNetCore
    ENV SKYWALKING__SERVICENAME=adnc-gateway-ocelot
    ENV TZ Asia/Shanghai
    ENTRYPOINT ["dotnet", "Adnc.Gateway.Ocelot.dll"]
  • adnc_gateway目录下打包镜像文

    cd /wwwroot/adnc_gateway
    #注意最后有一个点,表示打包dockerfile文件所在的目录
    docker build -t adnc-gateway-ocelot .
  • 启动adnc-gateway容器,容器监听8888端口。网关需要对外,所以需要暴露端口。

    #启动网关
    docker run --name adnc-gateway-ocelot-8888 -d -p 8888:80 adnc-gateway-ocelot
  • 进入gateway首页http://服务器ip:8888,会显示Hello Ocelot,Production!

  • 到了这一步,如果服务都成功部署,以下地址都可以通过网关正常路由到各自健康检测页面。
    http://服务器ip:8888/usr/health-24b01005-a76a-4b3b-8fb1-5e0f2e9564fb
    http://服务器ip:8888/maint/health-24b01005-a76a-4b3b-8fb1-5e0f2e9564fb
    http://服务器ip:8888/cus/health-24b01005-a76a-4b3b-8fb1-5e0f2e9564fb


ClientApp

终于写到前端了,写到这里我都已经写了一整天。

  • 新建/wwwroot/nginx/conf/nginx.conf
#nginx.conf
#运行nginx的用户
#user  nginx;
user root;
#启动进程设置成和CPU数量相等
worker_processes  1;
#全局错误日志及PID文件的位置
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
#工作模式及连接数上限
events {
        #单个后台work进程最大并发数设置为1024
    worker_connections  1024;
}
http {
        #设定mime类型
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

        #设定日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

        #设置连接超时的事件
    keepalive_timeout  65;

        #开启GZIP压缩
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}
  • 新建/wwwroot/nginx/conf.d/default.conf
#default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • 部署nginx

    #拉取镜像文件
    docker pull nginx
    #启动nginx容器,容器监听80端口,并挂载nginx.conf文件,conf.d,log,data目录
    docker run --name nginx -p 80:80 -v /wwwroot/nginx/html:/usr/share/nginx/html -v /wwwroot/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /wwwroot/nginx/conf.d:/etc/nginx/conf.d -v /wwwroot/nginx/logs:/var/log/nginx -d nginx
  • 用visual code 打开 clientApp

    #先编译
    npm run build:prod
    #打包后的文件在clientApp目录下dist目录里面
  • build成功后,将dist目录里的文件上传到/wwwroot/nginx/html目录

  • 打开http://服务器ip:80,如果成功,会出现登录页面,输入用户名/密码。
    如果能登录成功,并进入dashboard页,那么恭喜您,adnc部署成功。


WELL DONE,记得 star&&fork
全文完,ADNC一个可以落地的.NET微服务/分布式开发框架。

《如何手动部署到容器》有2个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注