BootNettyRpc:采用 Netty 实现的 RPC 框架

BootNettyRpc是一个适用于SpringBoot项目的RPC框架,基于Netty实现,并支持SpringCloud。本文档详细介绍了如何在本地和结合SpringCloud环境中部署及使用BootNettyRpc,包括配置依赖、启动Server端和Client端等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BootNettyRpc:采用 Netty 实现的 RPC 框架
什么是 BootNettyRpc?
BootNettyRpc 是一个采用Netty实现的Rpc框架,适用于Spring Boot项目,支持Spring Cloud。 目前支持的版本为Spring Boot 1.5.x,Spring Cloud版本为D和E版本。

怎么使用?
分为本地启动和结合Spring Cloud启动。具体见example 案例,现在以本地案例来说明,Spring Cloud案例省略。

BootNettyRpc 包括Server端和Client端。

Server端
在pom文件中加上依赖:

<dependency>
<groupId>io.github.forezp</groupId>
<artifactId>boot-netty-rpc-core</artifactId>
<version>1.0.5</version>
</dependency>
在Spring Boot启动工程加上注解@EnableNettyRpc,需要开启包扫描,不填也可以,会全局扫描,有一点影响启动速度,比如例子中的ExampleRpcServerApplication:

@SpringBootApplication
@EnableNettyRpc(basePackages = "com.forezp")
public class ExampleRpcServerApplication {

public static void main(String[] args) {
    SpringApplication.run( ExampleRpcServerApplication.class, args );
}

}
在配置文件配置Netty Server的端口和Netty Server的name,该name会作client端的调用的name。

server:
port: 7001

netty.server.name: server
netty.server.port: 6001
写一个服务,接口如下:

public interface IGreeting {

String sayHello(String name);

}
实现类如下:

@Service
public class Greeting implements IGreeting {br/>@Override
public String sayHello(String name) {
return "hi "+name;
}
}
Client端
在工程的pom 文件加上一下依赖:

<dependency>
<groupId>io.github.forezp</groupId>
<artifactId>boot-netty-rpc-core</artifactId>
<version>1.0.5</version>
</dependency>
在SpringBoot的启动类加上@EnableNettyRpc注解,如下:

@SpringBootApplication
@EnableNettyRpc(basePackages = "com.forezp")br/>@RestController
public class ExampleRpcClientApplication {

public static void main(String[] args) {
    SpringApplication.run( ExampleRpcClientApplication.class, args );
}

}
在Spring Boot配置文件 application.yml,加上以下的配置,其中name为Server端的name,同一个name可以配置多个服务实例,默认使用了轮询的负载均衡。

netty:
clients:

  • name: server
    host: localhost
    port: 6001
  • name: server
    host: localhost
    port: 6001
    服务调用者需要需要写一个接口,在接口上写@RpcClient注解,name必填为服务提供者名,rpcClz必填,为服务提供者的类。

@RpcClient(name = "server", rpcClz = "com.forezp.localrpcserver.api.Greeting")
public interface IGreeting {

String sayHello(String name);

}br/>@Autowired
IGreeting greeting;

Object object = greeting.sayHello( "hi" );
联系我
如果有任何问题和建议,请联系我,我的邮箱 miles02@163.com

已经实现的功能
rpc(实现同步、异步调用)
负载均衡
接口线程池隔离
接入Eureka
接入链路追踪
接入监控
接入报警邮箱
优化rpc性能 (需持续优化)
未来计划
接入多种序列化,做到可配置
自定义协议,trace的index不需要指定
接入consule
支持spring boot 2.0 Spring Cloud F

PS E:\tool\my-desktop-app> npm install electron robotjs npm warn deprecated npmlog@4.1.2: This package is no longer supported. npm warn deprecated are-we-there-yet@1.1.7: This package is no longer supported. npm warn deprecated boolean@3.2.0: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info. npm warn deprecated gauge@2.7.4: This package is no longer supported. npm warn cleanup Failed to remove some directories [ npm warn cleanup [ npm warn cleanup 'E:\\tool\\my-desktop-app\\node_modules', npm warn cleanup [Error: EPERM: operation not permitted, rmdir 'E:\tool\my-desktop-app\node_modules\bl\node_modules\readable-stream'] { npm warn cleanup errno: -4048, npm warn cleanup code: 'EPERM', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: 'E:\\tool\\my-desktop-app\\node_modules\\bl\\node_modules\\readable-stream' npm warn cleanup } npm warn cleanup ] npm warn cleanup ] npm error code 1 npm error path E:\tool\my-desktop-app\node_modules\robotjs npm error command failed npm error command C:\WINDOWS\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild npm error gyp info it worked if it ends with ok npm error gyp info using node-gyp@10.1.0 npm error gyp info using node@18.20.7 | win32 | x64 npm error gyp info find Python using Python version 3.10.0 found at "C:\Users\lijian\AppData\Local\Programs\Python\Python310\python.exe" npm error gyp ERR! find VS npm error gyp ERR! find VS msvs_version not set from command line or npm config npm error gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt npm error gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details. npm error gyp ERR! find VS npm error gyp ERR! find VS Failure details: undefined npm error gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details.
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值