Orleans getting started (1) getting started, orleans getting started
Return to navigation]
After a brief understanding of Orleans, we can use several examples to deepen our impression.
I. Orleans getting started example
This example is followed by Orleans getting started example (https://www.cnblogs.com/gaopang/articles/7379802.html)
1. Create
First, create a solution for four projects,
The four projects are:
Then use NuGet to reference Microsoft. Orleans. Server
Use NuGet to reference Microsoft. Orleans. Client
2. Encoding
IGrains. IBasic
public interface IBasic : IGrainWithIntegerKey { Task<string> SayHello(string hellostr); }
Grains. BasicGrain
public class BasicGrain : Grain, IGrains.IBasic { public Task<string> SayHello(string hellostr) { Console.WriteLine("{0} : {1}", DateTime.Now.ToString("HH:mm:ss.fff"), hellostr); return Task.FromResult<string>("done"); } }
Host. Program
Class Program {static void Main (string [] args) {// obtain a configuration instance // It requires two ports, and the first port 2334 is used for communication between silo and silo, the second 1234 is the var config = Orleans used to listen to client requests. runtime. configuration. clusterConfiguration. localhostPrimarySilo (2234,123 4); // initialize a silohost. Here we use the silohost provided by Orleans instead of silo. The name of silo is Ba; SiloHost siloHost = new SiloHost ("Ba ", config); // initialize the storage siloHost. initializeOrleansSilo (); // start siloHost. startOrleansSilo (); // check if (siloHost. isStarted) {Console. writeLine ("silohost started successfully");} else {Console. writeLine ("failed to start");} Console. readKey ();}}
Class Program {static void Main (string [] args) {// wait for the Console to be started on the server. readKey (); // then I intelligently typed the Enter key Run (); Console. readKey ();} static async void Run () {// obtain a configuration class using the built-in method. This class indicates that the server port is 1234 // you can use the configuration file, but here I will first use this simple configuration class var config = Orleans. runtime. configuration. clientConfiguration. localhostSilo (1234); // initialize a GrainClient. initialize (config); // obtain the BasicGrain interface IGrains from silo. IBasic agrain = GrainClient. grainFactory. getGrain <IGrains. IBasic> (314); // call the method in it and wait for it to return string result = await agrain. sayHello ("okay"); Console. writeLine (result );}}
3. Run
However, a problem occurs when running the Client.
I still don't know how to solve the problem. It's stuck here.
Subsequent solutions:
After receiving a big prompt from the blogger in the blog, I found that my host project did not reference the Grains project and IGrains project. After the reference, the entire project can be fully run.
Ii. Quick Start example
This example follows the Quick Start example in the Getting Started Guide to Microsoft Orleans (https://www.cnblogs.com/endv/p/6147976.html ).
1. Create
Similarly, it is to create a solution for four projects. Here we name it OrleansSamples
The project structure is the same as that in the previous example. Microsoft. Orleans. Server (Server package) and Microsoft. Orleans. Client (Client package) are also referenced)
The difference is that this example uses an interface different from the previous example.
2. Encoding
IUserService
public interface IUserService : IGrainWithIntegerKey { Task<bool> Exist(string mobileNumber); }
UserService
public class UserService : Grain, IUserService { public Task<bool> Exist(string mobileNumber) { return Task.FromResult<bool>(mobileNumber == "15665699774"); } }
Server
Class Program {static void Main (string [] args) {using (var host = new SiloHost ("Default") {host. initializeOrleansSilo (); host. startOrleansSilo (); Console. writeLine ("started successfully! "); Console. ReadLine (); host. StopOrleansSilo ();}}}
Client
Class Program {static void Main (string [] args) {System. threading. thread. sleep (15000); GrainClient. initialize (); while (true) {Console. writeLine ("Enter your mobile phone number:"); var mobileNumber = Console. readLine (); // here, because the grain we use inherits the IGrainWithIntegerKey, we use key = 10 of the numeric type to create this grain, // some people may ask what the key is. it uniquely identifies the grain. When you specify a key, Orleans will create one, it first goes to // your storage medium (if you have configured it, the memory storage is used by default. This method is suitable for development and the production environment needs to be kept in the status, therefore, you need to configure persistent Storage Place, such as sqlserver) // return directly if it is found. If it is not found, create a key based on the key you specified. This is grain activation, you can refer to the Grain chapter officially asked. Var userService = GrainClient. GrainFactory. GetGrain <IUserService> (10); // a new expression syntax of C #. This makes it much easier to concatenate strings. Console. WriteLine ($ "User {mobileNumber}, {(userService. Exist (mobileNumber). Result? "Already exists": "does not exist ")}");}}}
3. Run
To run this Orleans, you still need to do something
Add OrleansConfiguration. xml under the Server Directory
<?xml version="1.0" encoding="utf-8" ?><OrleansConfiguration xmlns="urn:orleans"> <Globals> <StorageProviders> <Provider Type="Orleans.Storage.MemoryStorage" Name="MemoryStore" /> <Provider Type="Orleans.Storage.MemoryStorage" Name="Default" /> <!--<Provider Type="Orleans.Storage.AzureTableStorage" Name="AzureStore"/>--> </StorageProviders> <SeedNode Address="localhost" Port="22222"/> <Messaging ResponseTimeout="30s"/> </Globals> <Defaults> <Networking Address="localhost" Port="22222"/> <ProxyingGateway Address="localhost" Port="40000" /> <Tracing DefaultTraceLevel="Info" TraceToConsole="true" TraceToFile="{0}-{1}.log" PropagateActivityId="false" BulkMessageLimit="1000"> <TraceLevelOverride LogPrefix="Application" TraceLevel="Info" /> <!--<TraceLevelOverride LogPrefix="Runtime.Dispatcher" TraceLevel="Verbose" /><TraceLevelOverride LogPrefix="AssemblyLoader.Silo" TraceLevel="Warning" />--> </Tracing> <Statistics MetricsTableWriteInterval="30s" PerfCounterWriteInterval="30s" LogWriteInterval="300s" WriteLogStatisticsToTable="true" StatisticsCollectionLevel="Info"/> </Defaults></OrleansConfiguration>
Add ClientConfiguration. xml under the Client Directory
<?xml version="1.0" encoding="utf-8" ?><ClientConfiguration xmlns="urn:orleans"> <Gateway Address="localhost" Port="40000"/> <!-- To turn tracing off, set DefaultTraceLevel="Off" and have no overrides. For the trace log file name, {0} is replaced by "Client" and {1} is the current time. --> <Tracing DefaultTraceLevel="Info" TraceToConsole="false" TraceToFile="{0}-{1}.log" BulkMessageLimit="1000"> <TraceLevelOverride LogPrefix="Runtime" TraceLevel="Info" /> <TraceLevelOverride LogPrefix="Application" TraceLevel="Info" /> <TraceLevelOverride LogPrefix="AssemblyLoader" TraceLevel="Warning" /> </Tracing> <Statistics MetricsTableWriteInterval="300s" PerfCounterWriteInterval="30s" LogWriteInterval="300s" WriteLogStatisticsToTable="true" StatisticsCollectionLevel="Info"/> <Messaging ResponseTimeout="30s" ClientSenderBuckets="8192" MaxResendCount="0"/></ClientConfiguration>
And set properties-advanced-copy to output directory-copy if the xml file is newer
After running the Server and Client respectively, you can get the results.
Now a simple single-service Orleans has been completed. In this example, we can briefly understand Orleans. However, there is no clue how to build a distributed cluster server. However, we continue to learn about the west region.