前言

最近蚂蚁金服开源了分布式框架 SOFA,楼主写了一个 demo,体验了一下 SOFA 的功能,SOFA 完全兼容 SpringBoot(当然 Dubbo 也是可以兼容的)。

项目地址:Alipay,该主页有 5 个项目,都是阿里开源的。

sofa-boot

sofa-rpc

sofa-bolt

sofa-ark

sofa-rpc-boot-projects

快速开始

实际上,SOFA-RPC 的官方文档已经详细介绍了如何使用这个 RPC 框架,基于 Netty 的长连接。类似 Dubbo。楼主看这个框架主要是为了学习分布式 RPC 框架的设计。

由于测试例子需要两个项目,我们建一个目录,目录下创建两个 maven module(SpringBoot 项目即可):

一个生产者,一个消费者。

将这两个项目的 pom.xml 中 springBoot 的 parent 标签换成如下:

  <parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>2.3.1</version>
</parent>

再增加一个依赖:

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

到这里,关于 RPC 框架的依赖和搭建就好了,是不是很简单?

接口创建

既然是 RPC 服务,那就需要一个接口,再有一个实现类。我们在提供方这里创建。

public interface HelloSyncService {
String saySync(String string);
} // 实现类
public class HelloSyncServiceImpl implements HelloSyncService { @Override
public String saySync(String string) {
return "provider tell you : this is your say: " + string;
}
}

然后在消费方的 pom.xml 添加对这个接口的依赖。

<dependency>
<groupId>cn.think.in.java</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

有了接口,就需要配置一下。

接口配置

首先在提供方这里发布接口。创建一个 xml 文件,名为:rpc-sofa-boot-starter-samples.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"
xmlns:context="http://www.springframework.org/schema/context"
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"> <bean id="helloSyncServiceImpl" class="cn.think.in.java.provider.HelloSyncServiceImpl"/>
<sofa:service ref="helloSyncServiceImpl" interface="cn.think.in.java.provider.HelloSyncService">
<sofa:binding.bolt/>
</sofa:service>
</beans>

很简单,发布了一个接口,类似 Spring 的一个 bean。

同时这个接口的协议是 bolt,也就是阿里的 RPC 网络通信框架 solt(基于 Netty 的最佳实践)。

同样的,在消费者的 resource 文件下,也创建一个同名文件。内容稍有不同。

<?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"
xmlns:context="http://www.springframework.org/schema/context"
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"> <sofa:reference id="helloSyncServiceReference" interface="cn.think.in.java.provider.HelloSyncService">
<sofa:binding.bolt/>
</sofa:reference>
</beans>

通过接口得到一个 bean。

好,接口的配置好了,那么就可以启动测试了。

准备测试

测试之前还要做点点工作。

在提供者配置文件 appcation.perproties 中,配置一下端口和程序名称。

# server.port=8080 # 默认
spring.application.name=provider

默认 8080 端口,就不必配置了。

然后,在消费者那里同样配置这个文件。内容如下:

spring.application.name=consumer
server.port=8081

消费者和提供者端口不能冲突。

还剩最后一步。

将文件引入到 Spring 容器中。

在提供者启动类上加入以下内容(引入配置文件):

@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@SpringBootApplication
public class ProviderApplication { public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}

在消费者启动类中引入以下内容:

@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@SpringBootApplication
public class ConsumerApplication { public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(ConsumerApplication.class);
ApplicationContext applicationContext = springApplication.run(args); HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext
.getBean("helloSyncServiceReference"); System.out.println(helloSyncServiceReference.saySync("sync"));
}
}

稍微多点东西,但也还是挺简单的。

首先创建一个 Spring 启动类。然后运行,从 Spirng 容器中获取到 bean(被动态代理封装的远程调用)。然后调用代理方法。

运行

先运行提供者:

2018-04-23 23:18:24.776  INFO 26654 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2018-04-23 23:18:24.776 INFO 26654 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-04-23 23:18:24.886 INFO 26654 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-23 23:18:24.893 INFO 26654 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
Sofa-Middleware-Log SLF4J : Actual binding is of type [ com.alipay.remoting Logback ]
2018-04-23 23:18:24.966 INFO 26654 --- [ main] com.alipay.sofa.common.log : Sofa-Middleware-Log SLF4J : Actual binding is of type [ com.alipay.remoting Logback ]
2018-04-23 23:18:25.174 INFO 26654 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-04-23 23:18:25.179 INFO 26654 --- [ main] c.t.i.java.provider.ProviderApplication : Started ProviderApplication in 3.352 seconds (JVM running for 3.978)

再运行消费者:

2018-04-23 23:19:21.940  INFO 26673 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2018-04-23 23:19:22.055 INFO 26673 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-23 23:19:22.063 INFO 26673 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2018-04-23 23:19:22.319 INFO 26673 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2018-04-23 23:19:22.324 INFO 26673 --- [ main] c.t.i.java.consumer.ConsumerApplication : Started ConsumerApplication in 3.898 seconds (JVM running for 4.524)
provider tell you : this is your say: sync

成功打印结果。

总结

