4.4 通过虚拟机连接到ActiveMQ(VM连接器) -Connecting to ActiveMQ inside the virtual machine(VM connector)
The VM transport connector is used by Java applications to launch an embedded broker
and connect to it. Use of the VM transport means that no network connections are
created between clients and the embedded broker. Communication is performed
through direct method invocations of the broker object. Because the network stack
isn’t employed, performance improves significantly. The broker is started when the
first connection is created using the VM protocol. All subsequent VM transport connections
from the same virtual machine will connect to the same broker.
A broker created using the VM protocol doesn’t lack any of the standard ActiveMQ
features. So, for example, the broker can be configured with other transport connectors
as well. When all clients that use the VM transport to the broker close their connections,
the broker will automatically shut down.
Java应用程序中的VM传输连接器用于启动并连接到一个内嵌的代理.使用VM传输连接器意味着客户端和内嵌
带代理之间不需要网络连接,通过直接调用代理对象的方法来实现通信.因为使用VM连接器后不需要网络协议
的参与,所以性能显著提高.使用VM协议首次连接到代理时会启动代理,同一个虚拟机中所有后续的VM传输连接都
将连接到这个代理.使用VM协议的代理具有标准ActiveMQ代理的所有特性.当所有使用VM传输连接到代理的客户端
都关闭连接后,代理自动关闭.
The URI syntax for the VM transport is as follows:
vm://brokerName?key=value
The broker name plays an important role in the VM transport connector URI by
uniquely identifying the broker. For example, you can create two different embedded
brokers by specifying different broker names. This is the only required difference.
Transport options are set using the query part of the URI, the same as the previously
discussed transports. The complete reference for this connector can be found at
the ActiveMQ website (http://mng.bz/716b).
配置VM连接器的URI语法如下:
vm://brokerName?key=value
URI中brokerName配置非常重要,它是代理的唯一标识.例如,你可以通过配置两个不同的broker name
创建两个不同的嵌入式代理,代理名称不能相同是唯一的要求(译注:其他配置参数可以相同).
与前文讨论的传输连接器一样,传输连接器的选项通过URI的查询字符串部分配置.VM连接器的完整
文档请参阅ActiveMQ网站(http://mng.bz/716b).
The important thing about options for the VM transport protocol is that you can
use them to configure the broker to some extent. Options whose name begins with
the prefix broker. are used to tune the broker. For example, the following URI starts
up a broker with persistence disabled (message persistence is explained in chapter 5):
vm://broker1?marshal=false&broker.persistent=false
There’s also an alternative URI syntax that can be used to configure an embedded
broker:
vm:broker:(transportURI,network:networkURI)/brokerName?key=value
The complete reference of the broker URI can be found at the ActiveMQ website
(http://mng.bz/FNos).
关于VM传输协议选项的一个比较重要的方面是,你可以通过配置这些选项来为代理增加一些扩展功能.
选项名称以broker.开头的配置项用于调整代理相关功能.比如,配置下面的RUI的代理不具备消息持久化功能(
消息持久化将在第5章介绍): vm://broker1?marshal=false&broker.persistent=false
还可以用下面这种URI语法来配置嵌入式代理:
vm:broker:(transportURI,network:networkURI)/brokerName?key=value
代理的URI相关的完整文档请参阅ActiveMQ网站(http://mng.bz/FNos).
As you can see, this kind of URI can be used to configure additional transport connectors.
Take a look at the following URI, for example:
vm:broker:(tcp://localhost:6000)?brokerName=embeddedbroker&persistent=false
Here, we’ve defined an embedded broker named embeddedBroker and also configured
a TCP transport connector that listens for connections on port 6000. Finally, persistence
is also disabled in this broker. Figure 4.4 can help you better visualize this
example configuration. This figure demonstrates that clients connecting to the broker
from within the application that embeds the broker will use the VM transport,
whereas external applications connect to that embedded broker using the TCP connector,
just as they would in the case of any standalone broker.
正如你所看到的那样,这种类型的URI可用来配置额外的传输连接器.例如,下面的RUI:
vm:broker:(tcp://localhost:6000)?brokerName=embeddedbroker&persistent=false
这里,我们定义了一个名称为embeddedBroker的代理,同时配置了一个TCP连接器在6000端口
监听连接.这个代理中消息持久化同样被禁用了.图4.4能帮助你更好的理解这个配置.这个图说明
(来自同一虚拟机的)应用程序内部的客户端通过VM传输连接器连接到代理,
而外部应用程序通过TCP连接器连接到这个嵌入的代理,就像连接到其他独立的代理一样.
An embedded broker using an external configuration file can be achieved using
the brokerConfig transport option and by specifying the URI for the activemq.xml
file. Here’s an example:
vm://localhost?brokerConfig=xbean:activemq.xml
The example will locate the activemq.xml file in the classpath using the xbean: protocol.
Using this approach, an embedded broker can be configured just like a standalone
broker using the XML configuration.
通过配置传输选项的brokerConfig参数,在URI中指定activemq.xml作为配置文件,
可以使用外部配置文件来配置嵌入式代理.下面是配置例子:
vm://localhost?brokerConfig=xbean:activemq.xml
该实例的配置会在类路径中根据xbean:协议查找activemq.xml文件.通过这种方式,使用XML作为配置文件
可以像配置一个独立的代理那样来配置嵌入式代理.
Now the stock portfolio publisher can be started with an embedded broker using
the following command:
现在,可以使用下面的命令启动一个嵌入式代理,然后运行让stock portfolio例子中的publisher:
$ mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Publisher -Dexec.args=”vm://localhost CSCO ORCL”
…
Sending: {price=65.713356601409, stock=JAVA, offer=65.779069958011, up=true} on destination: topic://STOCKS.JAVA
Sending: {price=66.071605671946, stock=JAVA, offer=66.137677277617, up=true} on destination: topic://STOCKS.JAVA
Sending: {price=65.929035001620, stock=JAVA, offer=65.994964036622, up=false} on destination: topic://STOCKS.JAVA
…
Note that the publisher works just fine without having to start an external broker.
One obvious advantage of the VM transport is improved performance for client-to-broker
communication. Also, you’ll have only one Java application to run (one JVM)
instead of two, which can ease your deployment process. This also means that there’s
one fewer Java process to manage. So, if you plan to use the broker mainly from one
application, maybe you should consider using the embedded broker. Embedding
ActiveMQ is covered in detail in chapter 8.
注意,这里没有启动一个外部代理时,但publisher仍然能够正常工作.使用VM传输连接器的一个显著好处是
提升客户端-代理之间通信的性能.同样,你将只需要运行一个Java程序(只需要启动一个Java虚拟机),而不是两个,
同时,这也将简化你的程序发布过程.这也意味着需要管理更小的Java进程.因此,假如只需要在一个应用程序
中使用代理,你应该考虑使用嵌入式代理.嵌入ActiveMQ的详细信息将在第8章中讨论.
On the other hand, if too many Java applications that use embedded brokers exist,
maintenance problems may arise when trying to consistently configure each broker as
well as back up the data. In such situations, it’s always easier to create a small cluster of
standalone brokers instead of using embedded brokers.
另一方面,如果有过多的Java程序使用了嵌入式代理,可能会产生诸如配置代理之间的同步以及备份数据这种
维护方面的问题.这种情况下,通常应该创建一个小型的独立代理集群以替代嵌入式代理.
Having one ActiveMQ broker to serve all your application needs works well for
most situations. But some environments need advanced features, such as high availability
and larger scalability. This is typically achieved using what’s known as a network
of brokers. In the following section you’ll learn about networks of brokers and network
connectors used to configure those networks.
大多数情况下,使用一个ActiveMQ代理就能很好的满足所有的应用需求.但是,有些环境下需要更高级的功能,
比如需要高可用性和可扩展性的环境.使用缩位的代理网络可以满足这种环境下的需求.在接下来的章节中
你将学习代理网络以及用于配置这些网络的网络连接器.
赞 赏微信赞赏 支付宝赞赏
本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/754.html | 边城网事