Orleans的入门教程 

官方Hello World 地址

https://github.com/dotnet/orleans/tree/master/Samples/2.0/HelloWorld

Doc地址

http://dotnet.github.io/orleans/Documentation/tutorials_and_samples/Hello-World.html

手绘流程图

三个项目

主简仓 网关配置

使用的Nuget:

Microsoft.Extensions.Logging.Console  V2.1.1

Microsoft.Orleans.Server V2.1.2

using Microsoft.Extensions.Logging;
using Orleans.Configuration;
using Orleans.Hosting;
using System;
using System.Net;
using System.Threading.Tasks; namespace OrleansGateway
{
class Program
{
static void Main(string[] args)
{
var Host = StartHost(); bool IsExit = true;
while (IsExit)
{
string read = Console.ReadLine();
if (read == "Exit")
{
IsExit = false;
Host.Result.StopAsync();
}
}
} /// <summary>
/// 在本地启动一个Host
/// </summary>
/// <returns></returns>
static async Task<ISiloHost> StartHost() {
var builder = new SiloHostBuilder()
.UseLocalhostClustering()
.Configure<ClusterOptions>(options =>
{
//ClusterId为集群名称 相同的名称才能被分配到一个集群中
options.ClusterId = "dev";
//当前服务的名称
options.ServiceId = "Gateway";
})
.ConfigureLogging(logging => logging.AddConsole()); var host = builder.Build();
await host.StartAsync();
Console.WriteLine("启动成功");
return host;
}
}
}
UseLocalhostClustering()启动的地址是127.0.0.1 是不支持局域网连接的,如果需要配置外网支持 则嵌入已下代码
  static class Exstatic {
public static ISiloHostBuilder UseLocalNetworkhostClustering(
this ISiloHostBuilder builder,
int siloPort = EndpointOptions.DEFAULT_SILO_PORT,
int gatewayPort = EndpointOptions.DEFAULT_GATEWAY_PORT,
IPEndPoint primarySiloEndpoint = null)
{
builder.Configure<EndpointOptions>(options =>
{
options.AdvertisedIPAddress = GetInternalIp();
options.SiloPort = siloPort;
options.GatewayPort = gatewayPort;
}); builder.UseDevelopmentClustering(primarySiloEndpoint ?? new IPEndPoint(GetInternalIp(), siloPort));
builder.Configure<ClusterMembershipOptions>(options => options.ExpectedClusterSize = ); return builder;
}
public static IPAddress GetInternalIp()
{
IPHostEntry myEntry = Dns.GetHostEntry(Dns.GetHostName());
return myEntry.AddressList.FirstOrDefault(e => e.AddressFamily.ToString().Equals("InterNetwork")); }

结果:

支持类:

OrleansSupport

使用的Nuget:

Microsoft.Orleans.Core  V2.1.2

Microsoft.Orleans.OrleansCodeGenerator.Build V2.1.2

using Orleans;
using System;
using System.Threading.Tasks; namespace IGoods
{
public interface IGoods: IGrainWithIntegerKey
{
/// <summary>
/// 商品服务测试接口
/// </summary>
/// <returns></returns>
Task<string> GetGoodsDescribe(); }
}
using Orleans;
using System;
using System.Threading.Tasks; namespace IShoppingRecord
{
public interface IShoppingRecord: IGrainWithIntegerKey
{
/// <summary>
/// 购物记录服务测试接口
/// </summary>
/// <returns></returns>
Task<string> GetShoppingRecordDescribe();
}
}

Goods项目

引入项目IGoods IShoppingRecord

新建类库GoodsServer 引用 IGoods

使用的Nuget包

Microsoft.Orleans.Core V2.12

Microsoft.Orleans.OrleansCodeGenerator.Build V2.12

using Orleans;
using System;
using System.Threading.Tasks; namespace GoodsServer
{
public class GoodsServer : Grain, IGoods.IGoods
{
public Task<string> GetGoodsDescribe()
{
return Task.FromResult("商品服务调用成功");
}
}
}

新建Goods项目

新建GoodsServer类库 添加项目IGoods,IShoppingRecord

使用的Nuget包:

Microsoft.Orleans.Client V2.1.2

Microsoft.Orleans.Server V2.1.2

Microsoft.Extensions.Logging.Console  V2.1.1

using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks; namespace Goods
{
class Program
{
static void Main(string[] args)
{
//本服务开放端口
int silePort = ;
//主简仓网关端口
int gatewayPort = ;
//主简仓开放端口
int mainSiloPort = ; //由于向网关添加一个服务处理需要多一些时间
//所以在程序运行后马上获取服务可能会抛出获取不到的异常
//详情请看5、常见问题
var host = StartHost(silePort, gatewayPort, mainSiloPort);
var client = StartClient(gatewayPort);
while (true)
{
string ReadLine = Console.ReadLine();
if (ReadLine=="Exit")
{
host.Result.StopAsync().Wait();
client.Result.Close();
break;
}
else if (ReadLine=="Goods")
{
try
{
IGoods.IGoods goods = client.Result.GetGrain<IGoods.IGoods>();
Console.WriteLine(goods.GetGoodsDescribe().Result);
}
catch (Exception e)
{
Console.WriteLine("服务暂时还没有启动完成 请稍后再试"+e.Message);
}
}
else if (ReadLine == "ShoppingRecord")
{
try
{
IShoppingRecord.IShoppingRecord shoppingRecord = client.Result.GetGrain<IShoppingRecord.IShoppingRecord>();
Console.WriteLine(shoppingRecord.GetShoppingRecordDescribe().Result);
}
catch (Exception e)
{
Console.WriteLine("服务暂时还没有启动完成 请稍后再试" + e.Message);
}
}
} } /// <summary>
/// 在本地启动一个Host
/// </summary>
/// <returns></returns>
static async Task<ISiloHost> StartHost(int silePort,int gatewayPort,int mainSiloPort)
{
var builder = new SiloHostBuilder()//IPAddress.Loopback为127.0.0.1
//.UseLocalhostClustering()
.UseDevelopmentClustering(new IPEndPoint(IPAddress.Loopback, mainSiloPort))
.ConfigureEndpoints(siloPort: silePort, gatewayPort: gatewayPort)
.Configure<ClusterOptions>(options =>
{
//ClusterId为集群名称 相同的名称才能被分配到一个集群中
options.ClusterId = "dev";
//当前服务的名称
options.ServiceId = "GoodsServer";
}) //注入打印消息的入口
.ConfigureLogging(logging => logging.AddConsole()); //进行构建
var host = builder.Build();
//启动服务
await host.StartAsync();
Console.WriteLine("服务启动成功");
return host;
} /// <summary>
/// 连接Orleans仓库
/// </summary>
/// <param name="GatewayPort"></param>
/// <returns></returns>
static async Task<IClusterClient> StartClient(int gatewayPort) {
IClusterClient client = new ClientBuilder()
//与主简仓进行连接
.UseStaticClustering(new IPEndPoint[] { new IPEndPoint(IPAddress.Loopback, gatewayPort) })
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "GoodsClient";
})
//配置刷新简仓的时间 一般来说不会这么短
.Configure<GatewayOptions>(d => d.GatewayListRefreshPeriod = TimeSpan.FromSeconds())
.ConfigureLogging(logging => logging.AddConsole())
.Build();
await client.Connect();
Console.WriteLine("已经成功连上网关");
return client;
}
}
}

