经过非常长的时间非常崩溃的不断尝试中终于解决了
1.在yum中配置好参数
2.写一个配置类,其中参数从yum中动态获取
@Configuration
public class RabbitMQConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Value("${spring.rabbitmq.virtual-host}")
private String virtualHost;
@Value("${spring.rabbitmq.connection-timeout}") // 自定义的连接超时时间,单位毫秒
private int connectionTimeout;
@Bean
public ConnectionFactory rabbitConnectionFactory() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost(host);
//端口
factory.setPort(5672);
//设置虚拟主机,用户名、密码、vhost
factory.setVirtualHost(virtualHost);
factory.setUsername(username);
factory.setPassword(password);
factory.setHandshakeTimeout(300000000);
// 如果你想设置读取超时,可以使用下面的方法(单位毫秒)
// factory.setRequestedHeartbeat(readTimeout); // 注意:这不是真正的读取超时,而是心跳间隔
return factory;
}
@Bean
public CachingConnectionFactory cachingConnectionFactory(ConnectionFactory connectionFactory) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);
// 可以设置其他CachingConnectionFactory的属性,如缓存大小等
return cachingConnectionFactory;
}