# simple-rpc
**Repository Path**: dqy000/simple-rpc
## Basic Information
- **Project Name**: simple-rpc
- **Description**: 一个基于netty的RPC程式
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 0
- **Created**: 2021-10-27
- **Last Updated**: 2021-12-06
## Categories & Tags
**Categories**: Uncategorized
**Tags**: Java, rpc
## README

## SimpleRpc介绍
这是一个极简风格的远程调用类库,并且带有一个简单的注册中心,支持服务断线重连,注册中心负载均衡(注册中心存在多个一样的服务提供方时根据配置来选择调用策略),还支持基本的点对点调用。
## 组件介绍
>## core
>核心模块,包含了服务端,客户端通信,基本类库,工具类
>
>## common
>公共模块,包含通用编解码器,通用类
>
>## center
>注册中心服务,提供服务注册,发现功能,掉线重连,以及软负载均衡功能
>
>## spring-boot-starter-simple-rpc
>服务端,客户端 spring boot插件,开箱即用
>
>## spring-boot-starter-simple-rpc-center
>注册中心的spring boot插件
## 架构图

## 分支介绍
>## LV1
> 最初版本分支,只支持点对点调用,无注册中心(不推荐!)
>
>## master
>最新分支,目前具备注册中心,掉线重连,调用负载均很策略支持以及LV1分支的全部功能
## 快速开始(基于SpringBoot Starter快速开始)
### 启动注册中心
1.新建spring boot项目,引入注册中心插件POM:
```xml
org.example
spring-boot-starter-simple-rpc-center
1.0-SNAPSHOT
```
2.启动类如下:
```java
@EnableCenterServer //开启注册中心
@SpringBootApplication
public class CenterApplication {
public static void main(String[] args) {
SpringApplication.run(CenterApplication.class);
}
}
```
3.yaml配置如下:
```yaml
server:
port: 12345
simple-rpc-center:
worker-thread-pool-size: 10 #工作线程池大小
port: 33333 #注册中心服务端口
lb-strategy: "polling" #注册中心负载均衡策略,目前支持2种:随机,轮询
```
4.启动注册中心项目,启动成功如下所示:

注册中心服务就启动完成了,接下来准备RPC服务提供方和调用方。
### 创建RPC服务提供方
1.新建spring boot项目,引入SimpleRpc插件POM:
```xml
org.example
spring-boot-starter-simple-rpc
1.0-SNAPSHOT
```
2.RPC服务提供方的启动类如下:
```java
@Configuration
@SpringBootApplication
@EnableSimpleRpcServer //开启RPC服务端
public class ApplicationMain1{
public static void main(String[] args) {
SpringApplication.run(ApplicationMain1.class);
}
}
```
3.编写一个简单的Service:
```java
@Service
@RemoteClass //表示需要暴露的服务类
public class TestServiceImpl implements TestService {
@Autowired
AppConfig appConfig;
@RemoteFunction //需要暴露的服务具体方法
@Override
public Std find() {
//注意:Std类请实现java序列化接口 由于服务调用方,服务提供方都需要使用到该类,所以请自己以公共包的形式放入自己项目
return new Std(“111111”,1);
}
}
```
4.yaml配置如下:
```yaml
server:
port: 7788
simple-rpc:
port: 11111 #RPC服务本地监听端口
scan-pack: com.app1 #注解扫描根目录
reconnection-time: 10000 #如果断线,自动重连间隔
enabled-reconnection: true #开启断线自动重连
enabled-spring-support: true #开启spring支持
center-addr: "192.168.10.101" #注册中心地址
center-port: 33333 #注册中心端口
```
5.启动服务提供方,使其向注册中心注册服务,以供调用方调用,启动成功如下:

### 创建RPC服务消费方
1.同样的创建Spring Boot项目,引入SimpleRpc插件POM,同上服务提供方步骤一样
```xml
```
2.编写服务消费方的启动类,如下:
```java
@SpringBootApplication
@EnableSimpleRpcClient //开启RPC客户端
public class ApplicationMain2 {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain2.class);
}
}
```
3.yaml配置如下(含义参考服务提供方):
```yaml
server:
port: 9999
simple-rpc:
scan-pack: com.app2
enabled-reconnection: true
reconnection-time: 5555
enabled-spring-support: true
center-addr: "192.168.10.102"
center-port: 33333
```
4.编写远程调用接口:
```java
@Caller //表示一个远程调用的服务接口
public interface TestService {
public Std find();
}
```
5.编写Control测试
```java
@RequestMapping("/app2")
@RestController
public class TestControl {
TestService testService=ProxyFactory.getProxy(TestService.class,null); //手动为该接口生成代理
@RequestMapping(value = "/test",method = RequestMethod.GET)
public Std test(){
return testService.find();
}
}
```
6.启动服务消费方项目,PostMan访问服务消费方Control

调用成功!
### 启用多个服务提供方实列
1.重复上述服务提供方创建步骤,创建多个服务提供方,在注册中心yaml中配置负载均衡策略为轮询:
```yaml
lb-strategy: "polling" #注册中心负载均衡策略,目前支持2种:随机,轮询
```
2.启用3个服务提供方,PostMan调用多次,分别返回:
 
可见多次调用返回总是111111,222222,333333,111111,222222,333333....循环
关闭其中一个服务提供方(333333),注册中心感知到服务3下线,日志输出:
```java
[nioEventLoopGroup-2-4] INFO server.support.ServerInfoMap - 192.168.10.102:55897 机器已经与注册中心断开连接...
```
再次多次调用:只会得到111111,222222循环的返回了。