、Mavenの依存関係
<親> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータ親</たartifactId> <バージョン> 2.1.7.RELEASE </バージョン> </親> <プロパティ> <ジャワ.version> 1.8 </java.version> <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> </プロパティ> <依存性> <依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスターター</たartifactId> </依存> <依存性> <のgroupId>org.springframework.boot </ groupIdを> <たartifactId>ばねブートスタータ試験</たartifactId> <スコープ>テスト</スコープ> </依存> <依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId> springbootスタータアクチュエータ</たartifactId> </依存> <! -添加springboot对AMQP的支持- > <依存性> <のgroupId> org.springframework.boot </のgroupId> <たartifactId>ばねブートスタータAMQP </たartifactId> </依存> </依存関係>
二、YML設定
春: RabbitMQの: ホスト: 127.0.0.1 ポート: 5672 ユーザー名:ゲスト パスワード: 123456 のVirtualHost: / パブリッシャ -returns:真の パブリッシャ -confirms:真の リスナー: ダイレクト: プリフェッチ: 100 同時実行: 200 最大 -concurrency:300
第三に、コンフィギュレーション・クラス
輸入org.springframework.amqp.core.Binding。 輸入org.springframework.amqp.core.BindingBuilder。 輸入org.springframework.amqp.core.DirectExchange; 輸入org.springframework.amqp.core.FanoutExchange; 輸入org.springframework.amqp.core.HeadersExchange; 輸入org.springframework.amqp.core.Queue; 輸入org.springframework.amqp.core.TopicExchange; 輸入org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory。 輸入org.springframework.amqp.rabbit.connection.CachingConnectionFactory。 輸入org.springframework.amqp.rabbit.connection.ConnectionFactory。 輸入org.springframework.amqp.rabbit.core.RabbitTemplate; 輸入org.springframework.beans.factory.annotation.Qualifier; 輸入org.springframework.beans.factory.annotation.Value; 輸入org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer; 輸入org.springframework.context.annotation.Bean; 輸入org.springframework.context.annotation.Configuration。 輸入lombok.extern.slf4j.Slf4j; @Configuration @ SLF4J パブリック クラスRabbitConfig { @value( "$ {spring.rabbitmq.publisher-確認}" ) プライベート ブール publisherConfirms = 真; @Bean(名 = "secondConnectionFactory" ) 公衆のConnectionFactory secondConnectionFactory( @value( "$ {spring.rabbitmq.host}" )は、文字列ホスト、 @value( "$ {spring.rabbitmq.port}" )は、文字列ポート、 @value( "$ {spring.rabbitmq.username}" )文字列のユーザー名、 @value( "$ {spring.rabbitmq.password}" )文字列のパスワード){ CachingConnectionFactoryはconnectionFactory = 新しい CachingConnectionFactory(); connectionFactory.setAddresses(ホスト + ":" connectionFactory.setUsername(ユーザー名); connectionFactory.setPassword(パスワード); connectionFactory.setPublisherConfirms(publisherConfirms)。 返却はconnectionFactoryを。 } @Bean(名 = "rabbitTemplate" ) 公衆 RabbitTemplate rabbitTemplate(@Qualifier( "secondConnectionFactory" ) のConnectionFactoryはconnectionFactory ){ RabbitTemplate rabbitTemplate = RabbitTemplate(はconnectionFactory)。 リターンrabbitTemplate。 }新しいです @Bean(名= " rabbitFactory ")
公衆SimpleRabbitListenerContainerFactory rabbitFactory(
SimpleRabbitListenerContainerFactoryConfigurer構成器、
@Qualifier(" secondConnectionFactory")のConnectionFactoryはconnectionFactory
){
SimpleRabbitListenerContainerFactory工場=新しいSimpleRabbitListenerContainerFactory()。
configurer.configure(工場、はconnectionFactory)。
工場を返します。
} }