一、简介
SOFARPC 是蚂蚁金服开源的一款基于 Java 实现的 RPC 服务框架,为应用之间提供远程服务调用能力,具有高可伸缩性,高容错性,目前蚂蚁金服所有的业务的相互间的 RPC 调用都是采用 SOFARPC。SOFARPC 为用户提供了负载均衡,流量转发,链路追踪,链路数据透传,故障剔除等功能。
SOFARPC 还支持不同的协议,目前包括 bolt,RESTful,dubbo,H2C 协议进行通信
二、使用
1.HelloSyncService接口
public interface HelloSyncService {
String saySync(String string);
}
2.HelloSyncServiceImpl实现类
public class HelloSyncServiceImpl implements HelloSyncService {
@Override
public String saySync(String string) {
return string;
}
}
3.DemoApplication启动类
@ImportResource({ "classpath*:applicationContext.xml" })
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext
.getBean("helloSyncServiceReference");
System.out.println(helloSyncServiceReference.saySync("sync"));
}
}
4.applicationContext.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="com.sofarpctest.demo.provider.HelloSyncServiceImpl"/>
<sofa:service ref="helloSyncServiceImpl" interface="com.sofarpctest.demo.provider.HelloSyncService">
<sofa:binding.bolt/>
</sofa:service>
<sofa:reference id="helloSyncServiceReference" interface="com.sofarpctest.demo.provider.HelloSyncService">
<sofa:binding.bolt/>
</sofa:reference>
</beans>