forked from shaogezhu/easy-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.基于zookeeper作为注册中心进行了统一的访问接口封装与实现 2.统一将节点的更新后的相关操作通过事件的机制来实现代码解偶 3.将对于netty连接的管理操作统一封装在了ConnectionHandler类中
- Loading branch information
Showing
23 changed files
with
1,407 additions
and
39 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
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
103 changes: 103 additions & 0 deletions
103
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/ConnectionHandler.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,103 @@ | ||
package com.shaogezhu.easy.rpc.core.client; | ||
|
||
import com.shaogezhu.easy.rpc.core.common.ChannelFutureWrapper; | ||
import com.shaogezhu.easy.rpc.core.common.utils.CommonUtil; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import static com.shaogezhu.easy.rpc.core.common.cache.CommonClientCache.CONNECT_MAP; | ||
import static com.shaogezhu.easy.rpc.core.common.cache.CommonClientCache.SERVER_ADDRESS; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/2/28 | ||
* @description: 按照单一职责的设计原则,将与连接(建立、断开)有关的功能都统一封装在了一起. | ||
*/ | ||
public class ConnectionHandler { | ||
|
||
/** | ||
* 核心的连接处理器 | ||
* 专门用于负责和服务端构建连接通信 | ||
*/ | ||
private static Bootstrap bootstrap; | ||
|
||
public static void setBootstrap(Bootstrap bootstrap) { | ||
ConnectionHandler.bootstrap = bootstrap; | ||
} | ||
|
||
/** | ||
* 构建单个连接通道 元操作,既要处理连接,还要统一将连接进行内存存储管理 | ||
* | ||
* @param providerIp | ||
* @return | ||
* @throws InterruptedException | ||
*/ | ||
public static void connect(String providerServiceName, String providerIp) throws InterruptedException { | ||
if (bootstrap == null) { | ||
throw new RuntimeException("bootstrap can not be null"); | ||
} | ||
//格式错误类型的信息 | ||
if(!providerIp.contains(":")){ | ||
return; | ||
} | ||
String[] providerAddress = providerIp.split(":"); | ||
String ip = providerAddress[0]; | ||
int port = Integer.parseInt(providerAddress[1]); | ||
//到底这个channelFuture里面是什么 | ||
ChannelFuture channelFuture = bootstrap.connect(ip, port).sync(); | ||
ChannelFutureWrapper channelFutureWrapper = new ChannelFutureWrapper(); | ||
channelFutureWrapper.setChannelFuture(channelFuture); | ||
channelFutureWrapper.setHost(ip); | ||
channelFutureWrapper.setPort(port); | ||
SERVER_ADDRESS.add(providerIp); | ||
List<ChannelFutureWrapper> channelFutureWrappers = CONNECT_MAP.getOrDefault(providerServiceName, new ArrayList<>()); | ||
channelFutureWrappers.add(channelFutureWrapper); | ||
CONNECT_MAP.put(providerServiceName, channelFutureWrappers); | ||
} | ||
|
||
/** | ||
* 构建ChannelFuture | ||
* @param ip | ||
* @param port | ||
* @return | ||
* @throws InterruptedException | ||
*/ | ||
public static ChannelFuture createChannelFuture(String ip,Integer port) throws InterruptedException { | ||
return bootstrap.connect(ip, port).sync(); | ||
} | ||
|
||
/** | ||
* 断开连接 | ||
* | ||
* @param providerServiceName | ||
* @param providerIp | ||
*/ | ||
public static void disConnect(String providerServiceName, String providerIp) { | ||
SERVER_ADDRESS.remove(providerIp); | ||
List<ChannelFutureWrapper> channelFutureWrappers = CONNECT_MAP.get(providerServiceName); | ||
if (CommonUtil.isNotEmptyList(channelFutureWrappers)) { | ||
channelFutureWrappers.removeIf(channelFutureWrapper -> | ||
providerIp.equals(channelFutureWrapper.getHost() + ":" + channelFutureWrapper.getPort())); | ||
} | ||
} | ||
|
||
/** | ||
* 默认走随机策略获取ChannelFuture | ||
* | ||
* @param providerServiceName | ||
* @return | ||
*/ | ||
public static ChannelFuture getChannelFuture(String providerServiceName) { | ||
List<ChannelFutureWrapper> channelFutureWrappers = CONNECT_MAP.get(providerServiceName); | ||
if (CommonUtil.isEmptyList(channelFutureWrappers)) { | ||
throw new RuntimeException("no provider exist for " + providerServiceName); | ||
} | ||
return channelFutureWrappers.get(new Random().nextInt(channelFutureWrappers.size())).getChannelFuture(); | ||
} | ||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/ChannelFutureWrapper.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,42 @@ | ||
package com.shaogezhu.easy.rpc.core.common; | ||
|
||
import io.netty.channel.ChannelFuture; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/2/27 | ||
* @description: 自定义包装类,将netty建立好的ChannelFuture做了一些封装 | ||
*/ | ||
public class ChannelFutureWrapper { | ||
|
||
private String host; | ||
|
||
private Integer port; | ||
|
||
private ChannelFuture channelFuture; | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public Integer getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(Integer port) { | ||
this.port = port; | ||
} | ||
|
||
public ChannelFuture getChannelFuture() { | ||
return channelFuture; | ||
} | ||
|
||
public void setChannelFuture(ChannelFuture channelFuture) { | ||
this.channelFuture = channelFuture; | ||
} | ||
|
||
} |
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
Oops, something went wrong.