using Com.Auth; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Orleans.Configuration; using Orleans.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection; namespace Orleans.SiloHost { class Startup { public IConfigurationRoot Configuration { get; internal set; } List<LoadItem> loads; internal void ConfigureService(IServiceCollection services) { services.Configure<List<LoadItem>>(Configuration.GetSection("loads")); var sp = services.BuildServiceProvider(); var optionAccess = sp.GetService<IOptions<List<LoadItem>>>(); loads = optionAccess.Value; services.AddDbContext<DbAuth>(option => { option.UseSqlite(Configuration["dbcon:com_auth"]); }); } internal void SiloSetting(ISiloBuilder isb) { //isb.UseLocalhostClustering(); isb.Configure<ClusterOptions>(options => { options.ClusterId = Fone.Orleans.OrleansBasic.DefaultClusterId; options.ServiceId = Fone.Orleans.OrleansBasic.DefaultServerId; }); isb.Configure<EndpointOptions>(options => { //这里的IP决定了是本机 还是内网 还是公网 //options.AdvertisedIPAddress = GetInternalIp(); //options.AdvertisedIPAddress = IPAddress.Loopback; options.AdvertisedIPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].MapToIPv6(); options.SiloPort = int.Parse(Configuration["endpoint:s2s_send_port"]); // gateway 的端口 options.GatewayPort = int.Parse(Configuration["endpoint:c2s_send_port"]); //监听的silo 远程连接点 options.GatewayListeningEndpoint = new IPEndPoint(options.AdvertisedIPAddress, options.GatewayPort); //监听的silo 远程端口连接点 options.SiloListeningEndpoint = new IPEndPoint(options.AdvertisedIPAddress, options.SiloPort); // 网关(client->silo)的监听终结点 //options.GatewayListeningEndpoint = new IPEndPoint(IPAddress.Any, int.Parse(Configuration["endpoint:c2s_listen_port"])); // silo之间的(silo->silo)的监听终结点 //options.SiloListeningEndpoint = new IPEndPoint(IPAddress.Any, int.Parse(Configuration["endpoint:s2s_listen_port"])); }); var assemblys = from i in loads where !string.IsNullOrWhiteSpace(i.location) select Assembly.Load(i.location); foreach (var item in assemblys) { Console.WriteLine(item.GetName()); isb.ConfigureApplicationParts(parts => parts.AddApplicationPart(item).WithReferences()); } isb.UseAdoNetClustering((AdoNetClusteringSiloOptions options) => { options.ConnectionString = Configuration["dbcon:cluster_mssql"]; options.Invariant = "System.Data.SqlClient"; }); //监听的主silo 远程连接点 为空则创建一个主silo连接点 //isb.UseDevelopmentClustering(new IPEndPoint(IPAddress.Parse("192.168.8.1"), 11111)); } internal void ConfigureLogging(ILoggingBuilder ilb) { ilb.AddConsole(); } } static public class HostEx { static public IHostBuilder ConfigByStartup(this IHostBuilder ihb) { var su = new Startup(); ihb.ConfigureLogging(ilb => { su.ConfigureLogging(ilb); }); ihb.ConfigureAppConfiguration(icb => { //icb.SetBasePath( // System.IO.Path.GetFullPath("../../../") // ); icb.AddJsonFile("appsettings.json"); su.Configuration = icb.Build(); }); ihb.ConfigureServices(services => { su.ConfigureService(services); }); ihb.UseOrleans(isb => { su.SiloSetting(isb); }); return ihb; } } }
这里值得一说的是下面这句:
options.AdvertisedIPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].MapToIPv6();
这句,之前用的是MapToIPv4()方法结果一值 报错,于是终于试了一把IPv6的,结果成功了,这是使用公网而非127.0.0.1的地址,意义很大因为silo集群一般情况是需要多个机器通信的这个公网ip则是非常必要的,现在几个端口走配置文件读,但是