springboot下的rabbitMQ传输实体及远程部署配置

导入依赖:


            org.springframework.boot
            spring-boot-starter-amqp

远程不能使用默认的guest用户,需要另外建立用户
配置文件:


image.png

注入:

@Configuration
public class MqConfig {
    @Value("${mqRabbitHost}")
    private String mqRabbitHost;
    @Value("${mqRabbitPort}")
    private int mqRabbitPort;
    @Value("${mqRabbitUserName}")
    private String mqRabbitUserName;
    @Value("${mqRabbitPassword}")
    private String mqRabbitPassword;
    @Value("${mqRabbitVirtualHost}")
    private String mqRabbitVirtualHost;
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.mqRabbitHost,this.mqRabbitPort);
        connectionFactory.setUsername(this.mqRabbitUserName);
        connectionFactory.setPassword(this.mqRabbitPassword);
        connectionFactory.setVirtualHost(this.mqRabbitVirtualHost);
        connectionFactory.setPublisherConfirms(true);

        return connectionFactory;
    }

}

发送端配置:

@Configuration
public class ProducerConfig implements CommandLineRunner {
    @Bean
    public MappingJackson2MessageConverter jackson2Converter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        return converter;
    }
    @Override
    public void run(String... strings) throws Exception {

    }
    @Autowired
    private UserSender sender;
}

接收端配置:


@EnableRabbit
@Configuration
public class ConsumerConfig implements RabbitListenerConfigurer {
    @Value("${mqRabbitHost}")
    private String mqRabbitHost;
    @Value("${mqRabbitPort}")
    private int mqRabbitPort;
    @Value("${mqRabbitUserName}")
    private String mqRabbitUserName;
    @Value("${mqRabbitPassword}")
    private String mqRabbitPassword;
    @Value("${mqRabbitVirtualHost}")
    private String mqRabbitVirtualHost;


    @Bean
    public MappingJackson2MessageConverter jackson2Converter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        return converter;
    }
    @Bean
    public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        factory.setMessageConverter(jackson2Converter());
        return factory;
    }
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.mqRabbitHost,this.mqRabbitPort);
        connectionFactory.setUsername(this.mqRabbitUserName);
        connectionFactory.setPassword(this.mqRabbitPassword);
        connectionFactory.setVirtualHost(this.mqRabbitVirtualHost);
        connectionFactory.setPublisherConfirms(true);

        return connectionFactory;
    }
    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
        rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
    }
}

接收:

@Component
public class UserReceiver{
 @RabbitListener(queues = "updateAllUser100")//同步所有用户
    public void updateAllUser(T t){
        //TODO
    }
}

你可能感兴趣的:(springboot下的rabbitMQ传输实体及远程部署配置)