image.png
    RabbitMQConfig:

    1. @Configuration
    2. public class RabbitConfig {
    3. @Autowired
    4. private RabbitProperties properties;
    5. /*RabbitMQ连接池,从配置文件读取参数*/
    6. @Bean
    7. public ConnectionFactory connectionFactory(){
    8. CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    9. cachingConnectionFactory.setHost(properties.getHost());
    10. cachingConnectionFactory.setPort(properties.getPort());
    11. cachingConnectionFactory.setUsername(properties.getUsername());
    12. cachingConnectionFactory.setPassword(properties.getPassword());
    13. cachingConnectionFactory.setVirtualHost(properties.getVirtualHost());
    14. return cachingConnectionFactory;
    15. }
    16. /* RabbitTemplate配置 */
    17. @Bean
    18. public RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory){
    19. RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); //让RabbitTemplate使用连接池
    20. return rabbitTemplate;
    21. }
    22. }

    image.png