亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

一、概述

手動實現一款輕量,高效的RPC框架,基于TCP的二進制協議實現

github源碼:
https://github.com/wosn00/srpc

二、特征

  • 基于netty的主從Reactor模型,NIO通信
  • 支持同步,異步,攜帶回調等調用方式
  • 支持spring項目下引入starter包開箱即用,整合spring,實現服務接口透明使用
  • 支持非spring項目下單獨使用,可不依賴spring環境
  • 支持多種序列化類型,Protostuff,Kryo,Json,Jdk等
  • 支持多種壓縮算法,SnAppy,Lz4,gzip,bzip2,Deflate,Lzo等
  • 支持注冊中心,自動服務注冊和發現,默認實現zookpeer,也可不使用注冊中心,手動指定服務端節點地址列表
  • 支持多種負載均衡策略,隨機,輪詢,一致性hash等
  • 支持服務容錯,連接/調用異常情況下自動排除服務端故障節點
  • 支持SPI擴展點,可擴展負載均衡策略,壓縮算法,序列化類型,線程池,注冊中心等
  • 支持TLS雙向認證加密
  • 支持流量整形,請求異常重試,服務端請求去重等功能

三、設計

手動實現一款輕量 高效的分布式RPC框架

 

可能對RPC框架性能產生影響的幾個因素:

  • 網絡IO線程模型
  • 通信協議設計
  • 序列化性能
  • 服務調用管理方式
  • 連接池的維護

3.1 RPC協議

手動實現一款輕量 高效的分布式RPC框架

 

協議上設計盡量緊湊,4位bit用于標識序列化類型,壓縮類型和指令類型,方法映射上不需要像dubbo那樣傳輸完整的類 方法 參數信息導致的無用流量增大,而是自行生成對應rpc調用方法的唯一標識字符串

3.2 同步線程模型

手動實現一款輕量 高效的分布式RPC框架

 

3.3 異步線程模型

手動實現一款輕量 高效的分布式RPC框架

 


 

四、使用示例

4.1、spring環境下

maven依賴

<dependency>
    <groupId>com.hex</groupId>
    <artifactId>srpc-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

若需使用zookeeper作為注冊中心則引入

<dependency>
    <groupId>com.hex</groupId>
    <artifactId>srpc-registry-zookeeper</artifactId>
    <version>1.1.0</version>
</dependency>

server端使用

1.定義服務接口

@SRpcClient(serviceName = "testService")
public interface HelloService {
    
    String hello(String name);
}

接口添加@SRpcClient注解,serviceName屬性為rpc服務都在注冊中心的服務名稱,若不使用注冊中心,則注解nodes屬性需手動指定服務端節點集群地址,將根據負載均衡策略自動選取節點調用

@SRpcClient(nodes = {"127.0.0.1:9955","127.0.0.1:9956","127.0.0.1:9957"})
public interface HelloService {
    
    String hello(String name);
}

2.服務接口實現

@SRpcRoute
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return name + " Hey bro, it's a good day";
    }
}

實現類添加@SRpcRoute注解,便會自動注冊為spring的單例bean,可視為等同@Comphonent使用,內部可用@Autowired等spring相關注解,也可被其他bean注入。

3.配置yml

因同時包含了rpc客戶端和服務端,所以客戶端和服務端都需要配置,如需個性化配置的地方在yml或properties文件按需配置即可,以srpc.server或srpc.client為前綴。所有可自由配置的選項如下

服務端默認配置:

@ConfigurationProperties(prefix = "srpc.server")
public class RpcServerProperties {
    private Integer port = 9957; //綁定端口
    private Integer businessThreads = 200; //業務處理線程池大小,0為不設置
    private Integer businessQueueSize = 500; //業務線程池隊列大小
    private Integer connectionIdleTime = 180;//超過連接空閑時間(秒)未收發數據則關閉連接
    private Integer printConnectionNumInterval = 0; //打印服務端當前連接詳情, 時間間隔(秒), 0為不打印
    private Boolean isPrintHearBeatPacketInfo = false; //是否打印心跳包信息

