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. 深度剖析Javascript执行环境、作用域链

    一.执行环境 执行环境(也叫做执行上下文,Execution Context)是Javascript中最为重要的一个概念.执行环境定义了变量或函数有权访问其他数据,决定了它们各自的行为.每个执行环境都 ...

  2. 领扣(LeetCode)对称二叉树 个人题解

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  3. python:沙盒(virtualenv)

    当电脑需要使用多个版本的python时,可以使用沙盒:或者使用docker: virtualenv是Python自带的,通过pip安装的 [root@centos7 public]# cd jinji ...

  4. 就该这样理解 OSI 七层参考模型、浅谈不同局域网之间的通信

    简介 说到OSI参考模型,理解网络与网络之间的关系,不说太深入难以理解的东西,只求能最大程度上理解与使用. 参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系,一般称为O ...

  5. React-Hook

    import React, { useState } from 'react'; // Hook 写法 function App2 () { const [count,setCount] = useS ...

  6. mac如何开启两个vmware虚拟机

    转载链接:https://blog.csdn.net/aifore/article/details/87833088

  7. Nginx-(四)基本模块2

    nginx常用模块介绍(二) ngx_http_rewrite_module模块配置 (1)       rewrite  regex  replacement [flag]; 将请求的url基于正则 ...

  8. MySQL CRUD使用之小总结

    总结一下最近碰到的一些关于MySQL CRUD方面的语句. 在使用pymysql的executemany方法时,需要注意的几个问题: 1.在写sql语句时,不管字段为什么类型,占位符统一使用%s,且不 ...

  9. 基于 Vue3.0 Composition Api 快速构建实战项目

    Quick Start 项目源码:https://github.com/Wscats/vue-cli 本项目综合运用了 Vue3.0 的新特性,适合新手学习

  10. shell中的函数、shell中的数组、告警系统需求分析

    7月16日任务 20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析 20.16/20.17 shell中的函数 函数就是一个子shell就是一个代码段 ...