Skip to content

Commit

Permalink
5.5 v2.1 Netty实现了全新的netty传输信息,使用了代理模式,异步调用的方式,线程的一些锁的方式,还有,同时兼容了之前的注册…
Browse files Browse the repository at this point in the history
…中心,负载均衡方法。
  • Loading branch information
zzzzzzzzyt committed May 5, 2022
1 parent 39c5bd8 commit 57af6d3
Show file tree
Hide file tree
Showing 38 changed files with 641 additions and 106 deletions.
24 changes: 24 additions & 0 deletions zyt-rpc-call/src/main/java/service/ClientCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package service;

import annotation.RpcClientBootStrap;
import annotation.RpcToolsSelector;
import exception.RpcException;
import method.Customer;
import service.call.ChosenClientCall;

import java.io.IOException;

//总客户端启动类 用户调用 什么版本的 和用什么工具 使用什么注册中心 序列化的选择 都可以用这个来玩
//注册中心不能给过去 这样就是重复依赖了
@RpcClientBootStrap(version = "2.1")
@RpcToolsSelector(rpcTool = "Netty")
public class ClientCall {
public static void main(String[] args) throws RpcException, IOException, InterruptedException {
//实现调用
Customer customer = ChosenClientCall.start();

System.out.println(customer.Hello("success"));
System.out.println(customer.Bye("fail"));
System.out.println(customer.Hello("fail"));
}
}
20 changes: 20 additions & 0 deletions zyt-rpc-call/src/main/java/service/ServerCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package service;

import annotation.RpcMethodCluster;
import annotation.RpcServerBootStrap;
import annotation.RpcToolsSelector;
import org.apache.zookeeper.KeeperException;
import service.call.ChosenServerCall;

import java.io.IOException;

//总服务端启动类 用户调用 注解是 注册什么方法进去
//调用的是什么版本的服务端启动方法
@RpcMethodCluster(method = {"Hello","Bye"},startNum = {2,3})
@RpcServerBootStrap(version = "2.1")
@RpcToolsSelector(rpcTool = "Netty")
public class ServerCall {
public static void main(String[] args) throws IOException, InterruptedException, KeeperException, NoSuchMethodException {
ChosenServerCall.start();
}
}
28 changes: 28 additions & 0 deletions zyt-rpc-call/src/main/java/service/call/ChosenClientCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package service.call;

import annotation.RpcToolsSelector;
import exception.RpcException;
import method.Customer;
import service.ClientCall;
import service.call.netty_call.NettyClientCall;
import service.call.nio_call.NIOClientCall;

import java.io.IOException;

//根据获取对应的启动类注解 来选择启动方法
public class ChosenClientCall {
public static Customer start() throws InterruptedException, RpcException, IOException {
RpcToolsSelector annotation = ClientCall.class.getAnnotation(RpcToolsSelector.class);
switch (annotation.rpcTool())
{
//暂时还没有 return的对象
case "Netty":
return NettyClientCall.main(null);
case "Nio":
return NIOClientCall.main(null);
default:
System.out.println("还没有那个方法呢,要不你写一个给我提个pr,我直接采纳");
throw new RpcException("暂时还没有该方法,博主正在努力跟进中");
}
}
}
28 changes: 28 additions & 0 deletions zyt-rpc-call/src/main/java/service/call/ChosenServerCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package service.call;


import annotation.RpcToolsSelector;
import org.apache.zookeeper.KeeperException;
import service.ServerCall;
import service.call.netty_call.NettyServerCall;
import service.call.nio_call.NIOServerCall;

import java.io.IOException;

