Orleans在.net core的开发
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地址:
Orleans在.net core的开发的更多相关文章
- ASP.NET Core WebAPI 开发-新建WebAPI项目
ASP.NET Core WebAPI 开发-新建WebAPI项目, ASP.NET Core 1.0 RC2 即将发布,我们现在来学习一下 ASP.NET Core WebAPI开发. 网上已经有泄 ...
- ASP.NET Core Web开发学习笔记-1介绍篇
ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...
- [.net 面向对象程序设计深入](9).NET Core 跨平台开发环境搭建
[.net 面向对象程序设计深入](9).NET Core 跨平台开发环境搭建 1.概述 读前必备:认识.NET Core 上篇介绍了.NET 新的生态环境:包括.NET Framework..NET ...
- ASP.NET Core 企业级开发架构简介及框架汇总
企业开发框架包括垂直方向架构和水平方向架构.垂直方向架构是指一个应用程序的由下到上叠加多层的架构,同时这样的程序又叫整体式程序.水平方向架构是指将大应用分成若干小的应用实现系统功能的架构,同时这样的系 ...
- 使用.NET Core快速开发一个较正规的命令行应用程序
程序员的世界,命令行工具一直是"体验非常友好"的工具,也能自动化完成很多事情,同时还能结合shell来进行某项任务的批处理(脚本).在.NET Core中,命令行应用程序是基础,但 ...
- ASP.NET Core MVC+EF Core从开发到部署
笔记本电脑装了双系统(Windows 10和Ubuntu16.04)快半年了,平时有时间就喜欢切换到Ubuntu系统下耍耍Linux,熟悉熟悉Linux命令.Shell脚本以及Linux下的各种应用的 ...
- 《ASP.NET Core跨平台开发从入门到实战》Web API自定义格式化protobuf
<ASP.NET Core跨平台开发从入门到实战>样章节 Web API自定义格式化protobuf. 样章 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于 ...
- 使用Asp.Net Core MVC 开发项目实践[第一篇:项目结构说明]
先从下图看整体项目结构: Mango.Manager: 为后台管理项目 Mango.Web: 为前台项目 Mango.Framework.Core: 为常用的基础操作类项目 Mango.Framewo ...
- ASP.NET Core WebAPI 开发-新建WebAPI项目 转
转 http://www.cnblogs.com/linezero/p/5497472.html ASP.NET Core WebAPI 开发-新建WebAPI项目 ASP.NET Core We ...
随机推荐
- 生信 - 从repeatmasker传送门过来的 blast
以前有的是非完整时间写的博客,抽时间需要统一整理一下. 今天在重新装repeatmasker. 整个过程是这样的,有关联的事情有两个. 1. 装repeatmasker需要各种Prerequisite ...
- HTML 转 PDF 之 wkhtmltopdf
wkhtmltopdf是一个可以把html转为pdf的插件,有windows.linux等平台的版本,比较简单 官网下载 https://wkhtmltopdf.org/downloads.html ...
- nyoj 64-鸡兔同笼 (解二元一次方程)
64-鸡兔同笼 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:26 submit:58 题目描述: 已知鸡和兔的总数量为n,总腿数为m.输入n和m, ...
- Python之selenium自动化PART1
本文适合有经验的测试童鞋 一.Selenium自动化测试环境搭建 1.cmd --- pip install selenium==2.53.0 (如果selenium后面不跟==,表示默认安装最新版本 ...
- 构思一个在windows下仿objc基于动画层ui编程的ui引擎
用c/c++编程有些年了,十个指头可以数齐,在涉入iOS objc开发后,有种无比舒服的感觉,尤其在UI开发上. 在QuartzCore.framework下动画和透明窗口等许多效果的事都变得那么方便 ...
- vue常用指令总结
一.vue指令 官网解释 指令 (Directives) 是带有 v- 前缀的特殊特性.指令特性的值预期是单个 JavaScript 表达式 (v-for 是例外情况).指令的职责是,当表达式的值改变 ...
- Windows 10上源码编译glog和gflags 编写glog-config.cmake和gflags-config.cmake | compile glog and glags on windows from source
本文首发于个人博客https://kezunlin.me/post/bb64e398/,欢迎阅读! compile glog v0.3.5 and glags on windows from sour ...
- GeoServer 修改端口
准备内容 安装环境:win10*64位专业版 安装文件:geoserver-2.15.2 操作步骤 1.找到文件夹下的start.ini,并用记事本打开 2.找到jetty.port,修改为自己需要的 ...
- 微信小程序使用 ECharts 实现数据可视化
微信小程序使用 ECharts 显示图表 首先创建微信小程序 这里就不再赘述 下载 GitHub 上的 ecomfe/echarts-for-weixin 下载后解压,打开文件夹,里面的 ec-can ...
- Spring-boot(一)通过向导快速创建Spring-boot项目
通过向导快速创建Spring-boot项目 创建步骤: 选择Spring Initializr 填写组织和模块名 选择对应的模块 注:这里左侧的模块比较多,玩家可以根据自己的实际需要自由选择,此处暂时 ...