コード例 #1
0
        public RaftOption(IConfiguration configuration)
        {
            var appSetting = configuration.GetSection("AppSetting");
            //设定端口
            var port = Convert.ToInt32(appSetting.GetSection("Port").Value);
            var name = appSetting.GetSection("Name").Value;

            Console.Title = name + "   port:" + port;
            var log = new Logger();
            //读取配置文件
            var config = File.ReadAllText(Directory.GetCurrentDirectory() + @"\config.txt");

            _node = TcpRaftNode.GetFromConfig(1, config,
                                              Directory.GetCurrentDirectory() + $@"\DBreeze\{name}", port, log,
                                              (entityName, index, data) =>
            {
                Console.WriteLine($"{entityName}/{index} { Encoding.UTF8.GetString(data)}");
                return(true);
            });
            //to do现在的问题是,必需启动三个节点,另一个IP是通过配置文件来加载的,不够灵活。
            _node.Start();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var port = int.Parse(ConfigurationManager.AppSettings["port"]);
            var name = ConfigurationManager.AppSettings["name"];

            Console.Title = name + "port:" + port;
            log           = new Logger();
            var config = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + @"\config.txt");
            var node   = TcpRaftNode.GetFromConfig(1, config,
                                                   System.IO.Directory.GetCurrentDirectory() + $@"\DBreeze\{name}", port, log,
                                                   (entityName, index, data) =>
            {
                Console.WriteLine($"{entityName}/{index} { System.Text.Encoding.UTF8.GetString(data)}");
                return(true);
            });

            node.Start();

            while (true)
            {
                Console.WriteLine("输入发送的内容并回车:");
                node.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(Console.ReadLine()));
            }
        }
コード例 #3
0
        static void Scenario1(string[] args)
        {
            Console.WriteLine("Scenario 1 is running");
            if (args.Length < 1)
            {
                Console.WriteLine("RaftCluster TCP listening port is not specified");
                return;
            }

            if (args.Length < 2 || !(new System.IO.FileInfo(args[2])).Exists)
            {
                Console.WriteLine("Path to configuration file is not supplied or file is not found");
                return;
            }

            string dbreezePath = "";

            if (args.Length >= 3)
            {
                dbreezePath = args[3];
            }

            Console.WriteLine($"Listening port: {args[1]}; Path to config: {args[2]}; Path to DBreeze folder: {args[3]}");
            var configLines = System.IO.File.ReadAllLines(args[2]);



            TcpRaftNode rn = null;

            //rn = TcpRaftNode.GetFromConfig(1, System.IO.File.ReadAllText(args[2]),
            //    dbreezePath, Convert.ToInt32(args[1]), log,
            //    (entName, index, data) => { Console.WriteLine($"wow committed {entName}/{index}"); return true; });

            rn = TcpRaftNode.GetFromConfig(System.IO.File.ReadAllText(args[2]),
                                           dbreezePath, Convert.ToInt32(args[1]), log,
                                           (entName, index, data) => { Console.WriteLine($"wow committed {entName}/{index}"); return(true); });

            rn.Start();

            AddLogEntryResult addRes = null;

            while (true)
            {
                var cmd = Console.ReadLine();
                switch (cmd)
                {
                case "set1":
                    addRes = rn.AddLogEntry(new byte[] { 23 });
                    Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    break;

                case "set1a":
                    addRes = rn.AddLogEntry(new byte[] { 27 }, entityName: "inMemory1");
                    Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    break;

                case "set10":
                    for (int k = 0; k < 10; k++)
                    {
                        addRes = rn.AddLogEntry(new byte[] { 23 });
                        Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    }
                    break;

                case "set10a":
                    for (int k = 0; k < 10; k++)
                    {
                        addRes = rn.AddLogEntry(new byte[] { 23 }, entityName: "inMemory1");
                        Console.WriteLine($"Adding: {addRes.AddResult.ToString()}");
                    }
                    break;
                }
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            log = new Logger();

            var         config = System.IO.File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + @"\config.txt");
            TcpRaftNode rn1    = TcpRaftNode.GetFromConfig(1, config,
                                                           System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node1", 4250, log,
                                                           (entityName, index, data) =>
            {
                Console.WriteLine($"Committed {entityName}/{index}");
                Console.WriteLine("0000000000000      4250数据:" + System.Text.Encoding.UTF8.GetString(data));
                return(true);
            });

            TcpRaftNode rn2 = TcpRaftNode.GetFromConfig(1, config,
                                                        System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node2", 4251, log,
                                                        (entityName, index, data) =>
            {
                Console.WriteLine($"Committed {entityName}/{index}");
                Console.WriteLine("111111111111      4251数据:" + System.Text.Encoding.UTF8.GetString(data));
                var random = new Random();
                var num    = random.Next(1, 3) % 2;
                Console.WriteLine(num);

                if (num == 1)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            });

            TcpRaftNode rn3 = TcpRaftNode.GetFromConfig(1, config,
                                                        System.IO.Directory.GetCurrentDirectory() + @"\DBreeze\Node3", 4252, log,
                                                        (entityName, index, data) =>
            {
                Console.WriteLine($"Committed {entityName}/{index}");
                Console.WriteLine("222222222222       4252数据:" + System.Text.Encoding.UTF8.GetString(data));
                return(true);
            });

            rn1.Start();
            rn2.Start();
            rn3.Start();


            while (true)
            {
                Console.Clear();
                Console.WriteLine("选择要发送的:1、4250     2、4251     3、4252");
                switch (Console.ReadLine())
                {
                case "1":
                    rn1.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1");
                    break;

                case "2":
                    rn2.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1");
                    break;

                case "3":
                    rn3.AddLogEntry(System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString()), "inMemory1");
                    break;
                }
            }
        }