    private CompressType compressType = CompressType.SNAPPY; //壓縮算法類型,無需壓縮為NONE
    private SerializeType serializeType = SerializeType.PROTOSTUFF; //序列化類型,默認protostuff

    private Integer sendBuf = 65535; //tcp發送緩沖區
    private Integer receiveBuf = 65535; //tcp接收緩沖區
    private Integer lowWaterLevel = 1024 * 1024; //netty低水位
    private Integer highWaterLevel = 10 * 1024 * 1024; //netty高水位

    private boolean deDuplicateEnable = false; //是否開啟去重處理
    private Integer duplicateCheckTime = 10; //請求去重緩存時長(秒)
    private Long duplicateMaxSize = 1024 * 64L; //最大緩存請求個數

    private Boolean trafficMonitorEnable = false; //是否開啟流控
    private Long maxReadSpeed = 10 * 1000 * 1000L; //帶寬限制,最大讀取速度
    private Long maxWriteSpeed = 10 * 1000 * 1000L; //帶寬限制,最大寫出速度
    
    // ----tls加密部分配置
    private Boolean useTLS = false; //是否開啟tls加密
    private String keyPath; //私鑰文件路徑
    private String keyPwd; //密碼
    private String certPath; //證書文件路徑
    private String trustCertPath; //受信任ca證書路徑
    private String clientAuth; //是否要求客戶端認證
    // ----注冊中心配置部分
    private Boolean enableRegistry = false; //是否使用注冊中心
    private String registrySchema; //注冊中心模式名稱
    private List<String> registryAddress; //注冊中心地址

客戶端默認配置:

@ConfigurationProperties(prefix = "srpc.client")
public class RpcClientProperties {
    private Integer callBackTaskThreads = 200; //回調任務處理線程池大小,0為不設置
    private Integer callBackTaskQueueSize = 500; //回調任務線程池隊列大小
    private Integer connectionTimeout = 5; //連接超時時間(秒)
    private Integer requestTimeout = 10; //請求超時時間(秒)
    private Integer connectionSizePerNode = 3; //每個節點連接數
    private Integer connectionIdleTime = 180; //超過連接空閑時間(秒)未收發數據則關閉連接
    private Integer heartBeatTimeInterval = 30; //發送心跳包間隔時間(秒)

    private CompressType compressType = CompressType.SNAPPY; //壓縮算法類型,無需壓縮為NONE
    private SerializeType serializeType = SerializeType.PROTOSTUFF; //序列化類型,默認protostuff

    private LoadBalanceRule loadBalanceRule = LoadBalanceRule.RANDOM; //集群負載均衡策略
    private boolean excludeUnAvailableNodesEnable = true; //集群模式下是否排除不可用的節點
    private Integer nodeErrorTimes = 3; //節點連接或請求超時/異常超過設置次數則置為節點不可用
    private Integer nodeHealthCheckTimeInterval = 10; //節點健康檢查周期(秒),心跳包響應成功則恢復不可用的節點
    
    private Integer sendBuf = 65535; //tcp發送緩沖區
    private Integer receiveBuf = 65535; //tcp接收緩沖區
    private Integer lowWaterLevel = 1024 * 1024; //netty低水位
    private Integer highWaterLevel = 10 * 1024 * 1024; //netty高水位

    private Boolean trafficMonitorEnable = false; //是否開啟流量控制
    private Long maxReadSpeed = 10 * 1000 * 1000L; //帶寬限制,最大讀取速度
    private Long maxWriteSpeed = 10 * 1000 * 1000L; //帶寬限制,最大寫出速度
    
    // ----TLS加密部分配置
    private Boolean useTLS = false; //是否開啟TLS加密
    private String keyPath; //私鑰文件路徑
    private String keyPwd; //密碼
    private String certPath; //證書文件路徑
    private String trustCertPath; //受信任ca證書路徑
    private String clientAuth; //是否要求客戶端認證
    // ----注冊中心配置部分
    private Boolean enableRegistry = false; //是否使用注冊中心
    private String registrySchema; //注冊中心模式名稱, 缺省為zookeeper
    private List<String> registryAddress; //注冊中心地址

配置類信息:

https://github.com/wosn00/srpc/blob/master/srpc-spring-boot-starter/src/main/JAVA/com/hex/rpc/spring/starter/properties/RpcClientProperties.java
4.服務端啟動

@SpringBootApplication
@EnableSRpc(basePackages = "com.hex.example.provider")
public class RpcTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(RpcTestApplication.class, args);
    }
}

