diff --git a/blade-service/blade-demo/pom.xml b/blade-service/blade-demo/pom.xml index 7030309b2..e968fabe3 100644 --- a/blade-service/blade-demo/pom.xml +++ b/blade-service/blade-demo/pom.xml @@ -42,6 +42,11 @@ blade-demo-api ${bladex.project.version} + + com.baomidou + dynamic-datasource-spring-boot-starter + 2.5.6 + org.springblade blade-core-test @@ -69,6 +74,7 @@ + diff --git a/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java b/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java new file mode 100644 index 000000000..47e129df8 --- /dev/null +++ b/blade-service/blade-demo/src/main/java/com/example/demo/controller/DynamicController.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package com.example.demo.controller; + +import com.example.demo.entity.Notice; +import com.example.demo.service.IDynamicService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiOperationSupport; +import lombok.AllArgsConstructor; +import org.springblade.core.tool.api.R; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * 多数据源 + * + * @author Chill + */ +@RestController +@AllArgsConstructor +@RequestMapping("dynamic") +@Api(value = "多数据源接口", tags = "多数据源") +public class DynamicController { + + private IDynamicService dynamicService; + + /** + * master列表 + */ + @GetMapping("/master-list") + @ApiOperationSupport(order = 1) + @ApiOperation(value = "master列表", notes = "master列表") + public R> masterList() { + List list = dynamicService.masterList(); + return R.data(list); + } + + /** + * slave列表 + */ + @GetMapping("/slave-list") + @ApiOperationSupport(order = 1) + @ApiOperation(value = "slave列表", notes = "slave列表") + public R> slaveList() { + List list = dynamicService.slaveList(); + return R.data(list); + } + +} diff --git a/blade-service/blade-demo/src/main/java/com/example/demo/service/IDynamicService.java b/blade-service/blade-demo/src/main/java/com/example/demo/service/IDynamicService.java new file mode 100644 index 000000000..dd09977c0 --- /dev/null +++ b/blade-service/blade-demo/src/main/java/com/example/demo/service/IDynamicService.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package com.example.demo.service; + +import com.example.demo.entity.Notice; +import org.springblade.core.mp.base.BaseService; + +import java.util.List; + +/** + * 服务类 + * + * @author Chill + */ +public interface IDynamicService extends BaseService { + + /** + * master数据源的列表 + * + * @return + */ + List masterList(); + + /** + * slave数据源的列表 + * + * @return + */ + List slaveList(); + +} diff --git a/blade-service/blade-demo/src/main/java/com/example/demo/service/impl/DynamicServiceImpl.java b/blade-service/blade-demo/src/main/java/com/example/demo/service/impl/DynamicServiceImpl.java new file mode 100644 index 000000000..64dd1b627 --- /dev/null +++ b/blade-service/blade-demo/src/main/java/com/example/demo/service/impl/DynamicServiceImpl.java @@ -0,0 +1,30 @@ +package com.example.demo.service.impl; + +import com.baomidou.dynamic.datasource.annotation.DS; +import com.example.demo.entity.Notice; +import com.example.demo.mapper.NoticeMapper; +import com.example.demo.service.IDynamicService; +import org.springblade.core.mp.base.BaseServiceImpl; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * DynamicServiceImpl + * + * @author Chill + */ +@Service +public class DynamicServiceImpl extends BaseServiceImpl implements IDynamicService { + + @Override + public List masterList() { + return this.list(); + } + + @Override + @DS("slave") + public List slaveList() { + return this.list(); + } +} diff --git a/blade-service/blade-demo/src/main/resources/application-dev.yml b/blade-service/blade-demo/src/main/resources/application-dev.yml index 25056f70f..38426fa15 100644 --- a/blade-service/blade-demo/src/main/resources/application-dev.yml +++ b/blade-service/blade-demo/src/main/resources/application-dev.yml @@ -3,8 +3,26 @@ server: port: 8200 #数据源配置 +#spring: +# datasource: +# url: ${blade.datasource.dev.url} +# username: ${blade.datasource.dev.username} +# password: ${blade.datasource.dev.password} + spring: + #排除DruidDataSourceAutoConfigure + autoconfigure: + exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure datasource: - url: ${blade.datasource.dev.url} - username: ${blade.datasource.dev.username} - password: ${blade.datasource.dev.password} + dynamic: + #设置默认的数据源或者数据源组,默认值即为master + primary: master + datasource: + master: + url: ${blade.datasource.demo.master.url} + username: ${blade.datasource.demo.master.username} + password: ${blade.datasource.demo.master.password} + slave: + url: ${blade.datasource.demo.slave.url} + username: ${blade.datasource.demo.slave.username} + password: ${blade.datasource.demo.slave.password} diff --git a/blade-service/blade-demo/src/main/resources/application-prod.yml b/blade-service/blade-demo/src/main/resources/application-prod.yml index 56b22dd98..da892629d 100644 --- a/blade-service/blade-demo/src/main/resources/application-prod.yml +++ b/blade-service/blade-demo/src/main/resources/application-prod.yml @@ -3,8 +3,26 @@ server: port: 8200 #数据源配置 +#spring: +# datasource: +# url: ${blade.datasource.prod.url} +# username: ${blade.datasource.prod.username} +# password: ${blade.datasource.prod.password} + spring: + #排除DruidDataSourceAutoConfigure + autoconfigure: + exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure datasource: - url: ${blade.datasource.prod.url} - username: ${blade.datasource.prod.username} - password: ${blade.datasource.prod.password} + dynamic: + #设置默认的数据源或者数据源组,默认值即为master + primary: master + datasource: + master: + url: ${blade.datasource.demo.master.url} + username: ${blade.datasource.demo.master.username} + password: ${blade.datasource.demo.master.password} + slave: + url: ${blade.datasource.demo.slave.url} + username: ${blade.datasource.demo.slave.username} + password: ${blade.datasource.demo.slave.password} diff --git a/blade-service/blade-demo/src/main/resources/application-test.yml b/blade-service/blade-demo/src/main/resources/application-test.yml index 2842aa483..4b4e157a3 100644 --- a/blade-service/blade-demo/src/main/resources/application-test.yml +++ b/blade-service/blade-demo/src/main/resources/application-test.yml @@ -3,8 +3,26 @@ server: port: 8200 #数据源配置 +#spring: +# datasource: +# url: ${blade.datasource.test.url} +# username: ${blade.datasource.test.username} +# password: ${blade.datasource.test.password} + spring: + #排除DruidDataSourceAutoConfigure + autoconfigure: + exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure datasource: - url: ${blade.datasource.test.url} - username: ${blade.datasource.test.username} - password: ${blade.datasource.test.password} + dynamic: + #设置默认的数据源或者数据源组,默认值即为master + primary: master + datasource: + master: + url: ${blade.datasource.demo.master.url} + username: ${blade.datasource.demo.master.username} + password: ${blade.datasource.demo.master.password} + slave: + url: ${blade.datasource.demo.slave.url} + username: ${blade.datasource.demo.slave.username} + password: ${blade.datasource.demo.slave.password} diff --git a/doc/nacos/blade-demo-dev.yaml b/doc/nacos/blade-demo-dev.yaml index 7116df7e0..fee2be5a3 100644 --- a/doc/nacos/blade-demo-dev.yaml +++ b/doc/nacos/blade-demo-dev.yaml @@ -7,3 +7,13 @@ blade: secure: skip-url: - /demo/** + datasource: + demo: + master: + url: jdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8 + username: root + password: root + slave: + url: jdbc:mysql://localhost:3306/bladex_slave?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8 + username: root + password: root