Easynetq Use (ii) "Connect Rabbitmq,ssl connection, Logging"

Source: Internet
Author: User
Tags string format server port ssl connection rabbitmq

If you connect to a relational database, such as SQL Server. You'll find easynetq handling connections a bit strange. and relational database communications have always started with the client. Client opens a connection, issues an SQL command, processes the result if necessary, and then closes the connection. It is generally recommended that you maintain an open connection for as short a time as possible and disconnect the connection pool via the API.

Message broker sessions such as RABBITMQ are somewhat different because the connection tends to remain connected throughout the lifetime of the application. Typically you open a connection, create a subscription, and then open the connection, waiting for the message to arrive. EASYNETQ cannot guarantee that the agent will be available at all times. Instead, it uses a deferred connection method that polls the endpoint in the background thread until the connection succeeds. If the server is disconnected for any reason (possibly a network failure, perhaps RABBITMQ server itself), EASYNETQ will resume polling the endpoint until the reconnection is successful.

The standard practice is to create only one IBUs instance during the life cycle of your application. When your application shuts down, dispose of it.
Deferred connections to the RABBITMQ server are represented through the IBUs interface. Most EASYNETQ operations are methods on the IBUs. Create a IBUs instance as follows:

var bus = Rabbithutch.createbus ("Host=myserver;virtualhost=myvirtualhost;username=mike;password=topsecret")

This connection string is made up of key value pairs, formatted as follows Key=value, separated by semicolons. Only one of the required fields is host. The possible connection string values are:

Host (for example, Host=localhost or host=192.168.2.56 or host=myhost.mydomain.com) This field is required. If you want to specify that you want to connect to the server port, you use the standard format host:port (for example: host=myhost.com:5673). If you omit the port number, the AMQP default port is 5672. To connect to the RABBITMQ cluster, you need to specify that each cluster node be separated by commas (for example: host:myhost1.com,myhost2.com,myhost3.com). For more information please refer to cluster Support

VirtualHost (for example: Virtualhost=myvirturalhost) The default virtual host is '/'

Username (for example: username=mike) default is ' guest ' (for non ' localhost ' host you need to use other username)

Password (for example: Password=mysecret) defaults to ' guest '

Requestedhearbeat (for example: requesthearbeat=10) defaults to 10 seconds. No heartbeat set to 0

Prefetchcount (for example: prefetchcount=1) defaults to 50. This value is the number of messages sent to RABBITMQ before the EASYNETQ sends an ACK. No limit is set to 0 (not recommended). In order to maintain fairness and balance between consumers set to 1.

Publisherconfirms (for example: Publisherconfirms=true) is false by default. How do I turn on publisher confirms?

Persistentmessages (for example: Persistentmessages=false) defaults to True. This determines what kind of delivery_mode to use when sending messages. false=1,true=2. Setting to TRUE,RABBITMQ will persist the message to disk and will still exist after the server restarts. Set to False to increase performance gains.

Product (for example: product= important service in my reality) was introduced in Easynetq 0.27.3. The default value is the instance name of the bus.
The values entered here will be displayed in the RABBITMQ admin interface.

Platform (for example: Platform=my.fully.qualified.domain.name) was introduced in Easynetq 0.27.3. The default value is the hostname of the machine running the instance of the client processing bus. The values entered here will be online in the RABBITMQ admin-side interface.

The timeout (for example: timeout=60) mode value is 10 seconds. was introduced in Easynet 0.17. The resolvable type is the value of system.uint16 from 0 to 65535. No Limit timeout is set to 0. Throw system.timeoutexception when timeout occurs.

Close the connection, as long as the simple Dispose, as follows:

Bus. Dispose ();

This closes the connections, pipelines, consumers, and all other resources used by EASYNETQ.

EASYNETQ can be connected through SSL. Gordon Coulter, author of this guide, originally wrote in response to a question.

First, you must carefully base the steps in the https://www.rabbitmq.com/ssl.html article. I spent a lot of time trying to get this part of OpenSSL to work, and then I spent a lot of time in order to get him to work on my needs and not just a demo.

The first time you let EASYNETQ operate through SSL, the point dotnet code on the pages they can run for testing helps me a lot. I have a simple console program that includes rabbit and EASYNETQ code that will be shown below. In addition, rabbit logs was used.

When you connect, the connection page of the Admin interface displays a small SSL protocol label. You also see port 443 on the Overview tab's Port monitor. The

code is as follows:

var connection = new Connectionconfiguration (); Connection.
Port = 443; Connection.
UserName = "User"; Connection.
Password = "Pass"; Connection.

Product = "Ssltest";
var host1 = new Hostconfiguration (); Host1.
Host = "rmq1.contoso.com"; Host1.
Port = 443; Host1.
Ssl.enabled = true; Host1.
Ssl.servername = "rmq1.contoso.com"; Host1.
Ssl.certpath = "C:\\TMP\\MYCLIENT.P12"; Host1.

Ssl.certpassphrase = "secret";
var host2 = new Hostconfiguration (); Host2.
Host = "rmq2.contoso.com"; Host2.
Port = 443; Host2.
Ssl.enabled = true; Host2.
Ssl.servername = "rmq2.contoso.com"; Host2.
Ssl.certpath = "C:\\TMP\\MYCLIENT.P12"; Host2.

Ssl.certpassphrase = "secret"; Connection.

Hosts = new List

Although there is an SSL option property on the Connectionconfiguration, it is a good place to set SSL option properties on the Hostconfiguration object. Setting the SSL option on the Hostconfiguration object can support the cluster scenario. Note that we specified two Hostconfiguration objects in the above code. If one hangs, the EASYNETQ continuous connection feature will automatically connect to the next available host. Configuring SSL on this host does not appear to have any errors when connected.

If you only specify a host, you can choose to set the SSL option through the Hostconfiguration object, or you can set it directly with the Connectionconfiguration object.

Don't forget to call the Validate () method. I skipped there at first (basically I write hard code, so there may be no mistakes to verify). However, this method call actually goes back to applying a series of settings, in order to ensure that the connection operation is necessary.

Ssloption details see document Ssloption Class

You can try to configure and test with the official RABBITMQ. NET client to try to run the sample. First, make it clear that all of these settings are properly described in official RABBITMQ SSL documentation for. NET before any modifications are made to your project.

EASYNETQ provides a logger interface Ieasynetqlogger:

Public interface Ieasynetqlogger
{
    void Debugwrite (String format,params object[] args);
    void Infowrite (string format, params object[] args);
    void Errorwrite (string format, params object[] args);
    void Errorwrite (Exception Exception);
}

Logging is closed by default, and Nulllogger is registered as a specific implementation of Ieasynetqlogger.

There is a console logger (Consolelogger) that can be used for testing or debugging. However, it may not be used in production environment systems. Debug-level logging are very detailed, documenting all the information that will have a performance impact on your application. And that may not make much sense for those who don't have a deep understanding of AMQP and EASYNETQ.

You should provide your own implementation based on Ieasynetqlogger, logging information and error messages to your application log. The Rabbithutch.createbus method provides a overloads method that allows you to replace your own log component. See replacing EASYNETQ components. You can use this method to register your own logger into the bus. Examples are as follows:

var logger = new MyLogger ();//Ieasynetqlogger implementation.
var bus = Rabbithutch.createbus ("My connection string", X => x.register<ieasynetqlooger> (_ => Looger));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.