Exemple #1
0
        static void Main(string[] args)
        {
            if (!Config.CheckEnvsExist(Keys))
            {
                Log.Color(LogTable.Msg7, ConsoleColor.Red);
                return;
            }

            //將需要的參數從環境變數中取出
            Dictionary <string, string> envs = Config.GetEnvironmentVariables();
            string RedisIP               = "localhost";
            string RedisChannelname      = envs[Keys[0]];
            string MQFilenameKeyInHeader = envs[Keys[1]];
            int    ReadinessPort         = int.Parse(envs[Keys[2]]);
            int    LivenessPort          = int.Parse(envs[Keys[3]]);

            //建立 Redis (一個連線) , RabbitMQ (一個連線) , K8sHeathCheck (2 個 tcp port)
            using (RedisClient redis = DBSetter.RedisConnect(RedisIP))
            {
                MQSetting mqSetting = MQListener.TryCreateByEnvs();

                void RabbitCallback(MQCallback mq)
                {
                    if (mq.Header != null && mq.Header.TryGetValue(MQFilenameKeyInHeader, out string header))
                    {
                        Console.WriteLine("header ConfigName : " + header);
                        Console.WriteLine(" Msg " + mq.Msg);

                        redis.Set(header, mq.Msg);
                        redis.Publish(RedisChannelname, header);
                    }
                    else
                    {
                        Console.WriteLine($"{LogTable.Msg2} {MQFilenameKeyInHeader}");
                    }
                }

                using (MQConnectObj mqConnection = MQListener.RabbitMQConnect(mqSetting, RabbitCallback))
                {
                    Console.WriteLine(LogTable.Msg1);

                    bool LivenessCheck()
                    {
                        try
                        {
                            redis.Ping();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            return(false);
                        }

                        return(redis.IsConnected && mqConnection.Connection.IsOpen);
                    }

                    heathChecker = new K8sTcpHeathCheck(ReadinessPort, LivenessPort,
                                                        () => { return(redis.IsConnected && mqConnection.Connection.IsOpen); },
                                                        LivenessCheck);
                    while (true)
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
        }
Exemple #2
0
        public static MQConnectObj RabbitMQConnect(MQSetting setting, Action <MQCallback> MQCallback)
        {
            ConnectionFactory factory = new ConnectionFactory()
            {
                UserName    = setting.UserName,
                Password    = setting.Password,
                HostName    = setting.HostName,
                Port        = setting.Port,
                VirtualHost = setting.VirualHost
            };

            IConnection connection = null;

            while (true)
            {
                try
                {
                    connection = factory.CreateConnection();
                    break;
                }
                catch (BrokerUnreachableException e)
                {
                    Log.Color(e.Message, ConsoleColor.Red);
                }
                Thread.Sleep((int)TimeSpan.FromSeconds(1).TotalMilliseconds);
            }
            Console.WriteLine(LogTable.Msg3);

            var channel = connection.CreateModel();

            channel.ExchangeDeclare(setting.ExchangeName, "fanout");
            var queueName = channel.QueueDeclare().QueueName;

            channel.QueueBind(queue: queueName, exchange: setting.ExchangeName, routingKey: "");

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                Dictionary <string, string> header = new Dictionary <string, string>();
                if (ea.BasicProperties.Headers != null)
                {
                    foreach (KeyValuePair <string, object> raw in ea.BasicProperties.Headers)
                    {
                        try
                        {
                            string s = Encoding.UTF8.GetString((byte[])raw.Value);
                            header.Add(raw.Key, s);
                        }
                        catch (InvalidCastException e)
                        {
                            Console.WriteLine(LogTable.Msg4);
                            continue;
                        }
                    }
                }
                MQCallback?.Invoke(new MQCallback()
                {
                    Header = header, Msg = Encoding.UTF8.GetString(ea.Body)
                });
            };
            channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
            return(new MQConnectObj()
            {
                Factory = factory, Connection = connection, Channel = channel
            });
        }