BootNettyRpc
README: English https://github.com/forezp/BootNettyRpc/blob/master/README-en.md | 中文 https://github.com/forezp/BootNettyRpc/blob/master/README.md
什么是 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 {
- @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")
- @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);
- }
- @Autowired
- IGreeting greeting;
- Object object = greeting.sayHello( "hi" );
联系我
如果有任何问题和建议, 请联系我, 我的邮箱 mailto:miles02@163.com
已经实现的功能
rpc(实现同步, 异步调用)
负载均衡
接口线程池隔离
接入 Eureka
接入链路追踪
接入监控
接入报警邮箱
优化 rpc 性能 (需持续优化)
未来计划
接入多种序列化, 做到可配置
自定义协议, trace 的 index 不需要指定
接入 consule
支持 spring boot 2.0 Spring Cloud F
来源: http://www.tuicool.com/articles/Unai63F