.netcore如何使用surging框架發(fā)布到docker

這篇文章將為大家詳細(xì)講解有關(guān).netcore如何使用surging框架發(fā)布到docker,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比瑪多網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式瑪多網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋瑪多地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。

demo運(yùn)行在windows的docker中,系統(tǒng)是win10,所以需要先下載Docker for Windows,安裝完畢后系統(tǒng)會(huì)重啟,然后桌面上可以找到Docker for Windows的快捷圖標(biāo),右下角有個(gè)鯨魚小圖標(biāo)

.netcore如何使用surging框架發(fā)布到docker

單擊右鍵,選擇菜單中的Kitematic

.netcore如何使用surging框架發(fā)布到docker

會(huì)提示你下載Kitematic,自行下載后解壓即可,將Kitematic快捷到桌面;

打開Kitematic,在搜索欄中下載好RabbitMQ、redis、consul相關(guān)鏡像,因?yàn)檫@些是surging運(yùn)行的先決條件。

.netcore如何使用surging框架發(fā)布到docker

.netcore如何使用surging框架發(fā)布到docker

.netcore如何使用surging框架發(fā)布到docker

接著去GitHub上下載surging網(wǎng)關(guān)項(xiàng)目,修改其中的gatewaySettings.json中Register下的Address地址,對應(yīng)的事consul docker鏡像的ip

具體如何查看其ip,看如下操作:

打開Kitematic,點(diǎn)擊左下角,如圖:

.netcore如何使用surging框架發(fā)布到docker

進(jìn)入到命令窗口,輸入docker container ls或者 docker ps -a 查看docker,

可以看到現(xiàn)在運(yùn)行的docker的相關(guān)信息,

如圖:

.netcore如何使用surging框架發(fā)布到docker

然后查看consul的相關(guān)配置,輸入docker inspect 鏡像的 containerID,如consul的id是b0e98b94638c,輸入命令如下:docker inspect b0e98b94638c,

顯示這個(gè)docker的配置,內(nèi)容會(huì)很多,不過ip的信息在最后,如圖

.netcore如何使用surging框架發(fā)布到docker

找到其中的ip是多少,然后修改surging網(wǎng)關(guān)中的consul地址為:"Address": "172.17.0.4:8500",其他配置根據(jù)上面的操作進(jìn)行修改,如redis 鏡像地址的查找和修改等;

修改好surging的網(wǎng)關(guān)配置后在Surging.ApiGateway項(xiàng)目上單擊右鍵,由于我項(xiàng)目中已經(jīng)添加過,所以該處為灰色,如圖:

.netcore如何使用surging框架發(fā)布到docker

新建docker-Compose后修改其中docker-compose.yml的配置如下:

.netcore如何使用surging框架發(fā)布到docker

在后面添加docker的外部端口和內(nèi)部端口的映射和網(wǎng)絡(luò)模式,這里我們都使用橋接模式,包括之前的consul、RabbitMQ、redis都是同一模式,這樣他們會(huì)在同一VLAN下,

然后運(yùn)行網(wǎng)關(guān),如下:

.netcore如何使用surging框架發(fā)布到docker

接下來新建一個(gè)解決方案,方案名隨意,喜歡就好,由于時(shí)間比較短,這里我簡單的處理,不清楚的可以留言

新建Service.A,然后在其下新建控制臺應(yīng)用Service.A、Service.B、Service.C,新建類庫Service.A.Service、Service.B.Service、Service.C.Service;

編輯Service.A.csporj、Service.B.csporj、Service.C.csporj,如下

.netcore如何使用surging框架發(fā)布到docker

將其中的引用包都復(fù)制過去,分別修改一下對應(yīng)的Service,即其中的<ProjectReference Include="..\Service.A.Service\Service.A.Service.csproj" />,Service.A引用Service.A.Service,Service.B引用Service.B.Service

Service.C引用Service.C.Service;

類庫Service.A.Service、Service.B.Service、Service.C.Service中都引用

<PackageReference Include="surging" Version="0.5.4" />

如圖:

.netcore如何使用surging框架發(fā)布到docker