新建ShoppingRecord项目

新建ShoppingRecordServer类库添加项目IGoods,IShoppingRecord

使用的Nuget包:

Microsoft.Orleans.Client V2.1.2

Microsoft.Orleans.Server V2.1.2

Microsoft.Extensions.Logging.Console  V2.1.1

using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks; namespace ShoppingRecord
{
class Program
{
static void Main(string[] args)
{
//本服务开放端口
int silePort = ;
//主简仓网关端口
int gatewayPort = ;
//主简仓开放端口
int mainSiloPort = ;
var host = StartHost(silePort, gatewayPort, mainSiloPort); var client = StartClient(gatewayPort); //由于向网关添加一个服务处理需要多一些时间
//所以可能会抛出获取不到的异常
//详情请看5、常见问题 while (true)
{
string ReadLine = Console.ReadLine();
if (ReadLine == "Exit")
{ host.Result.StopAsync().Wait();
client.Result.Close();
break;
}
else if (ReadLine == "Goods")
{
try
{
IGoods.IGoods goods = client.Result.GetGrain<IGoods.IGoods>();
Console.WriteLine(goods.GetGoodsDescribe().Result);
}
catch (Exception e)
{
Console.WriteLine("服务暂时还没有启动完成 请稍后再试" + e.Message);
}
}
else if (ReadLine == "ShoppingRecord")
{
try
{
IShoppingRecord.IShoppingRecord shoppingRecord = client.Result.GetGrain<IShoppingRecord.IShoppingRecord>();
Console.WriteLine(shoppingRecord.GetShoppingRecordDescribe().Result);
}
catch (Exception e)
{
Console.WriteLine("服务暂时还没有启动完成 请稍后再试" + e.Message);
}
}
} } /// <summary>
/// 在本地启动一个Host
/// </summary>
/// <returns></returns>
static async Task<ISiloHost> StartHost(int silePort, int gatewayPort, int mainSiloPort)
{
var builder = new SiloHostBuilder()//IPAddress.Loopback为127.0.0.1
.UseDevelopmentClustering(new IPEndPoint(IPAddress.Loopback, mainSiloPort))
.ConfigureEndpoints(siloPort: silePort, gatewayPort: gatewayPort)
.Configure<ClusterOptions>(options =>
{
//ClusterId为集群名称 相同的名称才能被分配到一个集群中
options.ClusterId = "dev";
//当前服务的名称
options.ServiceId = "ShoppingRecordServer";
})
//注入打印消息的入口
.ConfigureLogging(logging => logging.AddConsole()); //进行构建
var host = builder.Build();
//启动服务
await host.StartAsync();
Console.WriteLine("服务启动成功");
return host;
} /// <summary>
/// 连接Orleans仓库
/// </summary>
/// <param name="gatewayPort"></param>
/// <returns></returns>
static async Task<IClusterClient> StartClient(int gatewayPort)
{
IClusterClient client = new ClientBuilder()
.UseStaticClustering(new IPEndPoint[] { new IPEndPoint(IPAddress.Loopback, gatewayPort) })
.Configure<ClusterOptions>(options =>
{
options.ClusterId = "dev";
options.ServiceId = "ShoppingRecordClient";
})
//配置刷新简仓的时间 一般来说不会这么短
.Configure<GatewayOptions>(d => d.GatewayListRefreshPeriod = TimeSpan.FromSeconds())
.ConfigureLogging(logging => logging.AddConsole())
.Build();
await client.Connect();
Console.WriteLine("已经成功连上网关");
return client;
}
}
}

