Skip to content

Commit

Permalink
5.20 格式的修订 异常类的处理 crazy 遇到个就是之前不会注意的 就是日志显示级别
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzzzzzyt committed May 20, 2022
1 parent a9a9d1a commit 2c4f01d
Show file tree
Hide file tree
Showing 28 changed files with 229 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package service.nio_bootstrap;

import annotation.RpcClientBootStrap;

import consumer.bootstrap.nio.*;
import consumer.bootstrap.nio.NIOConsumerBootStrap12;
import consumer.bootstrap.nio.NIOConsumerBootStrap14;
import consumer.bootstrap.nio.NIOConsumerBootStrap15;
import exception.RpcException;
import lombok.extern.slf4j.Slf4j;
import method.Customer;
import service.ClientCall;



//之后启动直接在这边启动根据 在注解中配置对应的版本号 将相应的操作封装到之后的操作中即可 这样很方便 就是每次咱加一个启动器还得改下switch
//比如说这里的version 1.2 就是v1.2版本的启动器

/**
* @author 祝英台炸油条
*/
@Slf4j
public class NIOClientBootStrap {
public static Customer start() {
public static Customer start() {
//获取当前的注解上的版本然后去调用相应的远端方法 反射的方法
//当前客户端启动器class对象
RpcClientBootStrap annotation = ClientCall.class.getAnnotation(RpcClientBootStrap.class);
String currentVersion = annotation.version();
//根据注解获得的版本进行判断是哪个版本 然后进行启动
switch (currentVersion)
{
switch (currentVersion) {
//1.2版本之前都是键盘输入 所以不是根据代理对象来进行调用的 暂时注释掉
// case "1.0":
// NIOConsumerBootStrap10.main(null);
Expand All @@ -42,7 +42,7 @@ public static Customer start() {
try {
throw new RpcException("太着急了兄弟,这个版本还没出呢!要不你给我提个PR");
} catch (RpcException e) {
log.error(e.getMessage(),e);
log.error(e.getMessage(), e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

//之后启动直接在这边启动根据 在注解中配置对应的版本号 将相应的操作封装到之后的操作中即可
//比如说这里的version 1.2 就是v1.2版本的启动器

/**
* @author 祝英台炸油条
*/
@Slf4j
public class NIOServerBootStrap {

public static void start() {
public static void start() {

//先对ZK进行初始化
ZK.init();
Expand All @@ -31,24 +32,23 @@ public static void start() {
String[] methods = nowAnnotation.method();
int[] startNums = nowAnnotation.startNum();
//如果不存在那就返回
if (methods.length==0)return;
if (methods.length == 0) return;
//2.需要组合在一起传过去 如果不组合分别传 我怕就是端口号会出现问题
StringBuilder methodBuilder = new StringBuilder();
StringBuilder numBuilder = new StringBuilder();

//因为两个数量一致 那就不进行两次循环了
for (int i = 0;i< methods.length;++i) {
for (int i = 0; i < methods.length; ++i) {
methodBuilder.append(methods[i]);
methodBuilder.append(",");
numBuilder.append(startNums[i]);
numBuilder.append(",");
}
//除去最后多出来的,
methodBuilder.deleteCharAt(methodBuilder.length()-1);
numBuilder.deleteCharAt(numBuilder.length()-1);
methodBuilder.deleteCharAt(methodBuilder.length() - 1);
numBuilder.deleteCharAt(numBuilder.length() - 1);

switch (currentServerBootStrapVersion)
{
switch (currentServerBootStrapVersion) {
case "1.0":
NIOProviderBootStrap10.main(null);
break;
Expand All @@ -70,7 +70,7 @@ public static void start() {
try {
throw new RpcException("太着急了兄弟,这个版本还没出呢!要不你给我提个PR");
} catch (RpcException e) {
log.error(e.getMessage(),e);
log.error(e.getMessage(), e);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions zyt-rpc-common/src/main/java/annotation/CodecSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/**
* @author 祝英台炸油条
*/
@Deprecated
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CodecSelector {
Expand Down
17 changes: 17 additions & 0 deletions zyt-rpc-common/src/main/java/annotation/CompressSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @Author ףӢ̨ըÓÍÌõ
* @Time : 2022/5/20 20:55
**/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CompressSelector {
String CompressTool();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RegistryChosen {
String registryName() default "zookeeper";
String registryName();
}
9 changes: 5 additions & 4 deletions zyt-rpc-common/src/main/java/codec/AddCodec.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codec;

import annotation.CodecSelector;
import annotation.RpcSerializationSelector;
import configuration.GlobalConfiguration;
import entity.PersonPOJO;
import exception.RpcException;
import io.netty.channel.ChannelPipeline;
Expand All @@ -14,7 +16,6 @@
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;
import serialization.Serialization;

import java.lang.reflect.Method;

Expand All @@ -29,15 +30,15 @@ public class AddCodec {

public static void addCodec(ChannelPipeline pipeline, Method method, boolean isConsumer) {
//根据注解进行编解码器的选择
CodecSelector annotation = Serialization.class.getAnnotation(CodecSelector.class);
RpcSerializationSelector annotation = GlobalConfiguration.class.getAnnotation(RpcSerializationSelector.class);

//目前而来我的传输 传入的参数都是一个 所以根据这一个传入和返回的参数的类型进行判断
//下面是我传入的参数 和传出的参数
Class<?> returnType = method.getReturnType();
Class<?> parameterType = method.getParameterTypes()[0];

String codec = annotation.Codec();
switch (codec) {
String rpcSerialization = annotation.RpcSerialization();
switch (rpcSerialization) {
case "ObjectCodec": //2.2版本之前会使用
if (returnType != String.class && parameterType != String.class) {
pipeline.addLast(new ObjectEncoder());
Expand Down
2 changes: 2 additions & 0 deletions zyt-rpc-common/src/main/java/compress/Compress.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

/**
* @author 祝英台炸油条
* 之前的解压缩是否开启注册类 这个现在已经过时了
*/
@Deprecated
@CompressFunction(isOpenFunction = true)
public interface Compress {

Expand Down
58 changes: 50 additions & 8 deletions zyt-rpc-common/src/main/java/compress/CompressTypeTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

//实际调用 通过SPI机制

import annotation.CompressSelector;
import compress.bzip.BZipUtils;
import compress.deflater.DeflaterUtils;
import compress.diyzip.DiyZipUtils;
import compress.gzip.GZipUtils;
import compress.lz4.Lz4Utils;
import compress.zip.ZipUtils;
import configuration.GlobalConfiguration;
import exception.RpcException;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -11,20 +19,54 @@
*/
@Slf4j
public class CompressTypeTool implements CompressType {
//获取通过SPI机制获取的相应工具类

static CompressType compressUtils;

// // 获取通过SPI机制获取的相应工具类 想用SPI机制开启这个即可
// static {
// compressUtils = SPICompressUtils.getUtils();
// if (compressUtils == null) {
// try {
// throw new RpcException("相应工具类为空");
// } catch (RpcException e) {
// log.error(e.getMessage(), e);
// }
// }
// }


//通过注解获取对应的工具
static {
compressUtils = SPICompressUtils.getUtils();
if (compressUtils == null) {
try {
throw new RpcException("相应工具类为空");
} catch (RpcException e) {
log.error(e.getMessage(), e);
}
String compressTool = GlobalConfiguration.class.getAnnotation(CompressSelector.class).CompressTool();
switch (compressTool) {
case "BZipUtils":
compressUtils = new BZipUtils();
break;
case "DeflaterUtils":
compressUtils = new DeflaterUtils();
break;
case "GZipUtils":
compressUtils = new GZipUtils();
break;
case "Lz4Utils":
compressUtils = new Lz4Utils();
break;
case "ZipUtils":
compressUtils = new ZipUtils();
break;
case "DiyZipUtils":
compressUtils = new DiyZipUtils();
break;
default:
try {
throw new RpcException("兄弟 尚未定义该器件");
} catch (RpcException e) {
log.error(e.getMessage(), e);
}
}
}


@Override
public byte[] compress(byte[] bytes) {
return compressUtils.compress(bytes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package configuration;

import annotation.*;
import loadbalance.RandomLoadBalance;

/**
* @Author 祝英台炸油条
* @Time : 2022/5/20 20:42
* 全局配置 将所有的配置都配置在它上面
* 解压缩器 序列化器 注册中心 负载均衡 心跳机制等
**/
/*
@RegistryChosen
zookeeper zk注册中心
nacos nacos实现注册中心
zkCurator Curator协助操作注册中心
*/

/*
@CodecSelector
ObjectCodec
protoc
kryo
protostuff
hessian
fst
avro
jackson
fastjson
gson
*/

/*
@LoadBalanceMethodImpl
RandomLoadBalance.class
AccessLoadBalance.class
ConsistentLoadBalance.class
*/

/*
@CompressSelector
BZipUtils
DeflaterUtils
GZipUtils
Lz4Utils
ZipUtils
*/


@CompressFunction(isOpenFunction = true)
@CompressSelector(CompressTool = "DeflaterUtils")
@HeartBeatTool(isOpenFunction = true,
readerIdleTimeSeconds = 4,
writerIdleTimeSeconds = 4,
allIdleTimeSeconds = 2)
@LoadBalanceMethodImpl(chosenMethod = RandomLoadBalance.class)
@RegistryChosen(registryName = "zookeeper")
@RpcSerializationSelector(RpcSerialization = "fastjson")
public class GlobalConfiguration {
}
3 changes: 2 additions & 1 deletion zyt-rpc-common/src/main/java/heartbeat/HeartBeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

/**
* @author 祝英台炸油条
* 心跳机制 工具类
* 心跳机制 工具类 这个类别过时了 统一管理在统一配置类中
*/
@Deprecated
@HeartBeatTool(isOpenFunction = true,
readerIdleTimeSeconds = 4,
writerIdleTimeSeconds = 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
@Slf4j
public class ConsistentLoadBalance implements LoadBalance {
@Override
public String loadBalance(ZooKeeper zooKeeper, String path) throws InterruptedException, KeeperException {
List<String> children = zooKeeper.getChildren(path, false, null);
public String loadBalance(ZooKeeper zooKeeper, String path) {
List<String> children = null;
try {
children = zooKeeper.getChildren(path, false, null);
} catch (KeeperException | InterruptedException e) {
log.error(e.getMessage(),e);
}
assert children != null;
if (children.isEmpty()) {
try {
throw new RpcException("当前没有服务器提供该服务 请联系工作人员");
Expand Down
6 changes: 2 additions & 4 deletions zyt-rpc-common/src/main/java/loadbalance/LoadBalance.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package loadbalance;


import annotation.LoadBalanceMethodImpl;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

/*
Expand All @@ -12,8 +10,8 @@
*/

//实现不同的负载均衡策略
@LoadBalanceMethodImpl(chosenMethod = RandomLoadBalance.class)

public interface LoadBalance {
//通过负载均衡策略返回相应地址
String loadBalance(ZooKeeper zooKeeper, String path) throws InterruptedException, KeeperException;
String loadBalance(ZooKeeper zooKeeper, String path);
}
Loading

0 comments on commit 2c4f01d

Please sign in to comment.