

Goods 服務 啟動
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection; using System.Threading; using System.Threading.Tasks; using Entity; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Orleans; using Orleans.Configuration; using Orleans.Hosting; using Orleans.Serialization; namespace Order { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); int silePort = 11113; int gatewayPort = 30000; int mainSiloPort = 11111; //啟動Host StartAsyncHost(silePort, gatewayPort, mainSiloPort, services); //啟動本地服務 StartAsyncClient(mainSiloPort, gatewayPort, silePort, services).Wait(); services.ServerInjectionADD(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseMvc(); } /// <summary> /// 在本地啟動一個Host /// </summary> /// <returns></returns> async Task<ISiloHost> StartAsyncHost(int silePort, int gatewayPort, int mainSiloPort, IServiceCollection servicesCollection) { var builder = new SiloHostBuilder() .Configure<SerializationProviderOptions>(d => { d.SerializationProviders.Add(typeof(ProtobufSerializer).GetTypeInfo()); d.FallbackSerializationProvider = typeof(ProtobufSerializer).GetTypeInfo(); }) .UseDevelopmentClustering(new IPEndPoint(IPAddress.Loopback, mainSiloPort)) .ConfigureEndpoints(siloPort: silePort, gatewayPort: gatewayPort) .Configure<ClusterOptions>(options => { //ClusterId為集群名稱 相同的名稱才能被分配到一個集群中 options.ClusterId = "dev"; //當前服務的名稱 options.ServiceId = "GoodsServer"; }) .AddStartupTask( async (IServiceProvider services, CancellationToken cancellation) => { var grainFactory = services.GetRequiredService<IGrainFactory>(); //var grain = grainFactory.GetGrain<IMyGrain>(0); //注冊本機服務 }) //注入打印消息的入口 .ConfigureLogging(logging => logging.AddConsole()); //進行構建 var host = builder.Build(); //啟動服務 await host.StartAsync(); Console.WriteLine("服務啟動成功"); return host; } async Task StartAsyncClient(int mainSiloProt, int gatewayProt, int siloProt, IServiceCollection servicesCollection) { IClusterClient client = new ClientBuilder() .Configure<SerializationProviderOptions>(d => { d.SerializationProviders.Add(typeof(ProtobufSerializer).GetTypeInfo()); d.FallbackSerializationProvider = typeof(ProtobufSerializer).GetTypeInfo(); }) //與主簡倉進行連接 .UseStaticClustering(new IPEndPoint[] { new IPEndPoint(GetInternalIp(), gatewayProt) }) .Configure<ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "GoodsClient"; }) //配置刷新簡倉的時間 一般來說不會這么短 //.Configure<GatewayOptions>(d => d.GatewayListRefreshPeriod = TimeSpan.FromSeconds(5)) .ConfigureLogging(logging => logging.AddConsole()).Build(); await client.Connect(); Console.WriteLine("Orleans客戶端已經啟動"); servicesCollection.AddSingleton(client); } public static IPAddress GetInternalIp() { IPHostEntry myEntry = Dns.GetHostEntry(Dns.GetHostName()); return myEntry.AddressList.FirstOrDefault(e => e.AddressFamily.ToString().Equals("InterNetwork")); } } }
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Orleans; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Order { /// <summary> /// 注入本地服務 /// </summary> public static class ServerInjection { /// <summary> /// 服務注入 在最后使用 /// </summary> /// <param name="services"></param> public static void ServerInjectionADD(this IServiceCollection services) { services.AddSingleton<IShoppingRecord.IShoppingRecord, OrderServer.OrderServer>(); //引用對象留在最后 serviceProvider = services.BuildServiceProvider(); } public static ServiceProvider serviceProvider { get; set; } public static T GetServer<T>(this Controller controller) where T:class, IGrainWithIntegerKey { var TService = serviceProvider.GetService<T>(); if (TService==null) { var client = serviceProvider.GetService<IClusterClient>(); if (client==null) { throw new Exception("客戶端沒有啟動 或者沒有被注入"); } TService = client.GetGrain<T>(0); if (TService==null) { throw new Exception("沒有找到該類型" + TService.GetType()); } return TService; } return TService; } } }
截圖
Goods服務

Order服務

GitHub地址:
