【Soul源码阅读】7. sofa 用户接入 Soul 网关流程解析

官网文档中有详细的接入流程,地址:https://dromara.org/zh-cn/docs/soul/user-sofa.html

之前一直没接触过 sofa,官网的介绍如下:

SOFARPC is a high-performance, high-extensibility, production-level Java RPC framework. In Ant Financial, SOFARPC has been used for more than ten years and developing for five generations. SOFARPC is dedicated to simplify RPC calls between applications, and provide convenient, no code intrusion, stable, and efficient point-to-point remote service invocation solutions for applications. For user and developer easy to improve features, SOFARPC provides a wealth of model abstraction and extensible interfaces, including filter, routing, load balancing, and so on. At the same time, it provides a rich MicroService governance solution around the SOFARPC framework and its surrounding components.

再看下 dubbo 官网:

Apache Dubbo is a high-performance, Java-based open-source RPC framework. Please visit official site for quick start and documentations, as well as the wiki for news, FAQ, and release notes.

无论是昨天的 dubbo,还是今天的 sofa,都是 RPC 框架,用于分布系统间互相调用,这些调用都是在后台服务之间。

现在估计就有小伙伴疑问了,都是后台的服务,接入 Soul 网关是用于什么场景呢?

接入 Soul 网关,可以理解为,把后台的服务直接转换成 HTTP 请求,方便前端直接调用,而不是还需要写一套类似 SpringMVC 的 Controller 层来适配,方便了很多。

好了,说了这些,开始跟我一起操作吧。

1.前置条件

2.引入网关对sofa支持的插件

官网给了以下依赖样例,soul-bootstrap 的 pom 文件中没有,加上。

<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>

网关修改 pom 文件后,需要重启一下。 

这里插入一个小插曲,刚开始我接入时,只引入了 soul-spring-boot-starter-plugin-sofa 的依赖,其他没有引入,重启网关时,有一个 ClassNotFoundException,当时没在意。

然后使用 postman 调用,虽然返回的是 200,但没有返回数据,看了各个后台,也都没有异常。

找了半天,到这里才发现错误是因为依赖引用的不全。

这里告诉我,不要想当然,先老老实实按照官网指导操作来。如果还有问题,就可以到 github 上提 issue

要养成这样的习惯,融入到社区,提高了自身技术水平,也进一步促进了 Soul 网关的发展!

3.sofa 服务接入网关

依次给出了 springboot 和 spring 2种框架下接入的方案。

这里以源码中的示例 soul-examples-sofa 为例子。

3.1 maven 依赖

很显然,这个项目 soul-examples-sofa 属于 springboot 框架。

<dependency>
    <groupId>org.dromara</groupId>
    <artifactId>soul-spring-boot-starter-client-sofa</artifactId>
    <version>${soul.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
        </exclusion>
    </exclusions>
</dependency>

3.2 yml 配置文件

soul:
  sofa:
    adminUrl: http://localhost:9095
    contextPath: /sofa
    appName: sofa
# adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加 http://
# contextPath: 为你的这个项目在soul网关的路由前缀,这个你应该懂意思把? 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
# appName:你的应用名称,不配置的话,会默认取sofa配置中application 中的名称

3.3 注解 @SoulSofaClient

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface SoulSofaClient {
    
    String path();

    String ruleName() default "";

    String desc() default "";

    boolean enabled() default true;

    String loadBalance() default "hash";

    int timeout() default -1;

    int retries() default 3;
}

通过找这个注解使用的地方,又找到了类似的代码,url 就是自动将 sofa 服务接口信息同步给 soul-admin 的注册地址。

// SofaServiceBeanPostProcessor.java
public SofaServiceBeanPostProcessor(final SofaConfig sofaConfig) {
    String contextPath = sofaConfig.getContextPath();
    String adminUrl = sofaConfig.getAdminUrl();
    if (contextPath == null || "".equals(contextPath)
            || adminUrl == null || "".equals(adminUrl)) {
        throw new RuntimeException("sofa client must config the contextPath, adminUrl");
    }
    this.sofaConfig = sofaConfig;
    url = sofaConfig.getAdminUrl() + "/soul-client/sofa-register";
    executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
}

4.打开 sofa 插件

【Soul网关管理】【系统管理】【插件管理】【编辑】

这里注册中心配置的是 zookeeper。

5.启动项目

依次启动3个项目,正常启动后,如下图所示:

【Soul网关管理】【插件列表】【sofa】

在 soul-admin web 页面可以看到 sofa 接口注册进来了。

使用 ZooInspector 连接 zk,可以看到 sofa-rpc 节点及接口信息:

6.调用接口,测试

6.1无参 GET 方法

localhost:9195/sofa/findAll

6.2 有参 GET 方法

localhost:9195/sofa/findById?id=95

6.3 有参数 POST 请求

localhost:9195/sofa/insert
{
    "id": "95",
    "name": "sofa_insert_name"
}

猜你喜欢

转载自blog.csdn.net/hellboy0621/article/details/112912959