8.rabbitmq RPC模拟微服务架构中的服务调用

标题 :
8.rabbitmq RPC模拟微服务架构中的服务调用
目录 :
RabbitMQ
序号 :
8

    {
        var connectionFactory = new ConnectionFactory
        {
            Port = 5672,
            VirtualHost = "test",
            HostName = "192.168.161.180",
            UserName = "test",
            Password = "123456",
            AutomaticRecoveryEnabled = true,
            TopologyRecoveryEnabled = true
        };



const string queueName = "rpc-queue";


var connection = connectionFactory.CreateConnection();
var channel = connection.CreateModel();

//设置服务质量
channel.BasicQos(0, 1, false);


//定义一个队列,将用于存放文件信息
channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);


var client = new SimpleRpcClient(channel, string.Empty, ExchangeType.Direct, queueName);


Console.WriteLine("开始RPC调用,调用的方法为 order.queryWithId");
var basicProperties = new BasicProperties { Headers = new Dictionary<string, object>() };
basicProperties.Headers.Add("method", "order.queryWithId");
const string id = "789852";
var body = Encoding.UTF8.GetBytes(id);
var rpcCallResBytes = client.Call(basicProperties, body);
var rpcCallResString = Encoding.UTF8.GetString(rpcCallResBytes.Body);
Console.WriteLine($"RPC调用完毕,返回结果是:{Environment.NewLine} {rpcCallResString}");


Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}

![](https://www.showdoc.cc/server/api/common/visitfile/sign/e97ad84645cdaf5ce88daff74a85e1bf?showdoc=.jpg)
​
###引用链接
https://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html
请尽量按照自己期望的生活 email:18980489167@189.cn
原文地址:https://www.cnblogs.com/gytangyao/p/11406104.html