mirror of
https://gitee.com/nutz/nutzboot.git
synced 2024-12-02 03:38:08 +08:00
add: 添加sharding-jdbc及其demo
This commit is contained in:
parent
d906b04fa1
commit
2b08c6076c
@ -6,7 +6,9 @@
|
||||
* add: new NbApp()可以不传类名,从堆栈自动推断
|
||||
* add: starter-mongodb by @qingerg
|
||||
* add: starter-tomcat by @ben
|
||||
* add: starter-beetlsql by wendal
|
||||
* add: starter-beetlsql 来自beetl的SQL解决方案
|
||||
* add: starter-sharding-jdbc 分库分表
|
||||
* fix: jetty扫描websocket的endpoint有问题
|
||||
|
||||
## 2.0-RC "属于"
|
||||
|
||||
|
@ -0,0 +1,77 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-demo-simple</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>nutzboot-demo-simple-sharding-jdbc</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-nutz-mvc</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-nutz-dao</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-sharding-jdbc</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter-jetty</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.44</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/nutz/org.nutz.boot.starter.NbStarter</resource>
|
||||
</transformer>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>io.nutz.demo.simple.MainLauncher</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,71 @@
|
||||
package io.nutz.demo.simple;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.nutz.boot.NbApp;
|
||||
import org.nutz.dao.Cnd;
|
||||
import org.nutz.dao.Dao;
|
||||
import org.nutz.dao.impl.NutDao;
|
||||
import org.nutz.dao.impl.SimpleDataSource;
|
||||
import org.nutz.dao.util.Daos;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.json.Json;
|
||||
import org.nutz.log.Log;
|
||||
import org.nutz.log.Logs;
|
||||
import org.nutz.mvc.annotation.At;
|
||||
|
||||
import io.nutz.demo.simple.bean.UserOrder;
|
||||
|
||||
@IocBean(create = "init")
|
||||
public class MainLauncher {
|
||||
|
||||
private static final Log log = Logs.get();
|
||||
|
||||
@Inject
|
||||
protected Dao dao;
|
||||
|
||||
@At("/user/query/?")
|
||||
public List<UserOrder> queryByUserId(int userId) {
|
||||
return dao.query(UserOrder.class, Cnd.where("userId", "=", 1));
|
||||
}
|
||||
|
||||
public void init() {
|
||||
// 建表及插入测试数据
|
||||
// shardingjdbc的表需要自己建,为了demo,这里只能迁就一下...
|
||||
creare_order_table();
|
||||
|
||||
// 测试一下查询语句
|
||||
List<UserOrder> orders = dao.query(UserOrder.class, Cnd.where("userId", "=", 1));
|
||||
log.info("User A orders = " + Json.toJson(orders));
|
||||
orders = dao.query(UserOrder.class, Cnd.where("userId", "=", 2));
|
||||
log.info("User B orders = " + Json.toJson(orders));
|
||||
}
|
||||
|
||||
protected void creare_order_table() {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
SimpleDataSource ds = new SimpleDataSource();
|
||||
ds.setJdbcUrl("jdbc:mysql://localhost:3306/ds_" + i);
|
||||
ds.setUsername("root");
|
||||
ds.setPassword("root");
|
||||
NutDao dao = new NutDao(ds);
|
||||
Daos.ext(dao, "_" + j).create(UserOrder.class, false);
|
||||
}
|
||||
}
|
||||
// 分别为userId=1和userId=2插入2条记录
|
||||
if (dao.count(UserOrder.class, Cnd.where("userId", "=", 1)) == 0) {
|
||||
dao.insert(new UserOrder(1, 11));
|
||||
dao.insert(new UserOrder(1, 18));
|
||||
}
|
||||
if (dao.count(UserOrder.class, Cnd.where("userId", "=", 2)) == 0) {
|
||||
dao.insert(new UserOrder(2, 12));
|
||||
dao.insert(new UserOrder(2, 9));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new NbApp().setPrintProcDoc(true).run();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package io.nutz.demo.simple.bean;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.nutz.dao.entity.annotation.Column;
|
||||
import org.nutz.dao.entity.annotation.EL;
|
||||
import org.nutz.dao.entity.annotation.PK;
|
||||
import org.nutz.dao.entity.annotation.Prev;
|
||||
import org.nutz.dao.entity.annotation.Table;
|
||||
|
||||
@PK({ "userId", "orderId" })
|
||||
@Table("t_order${t}")
|
||||
public class UserOrder {
|
||||
|
||||
@Column("user_id")
|
||||
private int userId;
|
||||
|
||||
@Column("order_id")
|
||||
private int orderId;
|
||||
|
||||
@Prev(els = @EL("now()"))
|
||||
private Date createTime;
|
||||
|
||||
public UserOrder() {
|
||||
}
|
||||
|
||||
public UserOrder(int userId, int orderId) {
|
||||
super();
|
||||
this.userId = userId;
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package io.nutz.demo.simple.module;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.nutz.dao.Cnd;
|
||||
import org.nutz.dao.Dao;
|
||||
import org.nutz.dao.pager.Pager;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.mvc.annotation.At;
|
||||
import org.nutz.mvc.annotation.Ok;
|
||||
import org.nutz.mvc.annotation.Param;
|
||||
|
||||
import io.nutz.demo.simple.bean.UserOrder;
|
||||
|
||||
@At("/user")
|
||||
@IocBean
|
||||
public class UserModule {
|
||||
|
||||
@Inject
|
||||
Dao dao;
|
||||
|
||||
@Ok("raw")
|
||||
@At("/count")
|
||||
public long count() {
|
||||
return dao.count(UserOrder.class);
|
||||
}
|
||||
|
||||
@Ok("json:full")
|
||||
@At("/query")
|
||||
public List<UserOrder> query(@Param("..")Pager pager) {
|
||||
return dao.query(UserOrder.class, Cnd.orderBy().asc("age"), pager);
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
jetty.port=8080
|
||||
jetty.host=127.0.0.1
|
@ -0,0 +1,8 @@
|
||||
log4j.rootLogger=info,Console
|
||||
|
||||
log4j.logger.org.nutz.mvc=debug
|
||||
log4j.logger.org.eclipse.jetty=info
|
||||
|
||||
log4j.appender.Console=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] %5p [%t] --- %c{1}: %m%n
|
@ -0,0 +1,22 @@
|
||||
dataSources:
|
||||
ds_0: !!com.alibaba.druid.pool.DruidDataSource
|
||||
url: jdbc:mysql://localhost:3306/ds_0
|
||||
username: root
|
||||
password: root
|
||||
ds_1: !!com.alibaba.druid.pool.DruidDataSource
|
||||
url: jdbc:mysql://localhost:3306/ds_1
|
||||
username: root
|
||||
password: root
|
||||
|
||||
shardingRule:
|
||||
tables:
|
||||
t_order:
|
||||
actualDataNodes: ds_${0..1}.t_order_${0..1}
|
||||
databaseStrategy:
|
||||
inline:
|
||||
shardingColumn: user_id
|
||||
algorithmExpression: ds_${user_id % 2}
|
||||
tableStrategy:
|
||||
inline:
|
||||
shardingColumn: order_id
|
||||
algorithmExpression: t_order_${order_id % 2}
|
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello, So NB!</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello, So NB!
|
||||
</body>
|
||||
</html>
|
22
nutzboot-starter-sharding-jdbc/pom.xml
Normal file
22
nutzboot-starter-sharding-jdbc/pom.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-parent</artifactId>
|
||||
<version>2.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>nutzboot-starter-sharding-jdbc</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.nutz</groupId>
|
||||
<artifactId>nutzboot-starter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.shardingjdbc</groupId>
|
||||
<artifactId>sharding-jdbc-core</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,45 @@
|
||||
package org.nutz.boot.starter.shardingjdbc;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.nutz.boot.AppContext;
|
||||
import org.nutz.boot.annotation.PropDoc;
|
||||
import org.nutz.ioc.impl.PropertiesProxy;
|
||||
import org.nutz.ioc.loader.annotation.Inject;
|
||||
import org.nutz.ioc.loader.annotation.IocBean;
|
||||
import org.nutz.lang.Streams;
|
||||
|
||||
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
|
||||
|
||||
@IocBean
|
||||
public class ShardingJdbcDataSourceStarter {
|
||||
|
||||
protected static String PRE = "shardingjdbc.";
|
||||
@PropDoc(group = "shardingjdbc", value = "配置文件路径", defaultValue = "shardingjdbc.yaml")
|
||||
public static final String PROP_PATH = PRE + "path";
|
||||
|
||||
@Inject
|
||||
protected PropertiesProxy conf;
|
||||
|
||||
@Inject
|
||||
protected AppContext appContext;
|
||||
|
||||
@IocBean
|
||||
public DataSource getDataSource() throws Exception {
|
||||
String path = conf.get(PROP_PATH, "shardingjdbc.yaml");
|
||||
InputStream ins = appContext.getResourceLoader().get(path);
|
||||
if (ins == null) {
|
||||
File f = new File(path);
|
||||
if (f.exists() && f.canRead()) {
|
||||
ins = new FileInputStream(f);
|
||||
} else {
|
||||
throw new RuntimeException("no such shardingjdbc configure file=" + path);
|
||||
}
|
||||
}
|
||||
return ShardingDataSourceFactory.createDataSource(Streams.readBytesAndClose(ins));
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.nutz.boot.starter.shardingjdbc.ShardingJdbcDataSourceStarter
|
Loading…
Reference in New Issue
Block a user