最近很火的SOFARPC是什么?带你快速入门SOFARPC

240 阅读2分钟

上次我们介绍了sofaboot:

sofaboot

在这里插入图片描述

这次我们来介绍SOFAStack家族中的SOFARPC

首先我们知道了sofaboot是在Spring Boot 的基础上,提供了诸如 Readiness Check,类隔离,日志空间隔离等等能力。

那么,sofarpc又是什么呢?

SOFARPC 是蚂蚁金服开源的一款基于 Java 实现的 RPC 服务框架,为应用之间提供远程服务调用能力,SOFARPC 是一个高可扩展性、高性能、生产级的 Java RPC 框架。

在这里插入图片描述 下面是它的开源仓库地址:

gitee.com/sofastack/s…

sofarpc的原理是什么?

在这里插入图片描述

1.当一个 SOFARPC 的应用启动的时候,如果发现当前应用需要发布 RPC 服务的话,那么 SOFARPC 会将这些服务注册到服务注册中心上。如图中 Service 指向 Registry。 2.当引用这个服务的 SOFARPC 应用启动时,会从服务注册中心订阅到相应服务的元数据信息。服务注册中心收到订阅请求后,会将发布方的元数据列表实时推送给服务引用方。如图中 Registry 指向 Reference。 3.当服务引用方拿到地址以后,就可以从中选取地址发起调用了。如图中 Reference 指向 Service。

熟悉springcloud的同学是不是觉得它和eurekafegin之间的关系特别像?

我们来开始他的快速使用

大家也可以拉我仓库的代码下载起来直接启动:

gitee.com/WangFuGui-M…

我们是按照sofaboot的方式进行框架的快速启动 所以如果不知道sofaboot怎么启动的话请查看我的上篇文章sofaboot的快速入门

blog.csdn.net/csdnerM/art…

第一步,引入 RPC Starter:

        <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>rpc-sofa-boot-starter</artifactId>
        </dependency>
        

在这里插入图片描述 第二步,编写xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sofa="http://sofastack.io/schema/sofaboot"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://sofastack.io/schema/sofaboot   http://sofastack.io/schema/sofaboot.xsd"
       default-autowire="byName">

</beans>

在这里插入图片描述

第三步,定义服务接口与实现

public interface HelloSyncService {

    String saySync(String string);
}
public class HelloSyncServiceImpl implements HelloSyncService {

    @Override
    public String saySync(String string) {
        return string;
    }
}

在这里插入图片描述 在这里插入图片描述 第四步,服务端发布服务

在 xml 文件中编写如下配置。Spring 上下文在刷新时,SOFABoot 就将该服务实现注册到了服务器上,以 bolt 协议与客户端进行通信地址,并将地址等元数据发布到了注册中心(这里默认使用的本地文件作为注册中心)。


    <bean id="helloSyncServiceImpl" class="com.masiyi.sofaboot.HelloSyncServiceImpl"/>
    <sofa:service ref="helloSyncServiceImpl" interface="com.masiyi.sofaboot.HelloSyncService">
        <sofa:binding.bolt/>
    </sofa:service>

在这里插入图片描述 第四步,客户端引用服务

在 xml 文件中编写如下配置。Spring 上下文刷新时,SOFABoot 会生成一个RPC的代理 bean,即 personReferenceBolt 。这样就可以直接在代码中使用该 bean 进行远程调用了。

    <sofa:reference id="helloSyncServiceReference" interface="com.masiyi.sofaboot.HelloSyncService">
        <sofa:binding.bolt/>
    </sofa:reference>

在这里插入图片描述 第五步,运行

在 SpringBoot 的启动类中编码如下。其中利用 ImportResource 将上述的xml文件加载。


@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@SpringBootApplication
public class SofabootApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SofabootApplication.class);
        ApplicationContext applicationContext = springApplication.run(args);

        HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext
                .getBean("helloSyncServiceReference");

        System.out.println(helloSyncServiceReference.saySync("hello , 王富贵"));
    }

}

在这里插入图片描述 这样就可以在容器中拿到服务类并且进行访问了

控制台输入打印: 在这里插入图片描述 到此,一个sofarpc的快速启动就已经成功完成了!!