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

上次我们介绍了sofaboot:

https://blog.youkuaiyun.com/csdnerM/article/details/124750460

这次我们来介绍SOFAStack家族中的SOFARPC
在这里插入图片描述
首先我们知道了sofaboot是在Spring Boot 的基础上,提供了诸如
Readiness Check,类隔离,日志空间隔离等等能力。

那么,sofarpc又是什么呢?

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

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

https://gitee.com/sofastack/sofa-rpc

sofarpc的原理是什么?

在这里插入图片描述

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

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

我们来开始他的快速使用

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

https://gitee.com/WangFuGui-Ma/sofastack-quickstart

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

https://blog.youkuaiyun.com/csdnerM/article/details/124750460

第一步,引入 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的快速启动就已经成功完成了!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

掉头发的王富贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值