手动实现一款轻量 高效的分布式RPC框架( 二 )

客户端默认配置:
@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; //压缩算法类型,无需压缩为NONEprivate SerializeType serializeType = SerializeType.PROTOSTUFF; //序列化类型,默认protostuffprivate 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; //注册中心模式名称, 缺省为zookeeperprivate 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.服务接口调用@Componentpublic class HelloRpcTest {@Autowiredprivate 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.定义服务接口实现
@SRpcRoutepublic 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);}}


推荐阅读