您的位置 首页 java

SOFARPC 一个高性能的 Java RPC 框架

《开源精选》是我们分享 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="#34; 
       xmlns:xsi=" .w3.org/2001/XMLSchema-instance" 
       xmlns:sofa="#34; 
       xmlns:context="#34; 
       xsi:schemaLocation ="  
              .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 文件在这里使用 Import Resource 加载。

 @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") ); 
    } 
}   

路线图

更多内容:

文章来源:智云一二三科技

文章标题:SOFARPC 一个高性能的 Java RPC 框架

文章地址:https://www.zhihuclub.com/197416.shtml

关于作者: 智云科技

热门文章

网站地图