由于代碼很多地方相識,以下我只說Service.A,和Service.A.Service;

Service.A 中新增Configs文件夾,下面添加log4net.config,log4net.config代碼如下:

<log4net>

 <root>

 <level value="Error" />

 <!-- <appender-ref ref="RollingLogFileAppender" /> -->

 <appender-ref ref="ErrorRollingLogFileAppender" />

 </root>

 <appender name="ErrorRollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net" LEVEL="ERROR">

 <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

 <param name="File" value="c:\surging\Error\" />

 <param name="AppendToFile" value="true" />

 <param name="RollingStyle" value="Composite" />

 <param name="DatePattern" value="_yyyyMMddHH.TXT" />

 <param name="StaticLogFileName" value="false" />

 <param name="MaxSizeRollBackups" value="-1" />

 <param name="MaximumFileSize" value="5MB" />

 <layout type="log4net.Layout.PatternLayout,log4net">

  <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />

 </layout>

 <filter type="log4net.Filter.LevelRangeFilter">

  <param name="LevelMin" value="ERROR" />

  <param name="LevelMax" value="FATAL" />

 </filter>

 </appender>

</log4net>

然后新增cacheSettings.json其中Map:Properties下的value的值是redis地址

{ 
 "CachingSettings": [
  {
  "Id": "ddlCache",
  "Class": "Surging.Core.Caching.RedisCache.RedisContext,Surging.Core.Caching",
  "Properties": [
   {
   "Name": "appRuleFile",
   "Ref": "rule"
   },
   {
   "Name": "dataContextPool",
   "Ref": "ddls_sample",
   "Maps": [
    {
    "Name": "Redis",
    "Properties": [
    {
     "value": "172.17.0.2:6379::1"
     }
    ]
    },
    {
    "Name": "MemoryCache"
    }
   ]
   },
   {
   "Name": "defaultExpireTime",
   "value": "120"
   },
   {
   "Name": "connectTimeout",
   "Value": "120"
   },
   {
   "Name": "minSize",
   "Value": "1"

   },
   {
   "Name": "maxSize",
   "Value": "10"
   }
  ]
  }
 ]
}

新增eventBusSettings.json,其中的EventBusConnection對應(yīng)的是RabbitMQ docker的地址

{
 "EventBusConnection": "172.17.0.3",
 "EventBusUserName": "guest",
 "EventBusPassword": "guest"
}

Program.cs的代碼如下

using Autofac;
using Surging.Core.Codec.MessagePack;
using Surging.Core.Consul;
using Surging.Core.Consul.Configurations;
using Surging.Core.CPlatform;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.DotNetty;
using Surging.Core.EventBusRabbitMQ;
using Surging.Core.Log4net;
using Surging.Core.ProxyGenerator;
using Surging.Core.ServiceHosting;
using Surging.Core.ServiceHosting.Internal.Implementation;
using System;
using System.Text;
namespace Service.A
{
 class Program
 {
  static void Main(string[] args)
  {
   NewMethod();
  }
  private static void NewMethod()

  {

   Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

   var host = new ServiceHostBuilder()

    .RegisterServices(builder =>

    {

     builder.AddMicroService(option =>

     {

      option.AddServiceRuntime();

      option.AddRelateService();

      //option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181"));

      option.UseConsulManager(new ConfigInfo("172.17.0.4:8500"));

      option.UseDotNettyTransport();

      option.UseRabbitMQTransport();

      option.AddRabbitMQAdapt();

      //option.UseProtoBufferCodec();

      option.UseMessagePackCodec();

      builder.Register(p => new CPlatformContainer(ServiceLocator.Current));

     });

    })

    .SubscribeAt()

    .UseLog4net("Configs/log4net.config")

    //.UseServer("127.0.0.1", 98)

    //.UseServer("127.0.0.1", 98,“true”) //自動(dòng)生成Token

    //.UseServer("127.0.0.1", 98,“123456789”) //固定密碼Token

    .UseServer(options =>

    {

     options.Ip = "172.17.0.6";

     options.Port = 9990;

     options.Token = "True";

     options.ExecutionTimeoutInMilliseconds = 30000;

     options.MaxConcurrentRequests = 200;

     options.NotRelatedAssemblyFiles = "Centa.Agency.Application.DTO\\w*|StackExchange.Redis\\w*";

    })

    .UseProxy()

    .UseStartup<Startup>()

    .Build();

   using (host.Run())

   {
    Console.WriteLine($"服務(wù)端啟動(dòng)成功,{DateTime.Now}。");
   }
  }
 }
}

