我遇见你,我记得你,这座城市天生就适合谈恋爱,你天生就适合我的灵魂。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.2
com.yy
springboot-sharding-jdbc
0.0.1-SNAPSHOT
springboot-sharding-jdbc
Demo project for Spring Boot
1.8
2020.0.1
2021.1
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-bootstrap
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
org.springframework.boot
spring-boot-starter-jdbc
2.5.6
mysql
mysql-connector-java
runtime
org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
4.0.0-RC1
io.shardingsphere
sharding-jdbc-spring-namespace
3.1.0
com.alibaba
druid
1.1.20
compile
com.baomidou
mybatis-plus-boot-starter
3.2.0
com.baomidou
mybatis-plus-generator
3.2.0
org.freemarker
freemarker
2.3.28
org.projectlombok
lombok
true
cn.hutool
hutool-all
5.5.8
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibab.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
public
aliyun nexus
http://maven.aliyun.com/nexus/content/groups/public/
true
public
aliyun nexus
http://maven.aliyun.com/nexus/content/groups/public//
true
false
public interface UserMapper extends BaseMapper {
}
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_user")
@ApiModel(value="User对象", description="用户表")
public class User implements Serializable {
@ApiModelProperty(value = "用户编号")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "用户昵称")
@TableField(value = "nickname")
private String nickname;
@ApiModelProperty(value = "用户密码")
@TableField(value = "password")
private String password;
@ApiModelProperty(value = "性别")
@TableField(value = "sex")
private Integer sex;
@ApiModelProperty(value = "年龄")
@TableField(value = "age")
private Integer age;
@ApiModelProperty(value = "生日")
@TableField(value = "birthday")
private String birthday;
}
@RestController
@RequestMapping("/api/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/save")
public String addUser() {
User user = new User();
user.setNickname("JueDream" + new Random().nextInt());
user.setPassword("123456");
user.setAge(18);
user.setSex(1);
user.setBirthday("1997-12-03");
userMapper.insert(user);
return "success";
}
@GetMapping("/findUsers")
public List findUsers() {
return userMapper.selectList(new LambdaQueryWrapper());
}
}
@Slf4j
@MapperScan("com.yy.ssj.mapper")
@SpringBootApplication
@EnableDiscoveryClient
public class SpringbootShardingJdbcApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(SpringbootShardingJdbcApplication.class, args);
Environment env = application.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String path = env.getProperty("server.servlet.context-path");
log.info("\n----------------------------------------------------------\n\t" +
"Application springboot-sharding-jdbc is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:" + port + path + "\n\t" +
"External: \thttp://" + ip + ":" + port + path + "\n\t" +
"----------------------------------------------------------");
}
}
spring:
# Nacos 注册 配置
cloud:
nacos:
config:
server-addr: IP:8848
file-extension: yaml
group: sharding-jdbc
prefix: ${spring.application.name}-${spring.profiles.active}.yaml
discovery:
server-addr: IP:8848
group: sharding-jdbc
application:
name: springboot-sharding-jdbc
profiles:
active: rw
dream:
active: local-nacos
server:
port: 10012
servlet:
context-path: /
spring:
application:
name: springboot-sharding-jdbc
main:
allow-bean-definition-overriding: true
profiles:
active: rw
# mybatis-plus 配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
# 逻辑删除配置
db-config:
# 删除前
logic-not-delete-value: 0
# 删除后
logic-delete-value: 1
# sharding-jdbc 4.0 配置读写分离
spring:
shardingsphere:
# 显示sql
props:
sql:
show: true
# 配置数据源
datasource:
names: ds0,ds1,ds2
# master-ds0数据库连接信息
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://IP:13311/sharding-jdbc-db?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
# slave-ds1数据库连接信息
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://IP:13312/sharding-jdbc-db?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
maxPoolSize: 100
minPoolSize: 5
# slave-ds2数据库连接信息
ds2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://IP:13312/sharding-jdbc-db?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
minPoolSize: 5
ds:
maxPoolSize: 100
# 配置默认数据源ds0 默认数据源,主要用于写
sharding:
default-data-source-name: ds0
# 读写分离 单个读写分离
masterslave:
# 配置主从名称
name: ds-ms
# 配置主库master,负责数据的写入
master-data-source-name: ds0
# 配置从库slave节点
slave-data-source-names: ds1,ds2
# 配置slave节点的负载均衡均衡策略,采用轮询机制
load-balance-algorithm-type: round_robin
依次访问controler 接口; ds0 ->master ds1,ds2->slave
-- slave 从库查询
http://localhost:10012/api/user/findUsers
-- master 主库写入
http://localhost:10012/api/user/save
你是往事的遗书 是日落的余情未了 是路人脚下不停生长的风