/// <summary> /// 消费者接收字符串 /// </summary> static void ConsumerReceString() { var conn = SingleConnectionTool.CreateConnectionFromConfigName("MessageQueue:RabbitConnectionString_vtest"); var consumer = conn.CreateConsumer("TestExchange2"); //IConsumer consumer = SingleConnectionTool.Connection.CreateConsumer("TestQueue1");// 此处是队列名 consumer.Subscribe((string str) => { Console.WriteLine("a msg:" + str); return(true); }); // 退出要把连接都关闭,此处演示为注释 //SingleConnectionTool.Close(); }
/// <summary> /// 生产者发布字符串 /// </summary> static void PublishString() { IProducer producer = SingleConnectionTool.Connection.CreateProducer("TestExchange");//此处是交换机名称 while (true) { string msg = Console.ReadLine(); if ("quit".Equals(msg)) { // 退出要把所有资源都关闭 producer.Close(); SingleConnectionTool.Close(); return; } producer.Publish(msg, "TestKey1");//此处是绑定key } }
/// <summary> /// 生产者发布字符串 /// </summary> static void PublishString() { //IProducer producer = SingleConnectionTool.Connection.CreateProducer("TestExchange");//此处是交换机名称 var conn = SingleConnectionTool.CreateConnectionFromConfigName("MessageQueue:RabbitConnectionString_vtest"); IProducer producer = conn.CreateProducer("TestExchange2");//此处是交换机名称 while (true) { string msg = Console.ReadLine(); if ("quit".Equals(msg)) { // 退出要把所有资源都关闭 producer.Close(); SingleConnectionTool.Close(); return; } producer.Publish(msg, "TestKey1");//此处是绑定key } }
/// <summary> /// 调用 /// </summary> /// <param name="method">方法</param> /// <param name="message">消息</param> /// <returns>返回数据</returns> public object Call(MethodInfo method, object message) { var assemblyName = method.DeclaringType.Assembly.GetName().Name; var configInfo = rabbitConfigReader.Reader(); if (configInfo == null) { throw new Exception("找不到RabbitConfig配置信息"); } RpcClientAssemblyWrapInfo rpcClientAssemblyWrap; var messQueueInfo = GetMessageQueueByAssemblyName(configInfo, assemblyName, out rpcClientAssemblyWrap); if (rpcClientAssemblyWrap == null) { throw new Exception($"找不到程序集[{assemblyName}]的RPC客户端配置信息"); } var byteSeri = GetByteSerialization(rpcClientAssemblyWrap.RpcClientAssembly.DataType); var byteData = byteSeri.Serialize(message); IRpcClient rpcClient = null; if (dicVirtualPathMapRpcClient.ContainsKey(rpcClientAssemblyWrap.RabbitVirtualPath.VirtualPath)) { rpcClient = dicVirtualPathMapRpcClient[rpcClientAssemblyWrap.RabbitVirtualPath.VirtualPath]; } else { lock (syncDicVirtualPathMapRpcClient) { IMessageQueueConnection conn = null; // 如果连接配置信息不全,则找默认的连接对象 if (string.IsNullOrWhiteSpace(rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionString) && string.IsNullOrWhiteSpace(rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionStringAppConfigName)) { conn = SingleConnectionTool.CreateConnection(); } else { conn = connectionFactoy.CreateAndOpen(new ConnectionWrapInfo() { ConnectionString = rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionString, ConnectionStringAppConfigName = rpcClientAssemblyWrap.RabbitVirtualPath.ConnectionStringAppConfigName }); } connections.Add(conn); rpcClient = conn.CreateRpcClient(messQueueInfo); try { dicVirtualPathMapRpcClient.Add(rpcClientAssemblyWrap.RabbitVirtualPath.VirtualPath, rpcClient); } catch (ArgumentException) { } } } var reData = rpcClient.Call(byteData); // 如果返回值是空或者类型是void,则直接返回null if (reData.IsNullOrLength0() || method.ReturnType.IsTypeVoid()) { return(null); } else { Type targetType = null; // 判断返回类型是否Task if (method.ReturnType.IsTypeNotGenericityTask()) { return(null); } else if (method.ReturnType.IsTypeGenericityTask()) { targetType = method.ReturnType.GetProperty("Result").PropertyType; } else { targetType = method.ReturnType; } return(byteSeri.Deserialize(reData, targetType)); } }