diff --git a/zyt-rpc-call/src/main/java/service/ClientCall.java b/zyt-rpc-call/src/main/java/service/ClientCall.java new file mode 100644 index 0000000..db3f178 --- /dev/null +++ b/zyt-rpc-call/src/main/java/service/ClientCall.java @@ -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")); + } +} diff --git a/zyt-rpc-call/src/main/java/service/ServerCall.java b/zyt-rpc-call/src/main/java/service/ServerCall.java new file mode 100644 index 0000000..a7e3442 --- /dev/null +++ b/zyt-rpc-call/src/main/java/service/ServerCall.java @@ -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(); + } +} diff --git a/zyt-rpc-call/src/main/java/service/call/ChosenClientCall.java b/zyt-rpc-call/src/main/java/service/call/ChosenClientCall.java new file mode 100644 index 0000000..ffc72b6 --- /dev/null +++ b/zyt-rpc-call/src/main/java/service/call/ChosenClientCall.java @@ -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("暂时还没有该方法,博主正在努力跟进中"); + } + } +} diff --git a/zyt-rpc-call/src/main/java/service/call/ChosenServerCall.java b/zyt-rpc-call/src/main/java/service/call/ChosenServerCall.java new file mode 100644 index 0000000..f873664 --- /dev/null +++ b/zyt-rpc-call/src/main/java/service/call/ChosenServerCall.java @@ -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,我直接采纳"); + } + } +} diff --git a/zyt-rpc-call/src/main/java/service/call/netty_call/NettyClientCall.java b/zyt-rpc-call/src/main/java/service/call/netty_call/NettyClientCall.java new file mode 100644 index 0000000..32ad104 --- /dev/null +++ b/zyt-rpc-call/src/main/java/service/call/netty_call/NettyClientCall.java @@ -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(); + } +} diff --git a/zyt-rpc-call/src/main/java/service/netty_call/NettyServerCall.java b/zyt-rpc-call/src/main/java/service/call/netty_call/NettyServerCall.java similarity index 53% rename from zyt-rpc-call/src/main/java/service/netty_call/NettyServerCall.java rename to zyt-rpc-call/src/main/java/service/call/netty_call/NettyServerCall.java index 8029c29..068945e 100644 --- a/zyt-rpc-call/src/main/java/service/netty_call/NettyServerCall.java +++ b/zyt-rpc-call/src/main/java/service/call/netty_call/NettyServerCall.java @@ -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(); } } diff --git a/zyt-rpc-call/src/main/java/service/call/nio_call/NIOClientCall.java b/zyt-rpc-call/src/main/java/service/call/nio_call/NIOClientCall.java new file mode 100644 index 0000000..f32cd71 --- /dev/null +++ b/zyt-rpc-call/src/main/java/service/call/nio_call/NIOClientCall.java @@ -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(); + } +} diff --git a/zyt-rpc-call/src/main/java/service/nio_call/NIOServerCall.java b/zyt-rpc-call/src/main/java/service/call/nio_call/NIOServerCall.java similarity index 68% rename from zyt-rpc-call/src/main/java/service/nio_call/NIOServerCall.java rename to zyt-rpc-call/src/main/java/service/call/nio_call/NIOServerCall.java index 2f6856d..ce11d5d 100644 --- a/zyt-rpc-call/src/main/java/service/nio_call/NIOServerCall.java +++ b/zyt-rpc-call/src/main/java/service/call/nio_call/NIOServerCall.java @@ -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(); diff --git a/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyClientBootStrap.java b/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyClientBootStrap.java index ebe4777..74ee6c0 100644 --- a/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyClientBootStrap.java +++ b/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyClientBootStrap.java @@ -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("出现问题"); + } } } diff --git a/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyServerBootStrap.java b/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyServerBootStrap.java index 23f296e..2be7905 100644 --- a/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyServerBootStrap.java +++ b/zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyServerBootStrap.java @@ -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给我"); + } } } diff --git a/zyt-rpc-call/src/main/java/service/netty_call/NettyClientCall.java b/zyt-rpc-call/src/main/java/service/netty_call/NettyClientCall.java deleted file mode 100644 index 9684ce0..0000000 --- a/zyt-rpc-call/src/main/java/service/netty_call/NettyClientCall.java +++ /dev/null @@ -1,10 +0,0 @@ -package service.netty_call; - -import service.netty_bootstrap.NettyClientBootStrap; - -//客户端启动类 -public class NettyClientCall { - public static void main(String[] args) throws InterruptedException { - NettyClientBootStrap.start("127.0.0.1",6668); - } -} diff --git a/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOClientBootStrap.java b/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOClientBootStrap.java index 1ee3151..77828ba 100644 --- a/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOClientBootStrap.java +++ b/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOClientBootStrap.java @@ -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 currentClientBootStrapClass = NIOClientBootStrap.class; - RpcClientBootStrap annotation = currentClientBootStrapClass.getAnnotation(RpcClientBootStrap.class); + RpcClientBootStrap annotation = ClientCall.class.getAnnotation(RpcClientBootStrap.class); String currentVersion = annotation.version(); //根据注解获得的版本进行判断是哪个版本 然后进行启动 switch (currentVersion) diff --git a/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOServerBootStrap.java b/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOServerBootStrap.java index ce04efa..dbefd25 100644 --- a/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOServerBootStrap.java +++ b/zyt-rpc-call/src/main/java/service/nio_bootstrap/NIOServerBootStrap.java @@ -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 { @@ -21,14 +21,13 @@ public static void start() throws IOException, InterruptedException, KeeperExcep //先对ZK进行初始化 ZK.init(); - Class 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(); //如果不存在那就返回 diff --git a/zyt-rpc-call/src/main/java/service/nio_call/NIOClientCall.java b/zyt-rpc-call/src/main/java/service/nio_call/NIOClientCall.java deleted file mode 100644 index a2337f9..0000000 --- a/zyt-rpc-call/src/main/java/service/nio_call/NIOClientCall.java +++ /dev/null @@ -1,19 +0,0 @@ -package service.nio_call; - - -import exception.RpcException; -import method.Customer; -import service.nio_bootstrap.NIOClientBootStrap; - -import java.io.IOException; - -//通用启动类 将启动的逻辑藏在ClientBootStrap中 -public class NIOClientCall { - public static void main(String[] args) throws IOException, RpcException { - Customer customer = NIOClientBootStrap.start(); - //实现调用 - System.out.println(customer.Hello("success")); - System.out.println(customer.Bye("fail")); - System.out.println(customer.Hello("fail")); - } -} diff --git a/zyt-rpc-common/src/main/java/annotation/RpcSerializationSelector.java b/zyt-rpc-common/src/main/java/annotation/RpcSerializationSelector.java new file mode 100644 index 0000000..e79201d --- /dev/null +++ b/zyt-rpc-common/src/main/java/annotation/RpcSerializationSelector.java @@ -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(); +} diff --git a/zyt-rpc-common/src/main/java/annotation/RpcToolsSelector.java b/zyt-rpc-common/src/main/java/annotation/RpcToolsSelector.java new file mode 100644 index 0000000..fddf8c0 --- /dev/null +++ b/zyt-rpc-common/src/main/java/annotation/RpcToolsSelector.java @@ -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"; +} diff --git a/zyt-rpc-provider/src/main/java/provider/bootstrap/nio/NIOProviderBootStrap.java b/zyt-rpc-common/src/main/java/register/Register.java similarity index 61% rename from zyt-rpc-provider/src/main/java/provider/bootstrap/nio/NIOProviderBootStrap.java rename to zyt-rpc-common/src/main/java/register/Register.java index 244162a..2849add 100644 --- a/zyt-rpc-provider/src/main/java/provider/bootstrap/nio/NIOProviderBootStrap.java +++ b/zyt-rpc-common/src/main/java/register/Register.java @@ -1,8 +1,9 @@ -package provider.bootstrap.nio; +package register; import annotation.RegistryChosen; +//注册中心的选择在这配置 //注册中心的选择 启用的是nacos 目前 @RegistryChosen(registryName = "zkCurator") -public interface NIOProviderBootStrap { +public interface Register { } diff --git a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/netty/NettyConsumerBootStrap21.java b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/netty/NettyConsumerBootStrap21.java new file mode 100644 index 0000000..d01b96b --- /dev/null +++ b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/netty/NettyConsumerBootStrap21.java @@ -0,0 +1,15 @@ +package consumer.bootstrap.netty; + + +import consumer.proxy.RpcNettyClientProxy; +import method.Customer; + +/* + 以netty为网络编程框架的消费者端启动类 + */ +//进行启动 提供类的方式即可 +public class NettyConsumerBootStrap21 { + public static Customer main(String[] args) throws InterruptedException { + return (Customer) RpcNettyClientProxy.getBean(Customer.class); + } +} diff --git a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap12.java b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap12.java index b3d3e99..8a9cdda 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap12.java +++ b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap12.java @@ -1,7 +1,7 @@ package consumer.bootstrap.nio; -import consumer.proxy.RpcClientProxy; +import consumer.proxy.RpcNioClientProxy; import method.Customer; import java.io.IOException; @@ -12,7 +12,7 @@ public class NIOConsumerBootStrap12 { public static Customer main(String[] args) throws IOException { - RpcClientProxy clientProxy = new RpcClientProxy(); + RpcNioClientProxy clientProxy = new RpcNioClientProxy(); return (Customer) clientProxy.getBean(Customer.class); } } diff --git a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap14.java b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap14.java index f337857..3bde684 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap14.java +++ b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap14.java @@ -1,7 +1,7 @@ package consumer.bootstrap.nio; -import consumer.proxy.RpcClientProxy; +import consumer.proxy.RpcNioClientProxy; import method.Customer; import java.io.IOException; @@ -12,7 +12,7 @@ public class NIOConsumerBootStrap14 { public static Customer main(String[] args) throws IOException { - RpcClientProxy clientProxy = new RpcClientProxy(); + RpcNioClientProxy clientProxy = new RpcNioClientProxy(); return (Customer) clientProxy.getBean(Customer.class); } } diff --git a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap15.java b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap15.java index 359fe0d..2e14805 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap15.java +++ b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap15.java @@ -1,7 +1,7 @@ package consumer.bootstrap.nio; -import consumer.proxy.RpcClientProxy; +import consumer.proxy.RpcNioClientProxy; import method.Customer; import java.io.IOException; @@ -13,7 +13,7 @@ public class NIOConsumerBootStrap15{ public static Customer main(String[] args) throws IOException { - RpcClientProxy clientProxy = new RpcClientProxy(); + RpcNioClientProxy clientProxy = new RpcNioClientProxy(); return (Customer) clientProxy.getBean(Customer.class); } diff --git a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootstrap.java b/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootstrap.java deleted file mode 100644 index 1e0dc8b..0000000 --- a/zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootstrap.java +++ /dev/null @@ -1,9 +0,0 @@ -package consumer.bootstrap.nio; - -import annotation.RegistryChosen; - -//这个类就是为了放一些注解的 - -@RegistryChosen(registryName = "zkCurator") -public interface NIOConsumerBootstrap { -} diff --git a/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient20.java b/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient20.java index c4343da..df99e7d 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient20.java +++ b/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient20.java @@ -1,6 +1,6 @@ package consumer.netty; -import consumer.netty_client_handler.NettyClientHandler01; +import consumer.netty_client_handler.NettyClientHandler20; import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; @@ -21,7 +21,7 @@ public static void start(String hostName, int port) throws InterruptedException @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); - pipeline.addLast(new NettyClientHandler01()); + pipeline.addLast(new NettyClientHandler20()); } }); diff --git a/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient21.java b/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient21.java new file mode 100644 index 0000000..f7d903f --- /dev/null +++ b/zyt-rpc-consumer/src/main/java/consumer/netty/NettyClient21.java @@ -0,0 +1,64 @@ +package consumer.netty; + + +import consumer.netty_client_handler.NettyClientHandler21; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +//实际客户端启动类 进行操作 +//不确定能返回什么 所以判断是对象 +public class NettyClient21 { + + //线程池 实现异步调用 + private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + + static NettyClientHandler21 clientHandler; + + public static void initClient(String hostName,int port) + { + + clientHandler = new NettyClientHandler21(); + //建立客户端监听 + Bootstrap bootstrap = new Bootstrap(); + EventLoopGroup workGroup = new NioEventLoopGroup(); + + try { + bootstrap.group(workGroup) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + ChannelPipeline pipeline = socketChannel.pipeline(); + pipeline.addLast(new StringEncoder());//传输必须加编解码器 不然不认识的类传不过去 + pipeline.addLast(new StringDecoder()); + pipeline.addLast(clientHandler); + } + }); + + //进行连接 + bootstrap.connect(hostName, port).sync(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static Object callMethod(String hostName, int port, Object param) throws Exception { + + //我是有多个地方进行调用的 不能只连接一个 + initClient(hostName,port); + clientHandler.setParam(param); + //接下来这就有关系到调用 直接调用 + return executor.submit(clientHandler).get(); + } +} diff --git a/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler01.java b/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler20.java similarity index 92% rename from zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler01.java rename to zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler20.java index 148106b..7574814 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler01.java +++ b/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler20.java @@ -6,7 +6,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; -public class NettyClientHandler01 extends ChannelInboundHandlerAdapter { +public class NettyClientHandler20 extends ChannelInboundHandlerAdapter { //通道就绪就会发的信息 @Override diff --git a/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler21.java b/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler21.java new file mode 100644 index 0000000..d421f09 --- /dev/null +++ b/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler21.java @@ -0,0 +1,50 @@ +package consumer.netty_client_handler; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + + +import java.util.concurrent.Callable; + + +//实现了Callable接口实现了异步调用 + +public class NettyClientHandler21 extends ChannelInboundHandlerAdapter implements Callable{ + //传入的参数 + private Object param; + private Object response; + private ChannelHandlerContext context; + + public void setParam(Object param) { + this.param = param; + } + + //当成功建立 就赋值上下文对象 + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + context = ctx; + System.out.println("U•ェ•*U 成功连接"); + + } + + @Override + public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + response = msg; + notify(); + } + + //调用的时候 就进行传输 + @Override + public synchronized Object call() throws Exception { + context.writeAndFlush(param); + wait(); + return response; + } + + //异常处理 + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + cause.printStackTrace(); + ctx.close(); + } +} diff --git a/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcNettyClientProxy.java b/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcNettyClientProxy.java new file mode 100644 index 0000000..95831fd --- /dev/null +++ b/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcNettyClientProxy.java @@ -0,0 +1,65 @@ +package consumer.proxy; + +import annotation.RegistryChosen; +import consumer.netty.NettyClient21; +import consumer.service_discovery.NacosServiceDiscovery; +import consumer.service_discovery.ZkCuratorDiscovery; +import consumer.service_discovery.ZkServiceDiscovery; +import exception.RpcException; +import register.Register; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +//就是获取代理类 通过代理类中的方法进行对应方法的执行和获取 +public class RpcNettyClientProxy { + private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + //参数 就是我要对其生成代理类的类 + public static Object getBean(final Class serviceClass) + { + return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), + new Class[]{serviceClass}, + new InvocationHandler() { + //根据对应的方法 代理类进行处理 + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + String methodName = method.getName(); + Object param = args[0]; + //获取对应的方法地址 + String methodAddress = getMethodAddress(methodName); + String[] strings = methodAddress.split(":"); + String hostName = strings[0]; + int port = Integer.valueOf(strings[1]); + //进行方法的调用 随即进行方法的调用 + return NettyClient21.callMethod(hostName,port,param); + } + }); + } + + /** + * 实际去获得对应的服务 并完成方法调用的方法 + * @param methodName 根据方法名 根据添加的注册中心注解来选择相应的注册中心进行 实现负载均衡获取一个方法对应地址 + * @param + * @return + */ + private static String getMethodAddress(String methodName) throws Exception { + //根据注解进行方法调用 + //根据在代理类上的注解调用 看清楚底下的因为是个class数组 可以直接继续获取 注解 + RegistryChosen annotation = Register.class.getAnnotation(RegistryChosen.class); + switch (annotation.registryName()) + { + case "nacos": + return NacosServiceDiscovery.getMethodAddress(methodName); + case "zookeeper": + return ZkServiceDiscovery.getMethodAddress(methodName); + case "zkCurator": + return ZkCuratorDiscovery.getMethodAddress(methodName); + default: + throw new RpcException("不存在该注册中心"); + } + } +} + diff --git a/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcClientProxy.java b/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcNioClientProxy.java similarity index 55% rename from zyt-rpc-consumer/src/main/java/consumer/proxy/RpcClientProxy.java rename to zyt-rpc-consumer/src/main/java/consumer/proxy/RpcNioClientProxy.java index 97707a6..5a9af9e 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcClientProxy.java +++ b/zyt-rpc-consumer/src/main/java/consumer/proxy/RpcNioClientProxy.java @@ -1,22 +1,20 @@ package consumer.proxy; import annotation.RegistryChosen; -import com.alibaba.nacos.api.exception.NacosException; -import consumer.bootstrap.nio.NIOConsumerBootstrap; import consumer.service_discovery.NacosServiceDiscovery; import consumer.service_discovery.ZkCuratorDiscovery; import consumer.service_discovery.ZkServiceDiscovery; import exception.RpcException; -import org.apache.zookeeper.KeeperException; +import register.Register; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; -//代理类的实现 -public class RpcClientProxy implements NIOConsumerBootstrap{ - +//代理类的实现 小修改了一下 就是不用频繁的去创建对象了 单例模式 每次启用 +public class RpcNioClientProxy { + public static Object rpcClientProxy; //获取代理对象 并返回 当前类别 public static Object getBean(final Class serviceClass){ /* @@ -25,21 +23,25 @@ public static Object getBean(final Class serviceClass){ 2、动态代理类需要实现的接口 class[]{xxxx.class} 得到的就是对应的类别 3、动态代理类执行方法的时候需要干的事 */ - return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), - new Class[]{serviceClass}, - new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - //暂时还没有设置回信这个操作 - String methodName = method.getName(); - //String response = ZkServiceDiscovery.getStart(methodName, (String) args[0]); - //String response = NacosServiceDiscovery.getStart(methodName, (String) args[0]); - //根据注解类进行调用 - String response = getResponse(methodName,(String) args[0]); - return response; + if (rpcClientProxy==null) + { + rpcClientProxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), + new Class[]{serviceClass}, + new InvocationHandler() { + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + //暂时还没有设置回信这个操作 + String methodName = method.getName(); + //String response = ZkServiceDiscovery.getStart(methodName, (String) args[0]); + //String response = NacosServiceDiscovery.getStart(methodName, (String) args[0]); + //根据注解类进行调用 + String response = getResponse(methodName,(String) args[0]); + return response; + } } - } - ); + ); + } + return rpcClientProxy; } /** @@ -51,8 +53,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl private static String getResponse(String methodName, String msg) throws Exception { //根据注解进行方法调用 //根据在代理类上的注解调用 看清楚底下的因为是个class数组 可以直接继续获取 注解 - Class[] interfaces = (Class[]) RpcClientProxy.class.getInterfaces(); - RegistryChosen annotation = interfaces[0].getAnnotation(RegistryChosen.class); + RegistryChosen annotation = Register.class.getAnnotation(RegistryChosen.class); switch (annotation.registryName()) { case "nacos": diff --git a/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkCuratorDiscovery.java b/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkCuratorDiscovery.java index 28a6a49..6424687 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkCuratorDiscovery.java +++ b/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkCuratorDiscovery.java @@ -8,14 +8,18 @@ import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; //简化zookeeper的使用 更加方便获取远端的方法的信息 public class ZkCuratorDiscovery { private static String connectString = RpcConstants.ZOOKEEPER_ADDRESS; - public static String getStart(String methodName, String msg) throws Exception { + + //获取对应的地址 + public static String getMethodAddress(String methodName) throws Exception { //创建连接后 获取对应的地址 地址和对应的端口信息 传入即可 //同时还要实现负载均衡 //BackoffRetry 退避策略,决定失败后如何确定补偿值。 @@ -44,11 +48,17 @@ public static String getStart(String methodName, String msg) throws Exception { //被选中的负载均衡实现类的对象 通过反射执行 获取对应的地址 Object methodChosenClass = methodClass.newInstance(); String methodAddress = (String) method.invoke(methodChosenClass,zooKeeper,prePath); + client.close(); + return methodAddress; + } + + + public static String getStart(String methodName, String msg) throws Exception { + String methodAddress = getMethodAddress(methodName); String[] strings = methodAddress.split(":"); //启动 String address = strings[0]; int port = Integer.valueOf(strings[1]); - client.close(); return NIONonBlockingClient12.start(address,port,msg); } } diff --git a/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkServiceDiscovery.java b/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkServiceDiscovery.java index 84ceb7b..95b0416 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkServiceDiscovery.java +++ b/zyt-rpc-consumer/src/main/java/consumer/service_discovery/ZkServiceDiscovery.java @@ -31,7 +31,9 @@ public void process(WatchedEvent watchedEvent) { } // 根据所请求的服务地址 获取对应的远端地址 - public static String getMethodAddress(String methodName) throws RpcException, InterruptedException, KeeperException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public static String getMethodAddress(String methodName) throws RpcException, InterruptedException, KeeperException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException { + //先进行连接 + getConnect(); //判断节点中是否存在对应路径 不存在则抛出异常 if (zooKeeper.exists("/service/"+methodName,null)==null) @@ -54,8 +56,6 @@ public static String getMethodAddress(String methodName) throws RpcException, In public static String getStart(String methodName,String msg) throws IOException, RpcException, InterruptedException, KeeperException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException { - //先进行连接 - getConnect(); //获取相应的远端地址 String methodAddress = getMethodAddress(methodName); //进行连接 diff --git a/zyt-rpc-provider/src/main/java/provider/bootstrap/netty/NettyProviderBootStrap21.java b/zyt-rpc-provider/src/main/java/provider/bootstrap/netty/NettyProviderBootStrap21.java new file mode 100644 index 0000000..ebac07f --- /dev/null +++ b/zyt-rpc-provider/src/main/java/provider/bootstrap/netty/NettyProviderBootStrap21.java @@ -0,0 +1,39 @@ +package provider.bootstrap.netty; + + + +import provider.netty.NettyServer21; + + +/* + 以netty为网络编程框架的服务提供端启动类 + */ +//实现方法和之前都是比较一致的 每个对象要开一个线程去执行呢 原因是我们会启动同步等他们关闭 才出来 这样才能关闭对应的管道 +public class NettyProviderBootStrap21 { + static volatile int port = 6666; //对应的端口 要传过去 注册到注册中心去 + public static void main(String[] args) throws InterruptedException { + //直接在这里将对应的方法什么的进行分开 然后传过去 + String methods = args[0]; + String nums = args[1]; + String[] methodArray = methods.split(","); + String[] methodNumArray = nums.split(","); + //进行创建 可能会出问题 这边的端口 + for (int i = 0; i < methodArray.length; i++) { + String methodName = methodArray[i]; + for (Integer methodNum = 0; methodNum < Integer.valueOf(methodNumArray[i]); methodNum++) { + new Thread(new Runnable() { + @Override + public void run() { + try { + NettyServer21.start(methodName,port++); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } + } + } +} diff --git a/zyt-rpc-provider/src/main/java/provider/netty/NettyServer20.java b/zyt-rpc-provider/src/main/java/provider/netty/NettyServer20.java index 77705a5..b314b04 100644 --- a/zyt-rpc-provider/src/main/java/provider/netty/NettyServer20.java +++ b/zyt-rpc-provider/src/main/java/provider/netty/NettyServer20.java @@ -5,7 +5,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -import provider.netty_server_handler.NettyServerHandler01; +import provider.netty_server_handler.NettyServerHandler20; //简单实现 主要还是进行了一段回想 @@ -26,7 +26,7 @@ protected void initChannel(SocketChannel socketChannel) throws Exception { //每个用户连接上都会进行初始化 System.out.println("客户socketChannel hashcode="+socketChannel.hashCode()); ChannelPipeline pipeline = socketChannel.pipeline();//每个通道都对应一个管道 将处理器往管道里放 - pipeline.addLast(new NettyServerHandler01()); + pipeline.addLast(new NettyServerHandler20()); } }); diff --git a/zyt-rpc-provider/src/main/java/provider/netty/NettyServer21.java b/zyt-rpc-provider/src/main/java/provider/netty/NettyServer21.java new file mode 100644 index 0000000..1057f04 --- /dev/null +++ b/zyt-rpc-provider/src/main/java/provider/netty/NettyServer21.java @@ -0,0 +1,68 @@ +package provider.netty; + + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import provider.netty_server_handler.NettyServerHandler21; +import provider.utils.MethodRegister; + +//进行启动 绑定然后创建对应的 然后注册进注册中心去 +public class NettyServer21 { + public static void start(String methodName,int port) throws Exception { + //真正的实现逻辑 被封装到下面的方法当中了 + start0(methodName,port); + } + + private static void start0(String methodName, int port) throws Exception { + //先将地址进行注册 + MethodRegister.register(methodName,"127.0.0.1",port); + + //开始创建相应的netty服务端 + ServerBootstrap serverBootstrap = new ServerBootstrap(); + + //创建对应的工作组 用于处理不同的事件 + EventLoopGroup bossGroup = new NioEventLoopGroup(1); + EventLoopGroup workGroup = new NioEventLoopGroup(); + + try { + //进行初始化 + serverBootstrap.group(bossGroup,workGroup) + .channel(NioServerSocketChannel.class) //自身实现的通道 + .option(ChannelOption.SO_BACKLOG,128) //设置线程队列得到的连接个数 + .childOption(ChannelOption.SO_KEEPALIVE,true) //设置保持活动连接状态 + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + System.out.println(socketChannel.remoteAddress()+"连接上了"); + ChannelPipeline pipeline = socketChannel.pipeline(); + pipeline.addLast(new StringEncoder()); + pipeline.addLast(new StringDecoder()); + pipeline.addLast(new NettyServerHandler21(methodName)); + } + }); + + ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); + + //对连接结果进行监听 + channelFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + if (channelFuture.isSuccess()) System.out.println("连接上"+port+"端口"); + else System.out.println("连接端口失败"); + } + }); + + //等待关闭 同步 + channelFuture.channel().closeFuture().sync(); + } finally { + //结束了的话 就进行关闭了 + bossGroup.shutdownGracefully(); + workGroup.shutdownGracefully(); + } + } +} diff --git a/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler01.java b/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler20.java similarity index 95% rename from zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler01.java rename to zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler20.java index 5922a85..774de52 100644 --- a/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler01.java +++ b/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler20.java @@ -7,7 +7,7 @@ import io.netty.util.CharsetUtil; //实现简单的服务注册和回写 -public class NettyServerHandler01 extends ChannelInboundHandlerAdapter { +public class NettyServerHandler20 extends ChannelInboundHandlerAdapter { //读取数据 @Override diff --git a/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler21.java b/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler21.java new file mode 100644 index 0000000..64347a4 --- /dev/null +++ b/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler21.java @@ -0,0 +1,40 @@ +package provider.netty_server_handler; + + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import java.lang.reflect.Method; + +//实现简单的服务注册和回写 +public class NettyServerHandler21 extends ChannelInboundHandlerAdapter { + private String methodName; + + //要传入对应的方法名 否则不知道 netty服务器能执行什么方法 + public NettyServerHandler21(String methodName) + { + this.methodName = methodName; + } + + //实现对应的方法 + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + System.out.println("收到来自"+ctx.channel().remoteAddress()+"的信息"); + //使用反射的方法获取对应的类 通过反射再进行执行 + Class calledClass = Class.forName("provider.api."+methodName + "ServiceImpl"); + Method method = calledClass.getMethod("say" + methodName, String.class); + Object instance = calledClass.newInstance(); + Object response = method.invoke(instance, msg.toString()); + + //获得对应信息并进行回传 + ctx.writeAndFlush(response); + } + + //出现异常的话 如何进行处理 + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + ctx.channel().close(); + cause.printStackTrace(); + } +} diff --git a/zyt-rpc-provider/src/main/java/provider/test/NacosTest.java b/zyt-rpc-provider/src/main/java/provider/test/NacosTest.java index c969cee..49fe588 100644 --- a/zyt-rpc-provider/src/main/java/provider/test/NacosTest.java +++ b/zyt-rpc-provider/src/main/java/provider/test/NacosTest.java @@ -20,6 +20,5 @@ public void registryTest() throws NacosException { namingService.registerInstance("nacos.test.3", "11.11.11.11", 8888, "DEFAULT"); namingService.registerInstance("nacos.test.3", "2.2.2.2", 9999, "DEFAULT"); System.out.println(namingService.getAllInstances("nacos.test.3")); - } } diff --git a/zyt-rpc-provider/src/main/java/provider/utils/MethodRegister.java b/zyt-rpc-provider/src/main/java/provider/utils/MethodRegister.java index ce1994b..dd5e89c 100644 --- a/zyt-rpc-provider/src/main/java/provider/utils/MethodRegister.java +++ b/zyt-rpc-provider/src/main/java/provider/utils/MethodRegister.java @@ -1,18 +1,16 @@ package provider.utils; import annotation.RegistryChosen; -import com.alibaba.nacos.api.exception.NacosException; import exception.RpcException; -import org.apache.zookeeper.KeeperException; -import provider.bootstrap.nio.NIOProviderBootStrap; import provider.service_registry.NacosServiceRegistry; import provider.service_registry.ZkCuratorRegistry; import provider.service_registry.ZkServiceRegistry; +import register.Register; + -import java.io.IOException; //直接实现启动类根据启动类接口上的注解选择对应需要选取的方法 -public class MethodRegister implements NIOProviderBootStrap { +public class MethodRegister { /** * 实际进行注册的方法 * @param method 方法名字 @@ -21,7 +19,7 @@ public class MethodRegister implements NIOProviderBootStrap { */ public static void register(String method, String ip, int port) throws Exception { - RegistryChosen annotation = MethodRegister.class.getInterfaces()[0].getAnnotation(RegistryChosen.class); + RegistryChosen annotation = Register.class.getAnnotation(RegistryChosen.class); switch (annotation.registryName()) { diff --git "a/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" "b/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" index bdf78a0..efc3fc5 100644 Binary files "a/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" and "b/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" differ