蚂蚁 RPC 框架 SOFA-RPC 初体验

image.png

前言

最近蚂蚁金服开源了分布式框架 SOFA,楼主写了一个 demo,体验了一下 SOFA 的功能,SOFA 彻底兼容 SpringBoot(固然 Dubbo 也是能够兼容的)。php

项目地址:Alipay ,该主页有 5 个项目,都是阿里开源的。 sofa-bootsofa-rpcsofa-boltsofa-arksofa-rpc-boot-projectsjava

快速开始

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

因为测试例子须要两个项目,咱们建一个目录,目录下建立两个 maven module(SpringBoot 项目便可):github

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

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

<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 框架的依赖和搭建就行了,是否是很简单?mvc

接口建立

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

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 相关的代码。