今天开发的时候遇见一个需求,就是配置数据库的读写分离,因为当时的项目需要部署在云服务去,而我们本地和测试环境又没有使用读写分离,于使我单独创建一个配置文件为云服务器的配置文件,想知道读写分离及双数据源配置的小伙伴可以看看另一篇文章:
在配置双数据源的时候引入的依赖包
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
但是我在配置之后发现的问题就是,云服务器的配置是没什么问题了,但是本地似乎没办法启动了,
无法连接到数据库。这是因为我们添加了读写分离的依赖包,项目启动的时候就会按照读写分离的配置去加载数据库的连接。所以在本地环境中我就 不需要这个依赖包。可以在项目的依赖中增加下面的配置:
<profiles>
<!-- 测试环境 -->
<profile>
<id>dev</id>
<!-- 默认激活 dev 环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profiles.active>dev</profiles.active>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
//生产环境中需要这个依赖,所以
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
</dependency>
</profile>
</profiles>
成功解决