结果

先跑主简仓

然后是Goods、 ShoppingRecord

注:这次配置的教程是非可靠部署的配置,也就是不需要三方存储信息 如(Sql、Azuer) 只需要知道网关端口就可以进行连接。官方Demo是本地开发配置,应该是不能有多个服务相互调用的

在连接网关成功后获取服务可能会抛出获取不到的错误  稍后试试就行了  在部分异常会有说明

文档地址:

http://dotnet.github.io/orleans/Documentation/clusters_and_clients/configuration_guide/typical_configurations.html

Demo下载地址

https://github.com/2821840032/MyOrleansDemo

Orleans的入门教程的更多相关文章

  1. Microsoft Orleans 之 入门指南

    Microsoft Orleans 在.net用简单方法构建高并发.分布式的大型应用程序框架. 原文:http://dotnet.github.io/orleans/ 在线文档:http://dotn ...

  2. wepack+sass+vue 入门教程(三)

    十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...

  3. wepack+sass+vue 入门教程(二)

    六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...

  4. wepack+sass+vue 入门教程(一)

    一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...

  5. Content Security Policy 入门教程

    阮一峰文章:Content Security Policy 入门教程

  6. gulp详细入门教程

    本文链接:http://www.ydcss.com/archives/18 gulp详细入门教程 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优 ...

  7. UE4新手引导入门教程

    请大家去这个地址下载:file:///D:/UE4%20Doc/虚幻4新手引导入门教程.pdf

  8. ABP(现代ASP.NET样板开发框架)系列之2、ABP入门教程

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之2.ABP入门教程 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...

  9. webpack入门教程之初识loader(二)

    上一节我们学习了webpack的安装和编译,这一节我们来一起学习webpack的加载器和配置文件. 要想让网页看起来绚丽多彩,那么css就是必不可少的一份子.如果想要在应用中增加一个css文件,那么w ...

随机推荐

  1. python学习-练习题

    1.使用while循环输入 1 2 3 4 5 6     8 9 10 # cat lx.py #!/usr/local/bin/python3.6 #邹姣姣 #使用while循环输入 1 2 3 ...

  2. 启动Spring Tool Suite 4时出现 could not find tools.jar spring boot live hovers....弹窗

    第一步:检查一下STS启动时的加载环境 Help  —› About Spring Tool Suite 4 —› Installation Details —› Configuration 本人已经 ...

  3. Python 面向对象-下篇

    面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...

  4. thinkphp 6.0 在 initialize 中重定向无效

    thinkphp 6.0 在 initialize 中重定向无效 改用 header() 函数 实例: // header('location:/index.php/模块/控制器/方法'); head ...

  5. nyoj 305 表达式求值 (递归)

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  6. nyoj 108-士兵杀敌(一)(数学)

    108-士兵杀敌(一) 内存限制:64MB 时间限制:1000ms 特判: No 通过数:60 提交数:221 难度:3 题目描述: 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. ...

  7. ASP.NET购物车Cookie获取,创建,添加,更新,删除的用法

    #region 添加购物车 public void GetShoppingCart(int skuId, int quanlity) { HttpCookie cookie = HttpContext ...

  8. ThreadLocal深度解析和应用示例

    开篇明意 ThreadLocal是JDK包提供的线程本地变量,如果创建了ThreadLocal<T>变量,那么访问这个变量的每个线程都会有这个变量的一个副本,在实际多线程操作的时候,操作的 ...

  9. 开始逆向objc基础准备(一)简单认识一下arm32,以及与x86汇编指令类比

    ARM32体系中有31或33个通用寄存器,没有特定的某种态下有r0-r15一共16个寄存器,快速中断态下有另一组r8-r12备份寄存器,在用户态和系统态之外其它态下都各自有一组r13-r14备份寄存器 ...

  10. bert+seq2seq 周公解梦,看AI如何解析你的梦境?【转】

    介绍 在参与的项目和产品中,涉及到模型和算法的需求,主要以自然语言处理(NLP)和知识图谱(KG)为主.NLP涉及面太广,而聚焦在具体场景下,想要生产落地的还需要花很多功夫. 作为NLP的主要方向,情 ...