新增Startup.cs

using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Surging.Core.Caching.Configurations;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.EventBusRabbitMQ.Configurations;
using System;
namespace Service.A
{
 public class Startup

 {
  public Startup()
  {
   var config = new ConfigurationBuilder()

   .SetBasePath(AppContext.BaseDirectory);

   ConfigureEventBus(config);

   //ConfigureCache(config);

  }
  public IContainer ConfigureServices(ContainerBuilder builder)
  {
   var services = new ServiceCollection();
   ConfigureLogging(services);
   builder.Populate(services);
   ServiceLocator.Current = builder.Build();
   return ServiceLocator.Current;
  }
  public void Configure(IContainer app)
  {
   app.Resolve<ILoggerFactory>()
     .AddConsole((c, l) => (int)l >= 3);

  }

  #region 私有方法
  /// <summary>
  /// 配置日志服務(wù)
  /// </summary>
  /// <param name="services"></param>
  private void ConfigureLogging(IServiceCollection services)
  {
   services.AddLogging();
  }
 
  private static void ConfigureEventBus(IConfigurationBuilder build)

  {
   build

   .AddEventBusFile("eventBusSettings.json", optional: false);
  } 

  /// <summary>
  /// 配置緩存服務(wù)
  /// </summary>
  private void ConfigureCache(IConfigurationBuilder build)
  {
   build
    .AddCacheFile("cacheSettings.json", optional: false);
  }
  #endregion
 }
}

Service.A.Service 類庫下新增AService.cs

using Surging.Core.ProxyGenerator;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Service.A.Service

{
 public class AService:ProxyServiceBase,IAService

 {
  public Task<string> SayHello(string name)

  {
   return Task.FromResult($"{name} say : hello");
  }
 }
}

新增IAService.cs

using Surging.Core.CPlatform.Ioc;
using Surging.Core.CPlatform.Runtime.Server.Implementation.ServiceDiscovery.Attributes;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Service.A.Service
{
 [ServiceBundle("api/{Service}")]
 public interface IAService : IServiceKey
 {
  Task<string> SayHello(string name); 
 }
}

其他類庫和服務(wù)與以上代碼基本無二,這里不在贅述。不清楚的可以留言

所有代碼都處理好后,在Service.A、Service.B、Service.C項(xiàng)目上右鍵新增docker支持文件,然后會(huì)生成一下文件

.netcore如何使用surging框架發(fā)布到docker

修改其中的docker-compose.yml

version: '3'

 

services:

 service.a:

 image: servicea

 ports:

  - "127.0.0.1:9990:9990"

 network_mode: "bridge"

 build:

  context: .

  dockerfile: Service.A/Dockerfile

 

 service.b:

 image: serviceb

 ports:

  - "127.0.0.1:9991:9991"

 network_mode: "bridge"

 build:

  context: .

  dockerfile: Service.B/Dockerfile

 

 service.c:

 image: servicec

 ports:

  - "127.0.0.1:9992:9992"

 network_mode: "bridge"

 build:

  context: .

  dockerfile: Service.C/Dockerfile

 

 webapplication1:

 image: webapplication1

 build:

  context: .

  dockerfile: ../WebApplication1/Dockerfile

然后選擇docker運(yùn)行即可

最后訪問surging網(wǎng)關(guān),即可看見效果

.netcore如何使用surging框架發(fā)布到docker

關(guān)于“.netcore如何使用surging框架發(fā)布到docker”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

本文題目:.netcore如何使用surging框架發(fā)布到docker
標(biāo)題鏈接:http://bm7419.com/article2/jcegic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、商城網(wǎng)站、微信公眾號、搜索引擎優(yōu)化、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)