《开源精选》是我们分享 Github、Gitee 等开源社区中优质项目的栏目,包括技术、学习、实用与各种有趣的内容。本期推荐的 SOFARPC 是一个高可扩展性、高性能、生产级的 Java RPC 框架。
项目介绍
SOFARPC 致力于简化应用之间的 RPC 调用,为应用提供方便透明、稳定高效的点对点远程服务调用方案。为了用户和开发者方便的进行功能扩展,SOFARPC 提供了丰富的模型抽象和可扩展接口,包括过滤器、路由、负载均衡等等。同时围绕 SOFARPC 框架及其周边组件提供丰富的微服务治理方案。
功能特性
-
• 透明化、高性能的远程服务调用
-
• 支持多种服务路由及负载均衡策略
-
• 支持多种注册中心的集成
-
• 支持多种协议,包括 Bolt、Rest、Dubbo 等
-
• 支持同步、单向、回调、泛化等多种调用方式
-
• 支持集群容错、服务预热、自动故障隔离
-
• 强大的扩展功能,可以按需扩展各个功能组件
开始使用 SOFABoot
请注意,代码示例需要本地安装 zookeeper 环境。如果没有,您需要删除 com.alipay.sofa.rpc.registry.address
配置application.properties
以使用本地文件作为注册中心。
创建项目
-
• 准备环境:SOFABoot 需要 JDK7或 JDK8,需要用 Apache Maven 2.2.5或以上版本编译。
-
• 构建 SOFABoot 项目:SOFABoot 基于 Spring Boot。所以可以使用 Spring Boot 的项目生成工具来生成一个标准的 Spring Boot 项目。
-
• 添加 SOFABoot 依赖:生成的标准 Spring Boot 项目直接使用 Spring 父依赖,需要改成 SOFABoot 提供的父依赖。父依赖提供和管理 SOFABoot 提供的各种 starter。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<relativePath/>
</parent>
将以上内容替换为以下内容:
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofaboot-dependencies</artifactId>
<version>3.0.0</version>
</parent>
-
1. Configure
application.properties
:application.properties
是 SOFABoot 项目中的配置文件。这里需要配置应用名称。
spring.application.name=AppName
2.引入 RPC 启动器:
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>rpc-sofa-boot-starter</artifactId>
</dependency>
3.声明 SOFABoot 的 xsd 文件:
在要使用的 XML 配置文件中,将头 xsd 文件的声明配置为以下内容。这支持使用 SOFABoot 定义的 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"
定义服务接口和实现
public interface HelloSyncService {
String saySync(String string);
}
public class HelloSyncServiceImpl implements HelloSyncService {
@Override
public String saySync(String string) {
return string;
}
}
在服务器上发布服务
在 xml 文件中配置以下内容。当 Spring 上下文刷新时,SOFABoot 在服务端注册服务实现,通过 Bolt 协议与客户端通信,并将地址等元数据发布到注册中心(默认使用本地文件作为注册中心)。
<bean id="helloSyncServiceImpl" class="com.alipay.sofa.rpc.samples.invoke.HelloSyncServiceImpl"/>
<sofa:service ref="helloSyncServiceImpl" interface="com.alipay.sofa.rpc. samples.invoke.HelloSyncService"
<sofa:binding.bolt/>
</sofa:service>
客户参考服务
在 xml 文件中配置以下内容。当 Spring 上下文刷新时,SOFABoot 会生成一个 RPC 代理 bean personReferenceBolt
,. 这允许您直接在代码中使用 bean 进行远程调用。
<sofa:reference id="helloSyncServiceReference" interface="com.alipay.sofa.rpc.samples.invoke.HelloSyncService">
<sofa:binding.bolt/>
</sofa:reference>
运行项目
SpringBoot 的启动类编码如下。上面的 xml 文件在这里使用 ImportResource 加载。
@ImportResource({ "classpath*:rpc-sofa-boot-starter-samples.xml" })
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SofaBootRpcSamplesApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SofaBootRpcSamplesApplication.class);
ApplicationContext applicationContext = springApplication.run(args);
HelloSyncService helloSyncServiceReference = (HelloSyncService) applicationContext
.getBean("helloSyncServiceReference");
System.out.println(helloSyncServiceReference.saySync("sync") );
}
}
路线图
传送门
开源协议:Apache 2.0
开源地址:https://gitee.com/sofastack/sofa-rpc
-END-
原文始发于微信公众号(开源技术专栏):高可扩展性、高性能、生产级的 Java RPC 框架
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/79642.html