1、springboot整合mongodb(主从集群模式整合)
(1)导入jar包: spring-boot-starter-data-mongodb
(2)进行配置:
spring:
data:
mongodb:
uri: mongodb://192.168.71.132:27018,192.168.71.133:27018/springboot_test?slaveOk=true&replicaSet=test&write=1&readPreference=secondaryPreferred&connectTimeoutMS=300000
#每个主机的连接数
connections-per-host: 50
#线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
threads-allowed-to-block-for-connection-multiplier: 50
connect-timeout: 5000
socket-timeout: 3000
max-wait-time: 1500
#控制是否在一个连接时,系统会自动重试
auto-connect-retry: true
socket-keep-alive: true
(3)注入接口
@Autowired
MongoTemplate mongoTemplate;
2、springboot整合rabbitmq
(1)导入jar包: spring-boot-starter-amqp
(2)进行配置
spring:
rabbitmq:
host: 192.168.71.132,192.168.71.133
port: 5672
username: fort
password: fort
# publisher-confirms: true
publisher-returns: true
virtual-host: /
listener:
simple:
concurrency: 1
max-concurrency: 1
(3)设置mq
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置质量类型的mq
* @author zjh
*/
@Configuration
public class MqConfig {
//队列 起名:TestDirectQueue
@Bean
public Queue TestDirectQueue() {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("TestDirectQueue",true,true,false);
//一般设置一下队列的持久化就好,其余两个就是默认false
return new Queue("TestDirectQueue",true);
}
//Direct交换机 起名:TestDirectExchange
@Bean
DirectExchange TestDirectExchange() {
// return new DirectExchange("TestDirectExchange",true,true);
return new DirectExchange("TestDirectExchange",true,false);
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
@Bean
DirectExchange lonelyDirectExchange() {
return new DirectExchange("lonelyDirectExchange");
}
}