[新增功能](master): MyCAT 与 基于Netty的Demo更新

1. MyCAT 的学习内容更新
2. 基于Netty的服务端代码更新
master
土豆兄弟 2 years ago
parent dfc0b84ba7
commit 46f25a0427

@ -0,0 +1,35 @@
# MyCat指南
## 1. Mycat相关的基本概念
* Mycat的概念
* MyCAT相当于MySQL SERVER层
* MySQL相当于MyCAT的存储层
* MyCAT中不存储数据,所有的数据存储在MySQL中
* MyCAT对于研发人员来说是透明的
* MyCAT对使用的SQL有一些限制
* MyCAT是数据库中间层
* MyCAT可以实现对后端数据库的分库分表和读写分离
* MyCAT对前端应用隐藏了后端数据库的存储逻辑
* 什么是数据中间层
* 链接数据和应用的一层设计MyCAT就是用来实现数据中间层的数据中间件
* MyCAT的主要作用
* 作为分布式数据库中间层使用
* 实现后端数据库读写分离及负载均衡
* 对业务数据库进行垂直/水平切分
* 控制数据库连接的数量
* Mycat的基本元素
* 逻辑库
* 应用层直接看成数据库即可,实际可能是由多个数据库组成的逻辑库
* 逻辑库中不会保存数据,只是一个对外开放数据库的概念
* 逻辑表
* 水平切分后的订单表,结构相同,对外可以作为一张大表来使用
* 逻辑表也是概念的东西,实际并不用来进行存储数据
* 逻辑表的种类: 分片表和非分片表全局表在所有分片中都存在的表ER关系表按ER关系进行分片的表
* MyCAT安装
* 下载解压MyCAT
* 安装JAVA运行环境
* 新建MyCAT运行系统账号
* 配置系统的环境变量
* 修改Mycat启动参数,启动MyCAT
*

Binary file not shown.

After

Width:  |  Height:  |  Size: 293 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 410 KiB

