一、什么是sofa:
SOFAStack(Scalable Open Financial Architecture Stack)是用于快速构建金融级分布式架构的一套中间件,也是在金融场景里锤炼出来的最佳实践。
首先我们县城最简单的项目、SOFARPC 是蚂蚁金服开源的一款基于 Java 实现的 RPC 服务框架,为应用之间提供远程服务调用能力,具有高可伸缩性,高容错性,目前蚂蚁金服所有的业务的相互间的 RPC 调用都是采用 SOFARPC。SOFARPC 为用户提供了负载均衡,流量转发,链路追踪,链路数据透传,故障剔除等功能。SOFARPC 还支持不同的协议,目前包括 bolt,RESTful,dubbo,H2C 协议进行通信。其中 bolt 是蚂蚁金融服务集团开放的基于 Netty 开发的网络通信框架。
详细信息读API文档:https://www.sofastack.tech/sofa-rpc/docs/Home
二、多的不说上代码:
1、服务端:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--<parent>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.1.2.RELEASE</version>-->
<!--<relativePath/> <!– lookup parent from repository –>-->
<!--</parent>-->
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.0.0</version>
</parent>
<groupId>com.citydo</groupId>
<artifactId>sofaboot_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sofaboot_server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-test</artifactId>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>com.alipay.sofa</groupId>-->
<!--<artifactId>healthcheck-sofa-boot-starter</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-all</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
rpc-sofa.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="helloSynServiceImpl" class="com.citydo.sofaboot_server.service.impl.HelloSyncServiceImpl" />
<sofa:service ref="helloSynServiceImpl" interface="com.citydo.sofaboot_server.service.HelloSyncService">
<!--服务通过 bolt 协协议通道发布,底层基于 Netty 实现。-->
<sofa:binding.bolt/>
<!--服务通过 http 协议发布。-->
<sofa:binding.rest/>
<!--服务基于 dubbo 的协议通道发布。-->
<sofa:binding.dubbo/>
</sofa:service>
<!--Future方式-->
<!--<bean id="helloFutureServiceImpl" class="com.citydo.sofaboot_server.service.impl.HelloFutureServiceImpl"/>-->
<!--<sofa:service ref="helloFutureServiceImpl" interface="com.citydo.sofaboot_server.service.HelloFutureService">-->
<!--<sofa:binding.bolt/>-->
<!--</sofa:service>-->
<!--回调方式-->
<!--<bean id="helloCallbackServiceImpl" class="com.citydo.sofaboot_server.service.impl.HelloCallbackServiceImpl"/>-->
<!--<sofa:service ref="helloCallbackServiceImpl" interface="com.citydo.sofaboot_server.service.HelloCallbackService">-->
<!--<sofa:binding.bolt/>-->
<!--</sofa:service>-->
<!--<bean id="sampleGenericServiceImpl" class="com.citydo.sofaboot_server.service.impl.SampleGenericServiceImpl"/>-->
<!--<sofa:service ref="sampleGenericServiceImpl" interface="com.citydo.sofaboot_server.service.SampleGenericService">-->
<!--<sofa:binding.bolt/>-->
<!--</sofa:service>-->
</beans>
HelloSyncService.java、HelloSyncServiceImpl.java
package com.citydo.sofaboot_server.service;
/**
* @author nick
*/
public interface HelloSyncService {
String saySync(String string);
}
package com.citydo.sofaboot_server.service.impl;
import com.citydo.sofaboot_server.service.HelloSyncService;
public class HelloSyncServiceImpl implements HelloSyncService {
@Override
public String saySync(String sync) {
return sync;
}
}
启动类
package com.citydo.sofaboot_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
/**
* 单向调用、同步调用、异步调用和回调
*/
@ImportResource({"classpath:spring/rpc-sofa.xml"})
@SpringBootApplication
public class SofabootServerApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SofabootServerApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
}
}
2、客户端
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--<parent>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.1.2.RELEASE</version>-->
<!--<relativePath/> <!– lookup parent from repository –>-->
<!--</parent>-->
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.0.0</version>
</parent>
<groupId>com.citydo</groupId>
<artifactId>sofaboot_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sofaboot_client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-web</artifactId>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-test</artifactId>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-all</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
rpc-sofa.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">
<!--同步方式-->
<!-- bolt引用 -->
<sofa:reference id="boltHelloSyncServiceReference" interface="com.citydo.sofaboot_client.service.HelloSyncService">
<sofa:binding.bolt/>
</sofa:reference>
<!-- rest引用 -->
<sofa:reference id="restHelloSyncServiceReference" interface="com.citydo.sofaboot_client.service.HelloSyncService">
<sofa:binding.rest/>
</sofa:reference>
<!-- dubbo引用 -->
<sofa:reference id="dubboHelloSyncServiceReference" interface="com.citydo.sofaboot_client.service.HelloSyncService">
<sofa:binding.dubbo/>
</sofa:reference>
<!--单向方式-->
<!--<sofa:reference id="dubboHelloSyncServiceReference" interface="com.citydo.sofaboot_client.service.HelloSyncService">-->
<!--<sofa:binding.bolt>-->
<!--<sofa:global-attrs type="oneway"/>-->
<!--</sofa:binding.bolt>-->
<!--</sofa:reference>-->
<!--Future 方式-->
<!--<sofa:reference id="helloFutureServiceReference" interface="com.citydo.sofaboot_client.service.HelloFutureService">-->
<!--<sofa:binding.bolt>-->
<!--<sofa:global-attrs type="future"/>-->
<!--</sofa:binding.bolt>-->
<!--</sofa:reference>-->
<!--回调方式-->
<!--<bean id="callbackImpl" class="com.citydo.sofaboot_client.service.impl.CallbackImpl"/>-->
<!--<sofa:reference id="helloCallbackServiceReference" interface="com.citydo.sofaboot_client.service.HelloCallbackService">-->
<!--<sofa:binding.bolt>-->
<!--<sofa:global-attrs type="callback" callback-ref="callbackImpl"/>-->
<!--</sofa:binding.bolt>-->
<!--</sofa:reference>-->
<!--<sofa:reference id="sampleGenericServiceReference" interface="com.alipay.sofa.rpc.api.GenericService">-->
<!--<sofa:binding.bolt>-->
<!--<sofa:global-attrs generic-interface="com.ostenant.sofa.rpc.example.generic.SampleGenericService"/>-->
<!--</sofa:binding.bolt>-->
<!--</sofa:reference>-->
</beans>
HelloSyncService.java
package com.citydo.sofaboot_client.service;
public interface HelloSyncService {
String saySync(String string);
}
启动类
package com.citydo.sofaboot_client;
import com.citydo.sofaboot_client.service.HelloCallbackService;
import com.citydo.sofaboot_client.service.HelloFutureService;
import com.citydo.sofaboot_client.service.HelloSyncService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
/**
* @author nick
*/
@ImportResource({"classpath:spring/rpc-sofa.xml"})
@SpringBootApplication
public class SofabootClientApplication {
public static void main(String[] args) {
System.setProperty("server.port", "8081");
SpringApplication springApplication = new SpringApplication(SofabootClientApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
HelloSyncService boltHelloSyncService = (HelloSyncService) applicationContext.getBean("boltHelloSyncServiceReference");
HelloSyncService restHelloSyncService = (HelloSyncService) applicationContext.getBean("restHelloSyncServiceReference");
HelloSyncService dubboHelloSyncService = (HelloSyncService) applicationContext.getBean("dubboHelloSyncServiceReference");
System.out.println("Bolt result:" + boltHelloSyncService.saySync("bolt"));
System.out.println("Rest result:" + restHelloSyncService.saySync("rest"));
System.out.println("Dubbo result:" + dubboHelloSyncService.saySync("dubbo"));
// HelloFutureService helloFutureServiceReference = (HelloFutureService) applicationContext
.getBean("helloFutureServiceReference");
helloFutureServiceReference.sayFuture("future");
try {
String result = (String)SofaResponseFuture.getResponse(1000, true);
System.out.println("Future result: " + result);
} catch (InterruptedException e) {
e.printStackTrace();
}
// HelloCallbackService helloCallbackServiceReference = (HelloCallbackService) applicationContext
// .getBean("helloCallbackServiceReference");
// helloCallbackServiceReference.sayCallback("callback");
// try {
// Thread.sleep(3000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}