//根据获取对应的启动类注解 来选择启动方法
public class ChosenServerCall {
public static void start() throws IOException, InterruptedException, KeeperException, NoSuchMethodException {
RpcToolsSelector annotation = ServerCall.class.getAnnotation(RpcToolsSelector.class);
switch (annotation.rpcTool())
{
case "Netty":
NettyServerCall.main(null);
break;
case "Nio":
NIOServerCall.main(null);
break;
default:
System.out.println("还没有那个方法呢,要不你写一个给我提个pr,我直接采纳");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package service.call.netty_call;

import exception.RpcException;
import method.Customer;
import service.netty_bootstrap.NettyClientBootStrap;

//客户端启动类
public class NettyClientCall {
public static Customer main(String[] args) throws InterruptedException, RpcException {
return NettyClientBootStrap.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package service.netty_call;
package service.call.netty_call;

import org.apache.zookeeper.KeeperException;
import service.netty_bootstrap.NettyServerBootStrap;

import java.io.IOException;

//启动类 给定对应的端口 进行启动并监听
public class NettyServerCall {
public static void main(String[] args) throws InterruptedException {
NettyServerBootStrap.start("127.0.0.1",6668);
public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
NettyServerBootStrap.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package service.call.nio_call;


import exception.RpcException;
import method.Customer;
import service.nio_bootstrap.NIOClientBootStrap;

import java.io.IOException;

//通用启动类 将启动的逻辑藏在ClientBootStrap中
public class NIOClientCall {
public static Customer main(String[] args) throws IOException, RpcException {
return NIOClientBootStrap.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package service.nio_call;
package service.call.nio_call;



import annotation.RpcMethodCluster;
import org.apache.zookeeper.KeeperException;
import service.nio_bootstrap.NIOServerBootStrap;

import java.io.IOException;

//通用启动类 将启动的逻辑藏在ServerBootStrap中
//注解 看你想启动多少个服务和对应的方法
@RpcMethodCluster(method = {"Hello","Bye"},startNum = {2,3})

public class NIOServerCall {
public static void main(String[] args) throws IOException, InterruptedException, KeeperException, NoSuchMethodException {
NIOServerBootStrap.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package service.netty_bootstrap;


import annotation.RpcClientBootStrap;
import consumer.bootstrap.netty.NettyConsumerBootStrap20;
import consumer.bootstrap.netty.NettyConsumerBootStrap21;
import exception.RpcException;
import method.Customer;
import service.ClientCall;

//netty客户端的启动类
public class NettyClientBootStrap {
public static void start(String address, int port) throws InterruptedException {
NettyConsumerBootStrap20.main(new String[]{address, String.valueOf(port)});
public static Customer start() throws InterruptedException, RpcException {
return start0();
}

private static Customer start0() throws InterruptedException, RpcException {

//获取对应的版本号 然后选取对应的版本进行调用
String currentClientVersion = ClientCall.class.getAnnotation(RpcClientBootStrap.class).version();

switch (currentClientVersion)
{
case "2.0": //2.0就是简单实现远端调用 所以没实现太那个
NettyConsumerBootStrap20.main(new String[]{"127.0.0.1", String.valueOf(6668)});
return null;
case "2.1":
return NettyConsumerBootStrap21.main(null);
default:
System.out.println("该版本还没出呢,你如果有想法可以私信我,或者提个pr");
throw new RpcException("出现问题");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
package service.netty_bootstrap;

import annotation.RpcMethodCluster;
import annotation.RpcServerBootStrap;
import init.ZK;
import org.apache.zookeeper.KeeperException;
import provider.bootstrap.netty.NettyProviderBootStrap20;
import provider.bootstrap.netty.NettyProviderBootStrap21;
import service.ServerCall;

import java.io.IOException;

public class NettyServerBootStrap {
public static void start(String address,int port) throws InterruptedException {
NettyProviderBootStrap20.main(new String[]{address, String.valueOf(port)});
public static void start() throws InterruptedException, IOException, KeeperException {
//先对ZK进行初始化
ZK.init();
RpcServerBootStrap annotation = ServerCall.class.getAnnotation(RpcServerBootStrap.class);
//当前服务端启动器 class对象
String currentServerBootStrapVersion = annotation.version();

//获取对应的方法和个数 然后进行启动
//1.获取对应方法 在获取对应的注解 注解中的属性
RpcMethodCluster nowAnnotation = ServerCall.class.getAnnotation(RpcMethodCluster.class);
String[] methods = nowAnnotation.method();
int[] startNums = nowAnnotation.startNum();
//如果不存在那就返回
if (methods.length==0)return;
//2.需要组合在一起传过去 如果不组合分别传 我怕就是端口号会出现问题
StringBuilder methodBuilder = new StringBuilder();
StringBuilder numBuilder = new StringBuilder();
for (String method : methods) {
methodBuilder.append(method);
methodBuilder.append(",");
}
methodBuilder.deleteCharAt(methodBuilder.length()-1);
for (int startNum : startNums) {
numBuilder.append(startNum);
numBuilder.append(",");
}
numBuilder.deleteCharAt(numBuilder.length()-1);

//根据对应的启动版本进行启动
switch (currentServerBootStrapVersion)
{

case "2.0": //2.0版本只是进行了测试 简单的实现了远端信息传输
NettyProviderBootStrap20.main(new String[]{"127.0.0.1",String.valueOf(6668)});
break;
case "2.1":
NettyProviderBootStrap21.main(new String[]{methodBuilder.toString(),numBuilder.toString()});
break;
default:
System.out.println("兄弟,该版本还在脑海中构思,如果你有想法可以pr给我");
}
}
}
10 changes: 0 additions & 10 deletions zyt-rpc-call/src/main/java/service/netty_call/NettyClientCall.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import consumer.bootstrap.nio.*;
import exception.RpcException;
import method.Customer;
import service.ClientCall;


import java.io.IOException;

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

public class NIOClientBootStrap {
public static Customer start() throws IOException, RpcException {
//获取当前的注解上的版本然后去调用相应的远端方法 反射的方法
//当前客户端启动器class对象
Class<NIOClientBootStrap> currentClientBootStrapClass = NIOClientBootStrap.class;
RpcClientBootStrap annotation = currentClientBootStrapClass.getAnnotation(RpcClientBootStrap.class);
RpcClientBootStrap annotation = ClientCall.class.getAnnotation(RpcClientBootStrap.class);
String currentVersion = annotation.version();
//根据注解获得的版本进行判断是哪个版本 然后进行启动
switch (currentVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import init.ZK;
import org.apache.zookeeper.KeeperException;
import provider.bootstrap.nio.*;
import service.nio_call.NIOServerCall;
import service.ServerCall;

import java.io.IOException;


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

public class NIOServerBootStrap {


Expand All @@ -21,14 +21,13 @@ public static void start() throws IOException, InterruptedException, KeeperExcep

//先对ZK进行初始化
ZK.init();
Class<NIOServerBootStrap> serverBootStrapClass = NIOServerBootStrap.class;
RpcServerBootStrap annotation = serverBootStrapClass.getAnnotation(RpcServerBootStrap.class);
RpcServerBootStrap annotation = ServerCall.class.getAnnotation(RpcServerBootStrap.class);
//当前服务端启动器 class对象
String currentServerBootStrapVersion = annotation.version();

//获取对应的方法和个数 然后进行启动
//1.获取对应方法 在获取对应的注解 注解中的属性
RpcMethodCluster nowAnnotation = NIOServerCall.class.getAnnotation(RpcMethodCluster.class);
RpcMethodCluster nowAnnotation = ServerCall.class.getAnnotation(RpcMethodCluster.class);
String[] methods = nowAnnotation.method();
int[] startNums = nowAnnotation.startNum();
//如果不存在那就返回
Expand Down
19 changes: 0 additions & 19 deletions zyt-rpc-call/src/main/java/service/nio_call/NIOClientCall.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package annotation;

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

//Rpc序列化方法的选择
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RpcSerializationSelector {
String RpcSerialization();
}
13 changes: 13 additions & 0 deletions zyt-rpc-common/src/main/java/annotation/RpcToolsSelector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package annotation;

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

//进行rpc工具的选择
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RpcToolsSelector {
String rpcTool() default "NIO";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package provider.bootstrap.nio;
package register;

import annotation.RegistryChosen;

//注册中心的选择在这配置
//注册中心的选择 启用的是nacos 目前
@RegistryChosen(registryName = "zkCurator")
public interface NIOProviderBootStrap {
public interface Register {
}
Loading

0 comments on commit 57af6d3

Please sign in to comment.