可扩展、高性能、响应迅速的API网关——Soul进行http协议转换sofa协议(四)

本文章将成系列介绍:包含但不限于高性能微服务API网关Soul的环境搭建、源码设计

本章节介绍使用soul进行http协议转换sofa协议

SOFA简介

​ SOFA 作为蚂蚁金服自主研发的分布式的系统中间件,包含金融级云原生的架构所需要的各种组件,其中包括微服务的研发框架、RPC调用框架、服务的注册中心、分布式定时任务、限流/熔断机制、动态配置的推送功能、分布式的链路追踪、Metrics监控、分布式高可用队列、分布式事务框架、分布式数据库代理等组件。是在金融领域完整的分布式解决方案。
​ SOFA作为蚂蚁金服的开源的高性能RPC框架。
 SOFA所做的工作就是简化应用之间的RPC调用,为应用提供高效的点对点的远程调用服务解决方案。也提供了很多的用户扩展功能,SOFA提供了丰富的模型和抽象可扩展接口,包括过滤器、路由、负载均衡等等。

配置方式

网关接入方式
  • 在网关的 pom.xml 文件中增加如下依赖:
    • sofa版本换成你的,注册中心的jar包换成你的,以下是参考。
       <dependency>
           <groupId>com.alipay.sofa</groupId>
           <artifactId>sofa-rpc-all</artifactId>
           <version>5.7.6</version>
       </dependency>
       <dependency>
           <groupId>org.apache.curator</groupId>
           <artifactId>curator-client</artifactId>
           <version>4.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.apache.curator</groupId>
           <artifactId>curator-framework</artifactId>
           <version>4.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.apache.curator</groupId>
           <artifactId>curator-recipes</artifactId>
           <version>4.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.dromara</groupId>
           <artifactId>soul-spring-boot-starter-plugin-sofa</artifactId>
           <version>${last.version}</version>
       </dependency>
  • 重启网关服务。
SOFA服务接入网关
  • springboot
    • 引入以下依赖
       <dependency>
           <groupId>org.dromara</groupId>
           <artifactId>soul-spring-boot-starter-client-sofa</artifactId>
           <version>${soul.version}</version>
       </dependency>
  • 在你的yml文件中新增如下配置 :
     soul:
       sofa:
         adminUrl: http://localhost:9095
         contextPath: /sofa
         appName: sofa
      # adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加 http://
      # contextPath: 为你的这个项目在soul网关的路由前缀,这个你应该懂意思把? 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
      # appName:你的应用名称,不配置的话,会默认取sofa配置中application 中的名称
SOFA插件设置
  • 首先在 soul-admin 插件管理中,把sofa 插件设置为开启。
  • 其次在 sofa 插件中配置你的注册地址或者其他注册中心的地址.
{"protocol":"zookeeper","register":"127.0.0.1:2181"}
接口注册到网关
  • 你sofa服务实现类的,方法上加上 @SoulSofaClient 注解,表示该接口方法注册到网关。
  • 启动你的提供者,输出日志 sofa client register success 大功告成,你的sofa接口已经发布到 soul网关.如果还有不懂的,可以参考 soul-test-sofa项目。
sofa用户请求以及参数说明
  • 说白了,就是通过http的方式来请求你的sofa服务
  • soul网关需要有一个路由前缀,这个路由前缀就是你接入项目进行配置 contextPath
# 比如你有一个 order服务 它有一个接口,它的注册路径 /order/test/save

# 现在就是通过 post方式请求网关:http://localhost:9195/order/test/save

# 其中 localhost:9195 为网关的ip端口,默认端口是9195 ,/order 是你sofa接入网关配置的 contextPath
  • 参数传递:
    • 通过 http post 方式访问网关,通过body,json类型传递。
  • 单个java bean参数类型 (默认)
  • 自定义实现多参数支持:
    • 在你搭建的网关项目中,新增一个类 A,实现 org.dromara.soul.plugin.api.sofa.SofaParamResolveService
   public interface SofaParamResolveService {
   
       /**
        * Build parameter pair.
        * this is Resolve http body to get sofa param.
        *
        * @param body           the body
        * @param parameterTypes the parameter types
        * @return the pair
        */
       Pair<String[], Object[]> buildParameter(String body, String parameterTypes);
   }
  • body为http中body传的json字符串。
  • parameterTypes: 匹配到的方法参数类型列表,如果有多个,则使用,分割。
  • Pair中,left为参数类型,right为参数值,这是sofa泛化调用的标准。
  • 把你的类注册成Spring的bean,覆盖默认的实现。
   @Bean
    public SofaParamResolveService A() {
            return new A();
    }

源码跟踪

彩蛋:https://blog.csdn.net/github_35735591/article/details/112759133

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值