分库分表的不要使用以下方法
这里是普通的多数据源配置
本方法是nacos的配置
1 依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2 nacos的yaml配置
spring:
datasource:
dynamic:
# 设置默认的数据源
primary: dbplus
# 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
dbplus:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxxxxxx.rds.aliyuncs.com:3306/dbplus?useAffectedRows=true&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
username: root
password: 123456
marketing:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxxxxxx.rds.aliyuncs.com:3306/marketing?useAffectedRows=true&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
username: root
password: 123456
3 多数据源注解
实现类加上注解
@Slf4j
@Service
@DS("marketing")
public class UserPrizeLogServiceImpl extends ServiceImpl<UserPrizeLogMapper, UserPrizeLog> implements IUserPrizeLogService {
@Resource
UserPrizeLogMapper mapper;
检测一下,配置有没有问题,然后启动
2021-10-11 15:39:22,536 []-[]-[]-[]-[main] [org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker] [INFO] (PostProcessorRegistrationDelegate.java:330)- Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration EnhancerBySpringCGLIB 841905a3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
........
2021-10-11 15:39:26,918 []-[]-[]-[]-[main] [com.netflix.config.sources.URLConfigurationSource] [WARN] (URLConfigurationSource.java:121)- No URLs will be polled as dynamic configuration sources.
2021-10-11 15:39:26,918 []-[]-[]-[]-[main] [com.netflix.config.sources.URLConfigurationSource] [INFO] (URLConfigurationSource.java:122)- To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
如果没有报错,表示正常