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 = ; int gatewayPort = ; int mainSiloPort = ; //启动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>();
if (TService==null)
{
throw new Exception("没有找到该类型" + TService.GetType());
}
return TService;
}
return TService;
}
} }

截图

Goods服务

Order服务

GitHub地址:

Demo下载

Orleans在.net core的开发的更多相关文章

  1. ASP.NET Core WebAPI 开发-新建WebAPI项目

    ASP.NET Core WebAPI 开发-新建WebAPI项目, ASP.NET Core 1.0 RC2 即将发布,我们现在来学习一下 ASP.NET Core WebAPI开发. 网上已经有泄 ...

  2. ASP.NET Core Web开发学习笔记-1介绍篇

    ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...

  3. [.net 面向对象程序设计深入](9).NET Core 跨平台开发环境搭建

    [.net 面向对象程序设计深入](9).NET Core 跨平台开发环境搭建 1.概述 读前必备:认识.NET Core 上篇介绍了.NET 新的生态环境:包括.NET Framework..NET ...

  4. ASP.NET Core 企业级开发架构简介及框架汇总

    企业开发框架包括垂直方向架构和水平方向架构.垂直方向架构是指一个应用程序的由下到上叠加多层的架构,同时这样的程序又叫整体式程序.水平方向架构是指将大应用分成若干小的应用实现系统功能的架构,同时这样的系 ...

  5. 使用.NET Core快速开发一个较正规的命令行应用程序

    程序员的世界,命令行工具一直是"体验非常友好"的工具,也能自动化完成很多事情,同时还能结合shell来进行某项任务的批处理(脚本).在.NET Core中,命令行应用程序是基础,但 ...

  6. ASP.NET Core MVC+EF Core从开发到部署

    笔记本电脑装了双系统(Windows 10和Ubuntu16.04)快半年了,平时有时间就喜欢切换到Ubuntu系统下耍耍Linux,熟悉熟悉Linux命令.Shell脚本以及Linux下的各种应用的 ...

  7. 《ASP.NET Core跨平台开发从入门到实战》Web API自定义格式化protobuf

    <ASP.NET Core跨平台开发从入门到实战>样章节 Web API自定义格式化protobuf. 样章 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于 ...

  8. 使用Asp.Net Core MVC 开发项目实践[第一篇:项目结构说明]

    先从下图看整体项目结构: Mango.Manager: 为后台管理项目 Mango.Web: 为前台项目 Mango.Framework.Core: 为常用的基础操作类项目 Mango.Framewo ...

  9. ASP.NET Core WebAPI 开发-新建WebAPI项目 转

    转 http://www.cnblogs.com/linezero/p/5497472.html ASP.NET Core WebAPI 开发-新建WebAPI项目   ASP.NET Core We ...

随机推荐

  1. nyoj 458-小光棍数 (471)

    458-小光棍数 内存限制:64MB 时间限制:1000ms 特判: No 通过数:6 提交数:6 难度:1 题目描述: 最近Topcoder的XD遇到了一个难题,倘若一个数的三次方的后三位是111, ...

  2. python中的__str__和__repr__方法

    如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class A(object): def __init__(self,name,age): self.name=name se ...

  3. 优秀的github项目学习

    优秀的github项目学习 后期会陆续添加遇到的优秀项目 https://github.com/chaijunkun

  4. 16 Zabbix4.4.1系统告警“Zabbix agent is not available (for 3m)“

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 Zabbix4.4.1系统告警“Zabbix agent is not available (fo ...

  5. Vue过渡动画运用transition

    vue的过渡动画,主要是transition标签的使用,配合css动画实现的.官方文档css过渡 通过点击事件来切换show的值来改变显示的文本,下面的css通过进入离开时的在匀速状态下xxs(秒)下 ...

  6. [FPGA]Verilog利用PWM调制巧妙完成RGB三色彩虹呼吸灯(给简约的题目以美妙的解答)

    概述 实现彩虹呼吸灯 题目就是这么简短,但这是目前我碰到的最有意思的一道题目,因为他有无数种解决方法,并且每一种都是那么高级或者巧妙,比如 可以利用3路不同初相的PWM调制信号驱动三颗RGB灯重叠呼吸 ...

  7. 高德JS依赖分析工程及关键原理

    一.背景 高德 App 进行 Bundle 化后,由于业务的复杂性,Bundle 的数量非常多.而这带来了一个新的问题——Bundle 之间的依赖关系错综复杂,需要进行管控,使 Bundle 之间的依 ...

  8. c# 为什么会出现死锁?多线程死锁怎么解决

    出现死锁必须满足以下几个条件: 1.互斥条件:该进程拥有的资源,其他进程只能等待其释放. 2.不剥夺条件:该进程拥有的资源只能由它自己来释放. 3.请求和保持条件:请求其他的资源,同时自己拥有的资源又 ...

  9. 利用keytool、openssl生成证书文件

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/52225073 本文出自: [HansChen的博客] 用openssl指令逐步生成各 ...

  10. vscode启动黑屏

    今天打开vscode的时候突然就黑屏了,一脸懵 于是上网找了一下,根据这位博主的解决办法: https://blog.csdn.net/insgo/article/details/102975986 ...