最近螞蟻金服開源了分佈式框架 SOFA,樓主寫了一個 demo,體驗了一下 SOFA 的功能,SOFA 徹底兼容 SpringBoot(固然 Dubbo 也是能夠兼容的)。php
項目地址:Alipay ,該主頁有 5 個項目,都是阿里開源的。 sofa-boot, sofa-rpc, sofa-bolt, sofa-ark, sofa-rpc-boot-projects。java
實際上,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 相關的代碼。