@ -47,6 +47,12 @@
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,82 @@
package com.baiye.number;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
/**
*
*
* @author q
* @date 2022/06/15
*/
public class NumberUtil {
/**
*
*
* @param type 线/线 1
* @param organizationId / 6
* @param spgId 5
* @param date 6
* @return {@link String}
*/
public static String createOrderCode(String type, String organizationId, String spgId, String date) {
return type +
organizationId +
spgId +
date +
produceRandomNumber();
}
static StringBuffer produceRandomNumber() {
StringBuffer buffer = new StringBuffer();
ThreadLocalRandom.current().ints(0, 9).limit(10).forEach(
buffer::append
);
return buffer;
}
/**
* -
* <p>
* fixme 线线
*/
@SuppressWarnings("all")
public static void main(String[] args) {
int sum = 10000;
CountDownLatch countDownLatch = new CountDownLatch(sum);
Set<String> set = new HashSet<>(sum);
for (int i = 0; i < sum; i++) {
new Thread(() -> {
String num = createOrderCode("S", "000012", "11002", "2022615");
System.out.println(
Thread.currentThread().getName()
+ " - "
+ num);
set.add(num);
countDownLatch.countDown();
}).start();
}
try {
countDownLatch.await();
// 结果为 定义的 sum值表示 生成的都是唯一的Id,且数目对的
System.out.println(set.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,3 @@
# Disruptor与Netty实现百万级长连接接入
##

@ -0,0 +1,46 @@
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
### Java template
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dev-protocol</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dev-protocol-disruptor-netty-client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>

@ -0,0 +1,46 @@
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
### Java template
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dev-protocol</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dev-protocol-disruptor-netty-common</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency>
<!-- 序列化框架marshalling -->
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling</artifactId>
<version>1.3.0.CR9</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>1.3.0.CR9</version>
</dependency>
<!-- 序列化框架protobuf -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
<!-- netty end -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,49 @@
package com.baiye.codec;
import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallingDecoder;
import io.netty.handler.codec.marshalling.MarshallingEncoder;
import io.netty.handler.codec.marshalling.UnmarshallerProvider;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
/**
* Marshalling -
*/
public final class MarshallingCodeCFactory {
/**
* Jboss MarshallingMarshallingDecoder
* @return MarshallingDecoder
*/
public static MarshallingDecoder buildMarshallingDecoder() {
//首先通过Marshalling工具类的精通方法获取Marshalling实例对象 参数serial标识创建的是java序列化工厂对象。
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
//创建了MarshallingConfiguration对象配置了版本号为5
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
//根据marshallerFactory和configuration创建provider
UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
//构建Netty的MarshallingDecoder对象俩个参数分别为provider和单个消息序列化后的最大长度
MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024 * 1024 * 1);
return decoder;
}
/**
* Jboss MarshallingMarshallingEncoder
* @return MarshallingEncoder
*/
public static MarshallingEncoder buildMarshallingEncoder() {
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
//构建Netty的MarshallingEncoder对象MarshallingEncoder用于实现序列化接口的POJO对象序列化为二进制数组
MarshallingEncoder encoder = new MarshallingEncoder(provider);
return encoder;
}
}

@ -0,0 +1,38 @@
package com.baiye.dto;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
/**
*
*
* @author q
* @date 2022/07/11
*/
@Getter
@Setter
@ToString
public class TranslatorData implements Serializable {
/**
* id
*/
private String id;
/**
*
*/
private String name;
/**
*
*/
private String message;
}

@ -0,0 +1,46 @@
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
### Java template
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>dev-protocol</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dev-protocol-disruptor-netty-server</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>dev-protocol-disruptor-netty-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,78 @@
package com.baiye.server;
import com.baiye.codec.MarshallingCodeCFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.AdaptiveRecvByteBufAllocator;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* Netty Server
*
* @author q
* @date 2022/07/11
*/
public class NettyServer {
/**
* Netty 线
* :
*
*
* | |------------ip/port,----------------|-> Server[Online] |
* | | | | | |
* | |<----------ack,-------------------|- |SYNC | |
* | | | |
* | | | sync + accept = backlog |
* | Client | | |
* | |--SYNCAccept----->| |Accept| |
* | | | |
* | | | serverSocket.accept() |
*
*
*
*/
public NettyServer() {
// 1. 创建两个工作线程组: 1个用于接收网络请求的, 另一个用于实际处理业务的线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
// 创建辅助类 - 帮助构建Netty模型
ServerBootstrap serverBootstrap = new ServerBootstrap()
.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
// fixme 设置 backlog大小
.option(ChannelOption.SO_BACKLOG, 1024)
// fixme 表示缓存区动态调配(自适应) 数据包相差不大的时候比较合适
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
// 缓冲区 池化操作
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
// 记录日志
.handler(new LoggingHandler(LogLevel.INFO))
// 数据接收过来给哪个方法进行回调 -> 接收数据进行异步处理
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
// fixme 在管道上加一些拦截器进行处理
// 进行设置编解码 -> 对Java对象转为的二进制数据进行编解码
socketChannel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
socketChannel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
// 信息处理
socketChannel.pipeline().addLast(new ServerHandler());
}
});
}
}

@ -0,0 +1,32 @@
package com.baiye.server;
import com.baiye.dto.TranslatorData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* ,
*
* @author q
* @date 2022/07/11
*/
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 接收到数据 数据已经进行了编解码, 所以直接进行转换
TranslatorData request = (TranslatorData) msg;
System.out.println("Server 端数据 " + request.toString());
// 设计响应回送数据
TranslatorData response = new TranslatorData();
response.setId("resp: " + request.getId());
response.setName("resp: " + request.getName());
response.setMessage("resp: " + request.getMessage());
// 写数据并刷新
ctx.writeAndFlush(response);
}
}

@ -28,6 +28,9 @@
<module>dev-protocol-pay</module>
<module>dev-protocol-tools</module>
<module>dev-protocol-drools</module>
<module>longpolling/demo/demo1/dev-protocol-disruptor-netty-client</module>
<module>longpolling/demo/demo1/dev-protocol-disruptor-netty-server</module>
<module>longpolling/demo/demo1/dev-protocol-disruptor-netty-common</module>
</modules>
<properties>

Loading…
Cancel
Save