-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5.5 v2.1 Netty实现了全新的netty传输信息,使用了代理模式,异步调用的方式,线程的一些锁的方式,还有,同时兼容了之前的注册…
…中心,负载均衡方法。
- Loading branch information
1 parent
39c5bd8
commit 57af6d3
Showing
38 changed files
with
641 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
zyt-rpc-call/src/main/java/service/call/ChosenClientCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
zyt-rpc-call/src/main/java/service/call/ChosenServerCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,我直接采纳"); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
zyt-rpc-call/src/main/java/service/call/netty_call/NettyClientCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
9 changes: 6 additions & 3 deletions
9
...a/service/netty_call/NettyServerCall.java → ...vice/call/netty_call/NettyServerCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
zyt-rpc-call/src/main/java/service/call/nio_call/NIOClientCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
.../java/service/nio_call/NIOServerCall.java → .../service/call/nio_call/NIOServerCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 26 additions & 3 deletions
29
zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyClientBootStrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("出现问题"); | ||
} | ||
} | ||
} |
51 changes: 49 additions & 2 deletions
51
zyt-rpc-call/src/main/java/service/netty_bootstrap/NettyServerBootStrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
zyt-rpc-call/src/main/java/service/netty_call/NettyClientCall.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
zyt-rpc-call/src/main/java/service/nio_call/NIOClientCall.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
zyt-rpc-common/src/main/java/annotation/RpcSerializationSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
zyt-rpc-common/src/main/java/annotation/RpcToolsSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
5 changes: 3 additions & 2 deletions
5
...r/bootstrap/nio/NIOProviderBootStrap.java → ...mmon/src/main/java/register/Register.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
Oops, something went wrong.