Open-Light-Rpc

轻量级,高性能rpc框架,客户端与服务端通信采用 Grpc-Netty 通信方式

🍪 快速开始

欢迎阅读 Open-Light-Rpc 文档!

如果你有任何与 Open-Light-Rpc 相关的问题,欢迎随时在 GitHub Discussions 上向我们的社区寻求帮助。

系统环境需求

构建客户端

1. 添加 Maven 依赖

<dependency>
    <groupId>com.saucesubfresh</groupId>
    <artifactId>open-rpc-client</artifactId>
    <version>1.3.0</version>
</dependency>

2. 在启动类上添加 @EnableOpenRpcClient 注解

@EnableOpenRpcClient
@SpringBootApplication
public class JobDashboardApplication {

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

3. 配置服务名称

com:
  saucesubfresh:
    rpc:
      client:
        server-name: open-job-services

4. 给服务端发送消息示例

节选自 Open-Job 代码片段,该代码仅为示例。

@Slf4j
@Component
public class OpenJobClientServiceImpl implements OpenJobClientService{

    private final ClusterInvoker clusterInvoker;

    public OpenJobClientServiceImpl(ClusterInvoker clusterInvoker) {
        this.clusterInvoker = clusterInvoker;
    }

    @Override
    public void invoke(Long jobId) {
        Message message = new Message();
        message.setMsgId(String.valueOf(jobId));
        message.setNamespace(serverName);

        try {
            clusterInvoker.invokeAsync(message, (response) -> {
                // TODO
            });
        } catch (RpcException ex){
            // TODO
        }
    }
}

构建服务端

1. 添加 Maven 依赖

<dependency>
    <groupId>com.saucesubfresh</groupId>
    <artifactId>open-rpc-server</artifactId>
    <version>1.3.0</version>
</dependency>

2. 在启动类上添加 @EnableOpenRpcServer 注解

@EnableOpenRpcServer
@SpringBootApplication
public class JobServerApplication {

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

3. 配置服务端地址和端口、服务名称

com:
  saucesubfresh:
    rpc:
      server:
        server-address: 127.0.0.1
        server-port: 5200
        server-name: open-job-services

4. 接收客户端发来的消息进行处理示例

节选自 Open-Job

@Slf4j
@Component
public class JobMessageProcessor extends AbstractMessageProcess {

    private final JobHandlerRegister jobHandlerRegister;
    private final JobThreadRepository jobThreadRepository;

    public JobMessageProcessor(RegistryService registryService,
                               ServerConfiguration configuration,
                               JobHandlerRegister jobHandlerRegister,
                               JobThreadRepository jobThreadRepository) {
        super(configuration, registryService);
        this.jobHandlerRegister = jobHandlerRegister;
        this.jobThreadRepository = jobThreadRepository;
    }

    @Override
    public void doProcess(Message message, MessageResponseBody responseBody, ResponseWriter responseWriter) {
        final byte[] body = message.getBody();
        final MessageBody messageBody = SerializationUtils.deserialize(body, MessageBody.class);
        CommandEnum command = CommandEnum.of(messageBody.getCommand());
        if (command == CommandEnum.SCHEDULE){
            handlerSchedule(messageBody, responseBody, responseWriter);
            return;
        }

        try {
            handlerMessage(messageBody, command, responseBody);
        } catch (Exception e){
            log.error(e.getMessage(), e);
            responseBody.setMsg(e.getMessage());
            responseBody.setStatus(ResponseStatus.ERROR);
        } finally {
            responseWriter.write(responseBody);
        }
    }
}

到目前为止,我们已经构建好了 open-light-rpc 的客户端和服务端。

注意点

系统默认客户端和服务端使用的注册中心是 Nacos,注意客户端与服务端使用的注册中心类型须一致,也就是说如果客户端使用 Nacos 作为注册中心,那服务端也需要使用 Nacos 作为注册中心。

如果使用 Nacos 作为注册中心

  1. 需要添加如下 maven 依赖, nacos-client 版本可以用最新的,这里是示例
<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>2.0.4</version>
</dependency>
  1. 需要注入 NamingService
@Configuration(proxyBeanMethods = false)
public class SpringWebMvcConfig {

    @Bean
    public NamingService namingService() throws NacosException {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.USERNAME, "nacos");
        properties.put(PropertyKeyConst.PASSWORD, "nacos");
        properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
        return NacosFactory.createNamingService(properties);
    }
}

如果使用 Zookeeper 作为注册中心

  1. 需要添加如下 maven 依赖
<dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.7.0</version>
    </dependency>

    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.11</version>
    </dependency>
</dependencies>
  1. 需要注入 ZkClient
@Configuration(proxyBeanMethods = false)
public class SpringWebMvcConfig {

    @Bean
    public ZkClient zkClient(){
        System.setProperty("zookeeper.sasl.client", "false");
        return new ZkClient(String.format(CommonConstant.ADDRESS_PATTERN, configuration.getAddress(), configuration.getPort()), configuration.getConnectionTimeout());
    }
}