国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Web服務器 > 正文

.netcore 使用surging框架發布到docker

2024-09-01 13:54:16
字體:
來源:轉載
供稿:網友

demo運行在windows的docker中,系統是win10,所以需要先下載Docker for Windows,安裝完畢后系統會重啟,然后桌面上可以找到Docker for Windows的快捷圖標,右下角有個鯨魚小圖標

netcore,docker,aspnetcore

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

netcore,docker,aspnetcore

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

打開Kitematic,在搜索欄中下載好RabbitMQ、redis、consul相關鏡像,因為這些是surging運行的先決條件。

netcore,docker,aspnetcore

netcore,docker,aspnetcore

netcore,docker,aspnetcore

接著去GitHub上下載surging網關項目,修改其中的gatewaySettings.json中Register下的Address地址,對應的事consul docker鏡像的ip

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

打開Kitematic,點擊左下角,如圖:

netcore,docker,aspnetcore

進入到命令窗口,輸入docker container ls或者 docker ps -a 查看docker,

可以看到現在運行的docker的相關信息,

如圖:

netcore,docker,aspnetcore

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

顯示這個docker的配置,內容會很多,不過ip的信息在最后,如圖

netcore,docker,aspnetcore

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

修改好surging的網關配置后在Surging.ApiGateway項目上單擊右鍵,由于我項目中已經添加過,所以該處為灰色,如圖:

netcore,docker,aspnetcore

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

netcore,docker,aspnetcore

在后面添加docker的外部端口和內部端口的映射和網絡模式,這里我們都使用橋接模式,包括之前的consul、RabbitMQ、redis都是同一模式,這樣他們會在同一VLAN下,

然后運行網關,如下:

netcore,docker,aspnetcore

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

新建Service.A,然后在其下新建控制臺應用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,docker,aspnetcore

將其中的引用包都復制過去,分別修改一下對應的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,docker,aspnetcore

由于代碼很多地方相識,以下我只說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對應的是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”) //自動生成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($"服務端啟動成功,{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>  /// 配置日志服務  /// </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>  /// 配置緩存服務  /// </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);  }}

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

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

netcore,docker,aspnetcore

修改其中的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運行即可

最后訪問surging網關,即可看見效果

netcore,docker,aspnetcore

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梨树县| 浦县| 杂多县| 拉萨市| 余姚市| 凤台县| 公主岭市| 阿鲁科尔沁旗| 尼木县| 金门县| 灵武市| 惠水县| 天津市| 邵武市| 重庆市| 克什克腾旗| 巴青县| 同仁县| 资溪县| 邮箱| 玛沁县| 大丰市| 石林| 龙山县| 饶阳县| 大竹县| 五台县| 宜阳县| 民乐县| 根河市| 施甸县| 花莲市| 沭阳县| 车致| 江源县| 玉龙| 金坛市| 大关县| 唐海县| 柘城县| 额济纳旗|