首先第一感觉是,这个框架还是挺好用,挺简单的,基于当前的 SpringBoot 。快速启动。而且不是 SpringCloud 的 Http 调用,使用 Netty 作为网络通信框架,性能当然是没有问题的。

当然,我们这里的 demo 使用的注册中心没有使用 ZK,毕竟初体验嘛,使用的本地的文件。

然而,楼主对这个框架有很大的兴趣。接下来的空闲时间里,楼主将好好研究 SOFA 相关的代码。

蚂蚁 RPC 框架 SOFA-RPC 初体验的更多相关文章

  1. Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验

    Net Core平台灵活简单的日志记录框架NLog+SqlServer初体验 前几天分享的"[Net Core平台灵活简单的日志记录框架NLog+Mysql组合初体验][http://www ...

  2. C# -- 高性能RPC框架:Socean.RPC

    简介 Socean.RPC是一个.Net下的高性能RPC框架,框架以高性能.高稳定性为目标,底层基于socket,无第三方库引用,代码简洁,总代码量大约在2000行,框架性能较高,在普通PC上测试,长 ...

  3. 开源自己实现一个.net rpc框架 - Machete.Rpc

    Machete.Rpc Machete.Rpc 是一个轻量级的Rpc(远程过程调用的)框架.底层代理使用了Emit提高了效率,底层通信采用DotNetty框架以提升通信的效率.目前正在逐步完善中. G ...

  4. 腾讯微服务框架Tars的初体验

    最近研究了一下腾讯的微服务体系开发框架. 官方的搭建过程:https://github.com/TarsCloud/Tars/blob/master/Install.zh.md 自己填的坑: 不得不说 ...

  5. mui框架移动开发初体验

      前  言 博主最近在接触移动APP,学习了几个小技巧,和大家分享一下. 1. 状态栏设置 现在打开绝大多数APP,状态栏都是与APP一体,不仅美观,而且与整体协调.博主是个中度强迫症患者,顶部那个 ...

  6. RPC框架实现

    转载RPC框架实现 RPC(Remote Procedure Call,远程过程调用)框架是分布式服务的基石,实现RPC框架需要考虑方方面面.其对业务隐藏了底层通信过程(TCP/UDP.打包/解包.序 ...

  7. Java实现简单的RPC框架

    一.RPC简介 RPC,全称为Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用) ...

  8. 服务化实战之 dubbo、dubbox、motan、thrift、grpc等RPC框架比较及选型

    转自: http://blog.csdn.net/liubenlong007/article/details/54692241 概述 前段时间项目要做服务化,所以我比较了现在流行的几大RPC框架的优缺 ...

  9. Java实现简单的RPC框架(美团面试)

    一.RPC简介 RPC,全称为Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用) ...

  10. FW:主流RPC框架

    主流RPC框架  2015年10月27日  zman  RPC 介绍目前在互联网公司比较流行的开源的RPC框架. RPC框架比较   语言 协议 ​服务治理 ​社区 机构 Hessian 多语言 he ...

随机推荐

  1. Android进阶(十)Android 发邮件

    最近在做的APP涉及到发邮件,总结如下: 在android里进行邮件客户端开发可以有两种方式: 在邮件客户端的设计中,可以采用两种方法. 一种是调用android系统自带的邮件服务 优点:这种方法比较 ...

  2. Chapter 1 Securing Your Server and Network(14):限制功能——xp_cmdshell 和OPENROWSET

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/38656615,专题目录:http://blog.csdn.net/dba_huangzj ...

  3. Scipy教程 - 距离计算库scipy.spatial.distance

    http://blog.csdn.net/pipisorry/article/details/48814183 在scipy.spatial中最重要的模块应该就是距离计算模块distance了. fr ...

  4. OJ题:成绩排序

    题目描述 查找和排序 题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩 都按先录入排列在前的规则处理. 例示: jack 70 peter 96 Tom 70 smit ...

  5. SpriteBuilder中CCMotionStreak坐标类型不匹配

    在SpriteBuilder需要被跟随的(或是说被拖尾的)节点坐标类型是父百分比,先是将CCMotionStreak本身位置设置为百分比类型,但是无效. 将节点坐标改为正常点类型后,MotionStr ...

  6. ISLR系列:(4.2)模型选择 Ridge Regression & the Lasso

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  7. Leetcode_116_Populating Next Right Pointers in Each Node

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43532817 Given a binary tree st ...

  8. App热补丁动态修复技术介绍

    安卓App热补丁动态修复技术介绍 来自qq空间团队:微信号qzonemobiledev QQ空间终端开发团队 1.背景 当一个App发布之后,突然发现了一个严重bug需要进行紧急修复,这时候公司各方就 ...

  9. android混淆和反编译

    混淆 Android Studio:  只需在build.gradle(Module:app)中的buildTypes中增加release的编译选项即可,如下: <code class=&quo ...

  10. Android 之dragger使用

    1.依赖的注入和配置独立于组件之外,注入的对象在一个独立.不耦合的地方初始化,这样在改变注入对象时,我们只需要修改对象的实现方法,而不用大改代码库. 2.依赖可以注入到一个组件中:我们可以注入这些依赖 ...