public override object invoke(object proxy, MethodInfo method, object[] args) { SuperMsgMulti msg = new SuperMsgMulti(args); byte[] body = this.serializer.SerializeBytes(msg); RabbitMQMsgFun rabbitMQMsgFun = (RabbitMQMsgFun)this.get(method.Name); if (rabbitMQMsgFun != null) { rabbitMQMsgFun.channel.BasicPublish(exchange: "", routingKey: rabbitMQMsgFun.FullName, basicProperties: rabbitMQMsgFun.properties, body: body); if (method.ReturnType != typeof(void)) { rabbitMQMsgFun.acquire(msg.Id); msg = rabbitMQMsgFun.getAndRemoveMsg(msg); if (null == msg) { return(Activator.CreateInstance(method.ReturnType)); } else {// this.serializer.DeSerializeString(method.ReturnType, new object().ToString()); return(this.serializer.DeSerializeString(method.ReturnType, msg.req.ToString())); } } } else { throw new NotImplementedException("未成功加载到方法,请仔细排查一下"); } return(Activator.CreateInstance(method.ReturnType));// this.serializer.DeSerializeString(method.ReturnType, new object().ToString()); }
public SuperMsgMulti getAndRemoveMsg(SuperMsgMulti superMsgMulti) { SuperMsgMulti smm; msgcd.TryGetValue(superMsgMulti.Id, out smm); msgcd.TryRemove(superMsgMulti.Id, out smm); return(smm); }
public void setMsg(SuperMsgMulti superMsgMulti) { msgcd.GetOrAdd(superMsgMulti.Id, superMsgMulti); }
public override void GetMathsInfo(Type t) { this.clear(); try { IConnection conection = factory.CreateConnection(); #region 方法二 MethodInfo[] info = t.GetMethods(); ///获取接口和类信息 this.interfaceFullName = t.FullName; for (int i = 0; i < info.Length; i++) { MsgFun mf = new MsgFun(); MethodInfo md = info[i]; //方法名 string mothodName = md.Name; Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name}"); //参数集合 ParameterInfo[] paramInfos = md.GetParameters(); if (md.Name.Equals("ToString") || md.Name.Equals("Equals") || md.Name.Equals("GetHashCode") || md.Name.Equals("GetType")) { continue; } RabbitMQMsgFun mfs = new RabbitMQMsgFun(); mfs.Name = md.Name; mfs.rep = md.ReturnType; mfs.FullName = version + "." + interfaceFullName + "." + mfs.Name; mfs.methodInfo = md; mfs.ReqFullName = version + "." + FullName + "." + mfs.Name; this.put(md.Name, mfs); for (int j = 0; j < paramInfos.Length; j++) { ParameterInfo parameterInfo = paramInfos[j]; mfs.req = parameterInfo.ParameterType; Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"入参" + j + ":" + parameterInfo.ParameterType}"); } IConnection connection = factory.CreateConnection(); IModel channel = connection.CreateModel(); mfs.channel = channel; String correlationId = mfs.ReqFullName; // 创建一个临时队列, 返回队列的名字 String replyQueue = channel.QueueDeclare().QueueName; IBasicProperties properties = channel.CreateBasicProperties(); properties.ReplyTo = replyQueue; properties.CorrelationId = correlationId; mfs.properties = properties; //创建消费者用于消息回调 var callbackConsumer = new EventingBasicConsumer(channel); //绑定临时队列的消费 channel.BasicConsume(queue: replyQueue, autoAck: true, consumer: callbackConsumer); callbackConsumer.Received += (model, ea) => { if (ea.BasicProperties.CorrelationId == correlationId) { //解码反回消息 SuperMsgMulti superMsg = this.serializer.DeSerializeString <SuperMsgMulti>(ea.Body.ToString()); var responseMsg = $"Get Response: {Encoding.UTF8.GetString(ea.Body.ToArray())}"; Console.WriteLine($"[x]: {responseMsg}"); } }; } #endregion } catch (Exception ex) { throw ex; } }
public override void GetMathsInfoMulti(Type t) { this.clear(); //var t = Type.GetType(className); try { IConnection conection = factory.CreateConnection(); #region 方法二 MethodInfo[] info = t.GetMethods(); List <Type> interfaces = t.GetInterfaces().Where(type => (!typeof(MsgController).IsAssignableFrom(type))).ToList(); if (interfaces.Count > 1 || interfaces.Count == 0) { throw new Exception("MsgController的实现类必须继承Sharing中共享的接口," + "且不能继承MsgController外的其他接口,且不能多层继承"); } for (int k = 0; k < interfaces.Count; k++) { ///获取接口和类信息 this.ControllerObj = Activator.CreateInstance(t); this.packageName = t.Namespace; this.FullName = t.FullName; this.className = t.Name; this.interfaceFullName = interfaces[k].FullName; IEnumerable <CustomAttributeData> eFRpcServiceAttribute = t.CustomAttributes.Where(type => type.AttributeType == typeof(EFRpcServiceAttribute)).ToList(); foreach (var a in eFRpcServiceAttribute) { foreach (var kv in a.NamedArguments) { if (kv.MemberName.Equals("version")) { this.version = kv.TypedValue.Value.ToString(); } } } for (int i = 0; i < info.Length; i++) { MsgFun mf = new MsgFun(); MethodInfo md = info[i]; //方法名 string mothodName = md.Name; Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name}"); //参数集合 ParameterInfo[] paramInfos = md.GetParameters(); if (md.Name.Equals("ToString") || md.Name.Equals("Equals") || md.Name.Equals("GetHashCode") || md.Name.Equals("GetType")) { continue; } MsgFun mfs = new MsgFun(); mfs.Name = md.Name; mfs.rep = md.ReturnType; mfs.methodInfo = md; this.put(md.Name, mfs); mfs.reqs = new Type[paramInfos.Length]; for (int j = 0; j < paramInfos.Length; j++) { ParameterInfo parameterInfo = paramInfos[j]; mfs.reqs[j] = parameterInfo.ParameterType; Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"入参" + j+1 + ":" + parameterInfo.ParameterType}"); } IModel channel = conection.CreateModel(); channel.QueueDeclare(queue: this.version + this.interfaceFullName + "." + md.Name, durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); Console.WriteLine("[*] Waiting for message."); consumer.Received += (model, ea) => { var properties = ea.BasicProperties; var replyProerties = channel.CreateBasicProperties(); replyProerties.CorrelationId = properties.CorrelationId; string s = ea.Body.ToString(); SuperMsgMulti superMsg = this.serializer.DeSerializeBytes <SuperMsgMulti>(ea.Body.ToArray()); //Console.WriteLine(superMsg.msg[0].ToString()); if (null != superMsg.msg && superMsg.msg.Length > 0) { object[] objs = new object[superMsg.msg.Length]; for (int j = 0; j < superMsg.msg.Length; j++) { //mfs.reqs.Length objs[j] = this.serializer.DeSerializeString(mfs.reqs[j], superMsg.msg[j].ToString()); } object rep = mfs.methodInfo.Invoke(this.ControllerObj, objs); if (md.ReturnType != typeof(void)) { channel.BasicPublish(exchange: "", routingKey: properties.ReplyTo, basicProperties: replyProerties, body: this.serializer.SerializeBytes(superMsg.setReq(rep)));//ProtobufSerializer.SerializeBytes(mfs.rep,rep) } } channel.BasicAck(ea.DeliveryTag, false); // Console.WriteLine($"Return result: {"消息:" + message}"); }; channel.BasicConsume(queue: this.version + this.interfaceFullName + "." + md.Name, autoAck: false, consumer: consumer); Console.WriteLine($"类名:{ t.Name}, {"方法名:" + md.Name},{"返回值" + ":" + md.ReturnType}"); } } #endregion } catch (Exception ex) { throw ex; } }
static void fun1(string[] args) { string rpc_queue = "v1.EF.RPC.Sharing.IMsgServer.GetSum"; var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { String correlationId = Guid.NewGuid().ToString(); // 创建一个临时队列, 返回队列的名字 String replyQueue = channel.QueueDeclare().QueueName; IBasicProperties properties = channel.CreateBasicProperties(); properties.ReplyTo = replyQueue; properties.CorrelationId = correlationId; string number = args.Length > 0 ? args[0] : "30"; var body = Encoding.UTF8.GetBytes(number); //发布消息 //channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: properties, body: body); Console.WriteLine($"[*] Request fib({number})"); // //创建消费者用于消息回调 var callbackConsumer = new EventingBasicConsumer(channel); //绑定临时队列的消费 channel.BasicConsume(queue: replyQueue, autoAck: true, consumer: callbackConsumer); callbackConsumer.Received += (model, ea) => { if (ea.BasicProperties.CorrelationId == correlationId) { //var responseMsg = $"Get Response: {Encoding.UTF8.GetString(ea.Body.ToArray())}"; //Console.WriteLine($"[x]: {responseMsg}"); } }; for (int i = 0; i < 10000; i++) { //Thread.Sleep(200); SuperMsgMulti superMsgMulti = new SuperMsgMulti(); GetMsgNumRequest msgNumRequest = new GetMsgNumRequest(); msgNumRequest.Num1 = i; msgNumRequest.Num2 = 99; superMsgMulti.setMsg(new object[] { msgNumRequest }); body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(superMsgMulti)); //body = Encoding.UTF8.GetBytes($"{ "msg":[{ \"Num1\":1,\"Num2\":398}],\"req\":null,\"Id\":\"b48a9ab2-2813-4868-a432-0f87606c8db0\",\"CreationDate\":\"2021-03-04T18:51:48.8286043+08:00\"}");//ProtobufSerializer.SerializeBytes< GetMsgNumRequest>( msgNumRequest);// Encoding.UTF8.GetBytes(msgNumRequest.ToString()); //发布消息 //默认路由,发布的制定rpc_queue队列 //配置参数带上临时队列的名字 channel.BasicPublish(exchange: "", routingKey: rpc_queue, basicProperties: properties, body: body); msgNumRequest.Num1 = i - 8; msgNumRequest.Num2 = 9; superMsgMulti.setMsg(new object[] { msgNumRequest }); body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(superMsgMulti)); channel.BasicPublish(exchange: "", routingKey: rpc_queue, basicProperties: properties, body: body); } Console.ReadLine(); } } }