啟動類上添加@EnableSRpc注解,basePackages為需要掃描的包路徑,包含@SRpcClient和@SRpcRoute注解的包路徑,相應的類都會被自動注冊為spring的單例bean,缺省為啟動類上級包路徑

client端使用

1.服務接口調用

@Component
public class HelloRpcTest {
    
    @Autowired
    private HelloService helloService; // 上面定義的rpc服務接口

    public void rpcServerTest(String name) {
        String msg = helloService.hello(name);
        System.out.println(msg);
    }
}

上述服務端定義的帶有@SRpcClient注解的rpc服務接口,使用spring的@Autowired注入即可遠程調用

2.配置yml(同上)

3.客戶端啟動(同上)

4.2、非spring環境下

maven依賴

<dependency>
    <groupId>com.hex</groupId>
    <artifactId>srpc-core</artifactId>
    <version>1.1.0</version>
</dependency>

若需使用zookeeper作為注冊中心則引入

<dependency>
    <groupId>com.hex</groupId>
    <artifactId>srpc-registry-zookeeper</artifactId>
    <version>1.1.0</version>
</dependency>

server端使用

1.定義服務接口實現

@SRpcRoute
public class HelloServiceImpl {
    @Mapping("hello")
    public String hello(String name) {
        return name + " Hey bro, it's a good day";
    }
}

2.服務端啟動

@SRpcScan("com.hex.example")
public class ServerTest {
    public static void main(String[] args) {
        // 啟動服務端, 需填入rpc服務端配置, 可使用默認配置, source填寫有@RouteScan注解的類
        SRpcServer.builder()
                .serverConfig(new SRpcServerConfig()) //包含rpc服務端的各項默認配置,可自行修改
                .sourceClass(ServerTest.class) //有@RouteScan注解的類
                .port(8005) //rpc服務端綁定的端口,默認9957
                .start();
    }
}

啟動類上添加@SRpcScan注解,值需填寫包含@SRpcRoute注解的類的包路徑,缺省為啟動類的上級包路徑,即可自動掃描

client端使用

1.客戶端啟動和服務接口調用

public class ClientTest {
    public static void main(String[] args1) {
        // 初始化客戶端,需填入rpc客戶端配置,可使用默認配置
        Client rpcClient = SRpcClient.builder()
                .config(new SRpcClientConfig())
                .start();

        Object[] args = {"Jack"};
        HostAndPort node = HostAndPort.from("127.0.0.1:8005");
        // 同步發送請求,獲取響應
        String response = rpcClient.invoke("hello", String.class, args, node);
        System.out.println(response);

        // 異步發送請求,發送完成即返回,不阻塞等待響應結果
        rpcClient.invokeAsync("hello",
                rpcResponse -> System.out.println("收到響應,開始執行回調方法" + rpcResponse), args, node);
    }
}

Client更多調用接口及參數可查看接口說明:

https://github.com/wosn00/srpc/blob/master/srpc-core/src/main/java/com/hex/srpc/core/rpc/Client.java

五、性能測試

5.1 與dubbo的性能對比測試
目前只是與dubbo進行了簡單了的性能測試對比 0_0,后續有時間會進行更多的測試

測試代碼:
https://github.com/wosn00/THOC/blob/master/srpc-demo-provider/src/main/java/com/hex/srpc/SRpcProviderApplication.java

條件:

1.測試相同接口模擬業務處理延遲30ms后返回

2.服務端業務處理線程池均為500

3.dubbo采用默認的dubbo協議,srpc使用protostuff序列化

手動實現一款輕量 高效的分布式RPC框架

 

分享到:
標簽:框架 RPC
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定