Soul API网关学习-Day03

目标

  • 运行examples下面的 apache-dubbo服务
  • 学习文档,结合dubbo插件,发起http请求soul网关,体验dubbo代理
  • 记录心得

准备工作

设置Dubbo插件

进入Soul Admin控制台,http://localhost:9095,进入「系统管理 -> 插件管理」菜单,可以看到 dubbo 插件。如下图所示:

点击“编辑”按钮:

设置Dubbo注册中心的地址信息:{"register":"zookeeper://localhost:2181"}
并设置开启状态.

因为 Soul Bootstrap 和 Soul Admin 暂时不支持插件修改的自动加载,所以我们此时需要手动重启下。

Soul-bootstrap API网关引入dubbo相关依赖

在pom.xml文件中加入:


    org.dromara
    soul-spring-boot-starter-plugin-apache-dubbo
    ${project.version}


    org.apache.dubbo
    dubbo
    2.7.8



    org.apache.curator
    curator-client
    4.0.1


    org.apache.curator
    curator-framework
    4.0.1


    org.apache.curator
    curator-recipes
    4.0.1


启动Soul-boostrap

运行dubbo provider项目

运行soul-examples/soul-examples-dubbo/soul-example-apache-dubbo-service的dubbo示例

配置server.port和soul.dubbo.context-path:

控制台输出日志:

2021-01-16 10:33:26.214  INFO 39870 --- [           main] o.a.c.f.imps.CuratorFrameworkImpl        : Starting
2021-01-16 10:33:26.217  INFO 39870 --- [           main] org.apache.zookeeper.ZooKeeper           : Initiating client connection, connectString=localhost:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@7a9ceddf
2021-01-16 10:33:26.219  INFO 39870 --- [           main] org.apache.zookeeper.common.X509Util     : Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-01-16 10:33:26.226  INFO 39870 --- [           main] org.apache.zookeeper.ClientCnxnSocket    : jute.maxbuffer value is 4194304 Bytes
2021-01-16 10:33:26.230  INFO 39870 --- [           main] org.apache.zookeeper.ClientCnxn          : zookeeper.request.timeout value is 0. feature enabled=
2021-01-16 10:33:26.234  INFO 39870 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
2021-01-16 10:33:26.241  INFO 39870 --- [           main] o.a.c.f.imps.CuratorFrameworkImpl        : Default schema
2021-01-16 10:33:26.251  INFO 39870 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:53884, server: localhost/0:0:0:0:0:0:0:1:2181
2021-01-16 10:33:26.278  INFO 39870 --- [localhost:2181)] org.apache.zookeeper.ClientCnxn          : Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1000ee25a01000b, negotiated timeout = 40000
2021-01-16 10:33:26.284  INFO 39870 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: CONNECTED
2021-01-16 10:33:26.298  INFO 39870 --- [ain-EventThread] o.a.c.framework.imps.EnsembleTracker     : New config event received: {}
2021-01-16 10:33:26.298  INFO 39870 --- [ain-EventThread] o.a.c.framework.imps.EnsembleTracker     : New config event received: {}
2021-01-16 10:33:26.851  INFO 39870 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8011 (http) with context path ''
2021-01-16 10:33:26.855  INFO 39870 --- [           main] o.d.s.e.a.d.s.TestApacheDubboApplication : Started TestApacheDubboApplication in 2.642 seconds (JVM running for 3.53)
2021-01-16 10:33:27.158  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/insert","pathDesc":"Insert a row of data","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboTestService","methodName":"insert","ruleName":"/dubbo/insert","parameterTypes":"org.dromara.soul.examples.dubbo.api.entity.DubboTest","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.199  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findById","pathDesc":"Query by Id","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboTestService","methodName":"findById","ruleName":"/dubbo/findById","parameterTypes":"java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.237  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findAll","pathDesc":"Get all data","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboTestService","methodName":"findAll","ruleName":"/dubbo/findAll","parameterTypes":"","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.275  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByIdsAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByIdsAndName","ruleName":"/dubbo/findByIdsAndName","parameterTypes":"java.util.List,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.315  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByArrayIdsAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByArrayIdsAndName","ruleName":"/dubbo/findByArrayIdsAndName","parameterTypes":"[Ljava.lang.Integer;,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.352  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByStringArray","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByStringArray","ruleName":"/dubbo/findByStringArray","parameterTypes":"[Ljava.lang.String;","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.398  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByListId","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByListId","ruleName":"/dubbo/findByListId","parameterTypes":"java.util.List","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.438  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSave","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSave","ruleName":"/dubbo/batchSave","parameterTypes":"java.util.List","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.481  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSaveAndNameAndId","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSaveAndNameAndId","ruleName":"/dubbo/batchSaveAndNameAndId","parameterTypes":"java.util.List,java.lang.String,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.519  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTest","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTest","ruleName":"/dubbo/saveComplexBeanTest","parameterTypes":"org.dromara.soul.examples.dubbo.api.entity.ComplexBeanTest","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 10:33:27.553  INFO 39870 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTestAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTestAndName","ruleName":"/dubbo/saveComplexBeanTestAndName","parameterTypes":"org.dromara.soul.examples.dubbo.api.entity.ComplexBeanTest,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 

通过日志输出的内容,看到将dubbo元数据信息注册到Soul Admin控制台。在Soul Admin中我们可以看到:

我们切换到Soul-Bootstrap API网关控制台,看到日志输出如下:

2021-01-16 10:33:27.711  INFO 39740 --- [-24-EventThread] o.a.c.framework.imps.EnsembleTracker     : New config event received: {}
2021-01-16 10:33:27.711  INFO 39740 --- [-24-EventThread] o.a.c.framework.imps.EnsembleTracker     : New config event received: {}
2021-01-16 10:33:27.956  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/insert, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboTestService, methodName=insert, parameterTypes=org.dromara.soul.examples.dubbo.api.entity.DubboTest, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:27.971  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/findById, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboTestService, methodName=findById, parameterTypes=java.lang.String, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:27.981  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/findAll, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboTestService, methodName=findAll, parameterTypes=null, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:27.999  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/findByIdsAndName, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=findByIdsAndName, parameterTypes=java.util.List,java.lang.String, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.015  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/findByArrayIdsAndName, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=findByArrayIdsAndName, parameterTypes=[Ljava.lang.Integer;,java.lang.String, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.025  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/findByStringArray, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=findByStringArray, parameterTypes=[Ljava.lang.String;, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.036  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/findByListId, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=findByListId, parameterTypes=java.util.List, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.046  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/batchSave, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=batchSave, parameterTypes=java.util.List, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.056  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/batchSaveAndNameAndId, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=batchSaveAndNameAndId, parameterTypes=java.util.List,java.lang.String,java.lang.String, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.067  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/saveComplexBeanTest, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=saveComplexBeanTest, parameterTypes=org.dromara.soul.examples.dubbo.api.entity.ComplexBeanTest, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)
2021-01-16 10:33:28.076  INFO 39740 --- [ctReadThread-24] o.d.s.p.a.d.c.ApplicationConfigCache     : init apache dubbo reference success there meteData is :MetaData(id=null, appName=dubbo, contextPath=/dubbo, path=/dubbo/saveComplexBeanTestAndName, rpcType=dubbo, serviceName=org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService, methodName=saveComplexBeanTestAndName, parameterTypes=org.dromara.soul.examples.dubbo.api.entity.ComplexBeanTest,java.lang.String, rpcExt={"group":"","version":"","loadbalance":"random","retries":2,"timeout":10000,"url":""}, enabled=true)

API网关将dubbo元数据配置同步更新到自已的内存中。

调用API网关请求Dubbo服务

GET http://127.0.0.1:9195/dubbo/findAll

HTTP/1.1 200 OK
Content-Type: application/json
content-length: 112

{
  "code": 200,
  "message": "Access to success!",
  "data": {
    "name": "hello world Soul Apache, findAll",
    "id": "1817748358"
  }
}

Response code: 200 (OK); Time: 331ms; Content length: 112 bytes

HTTP Status 返回200,证明通过API网关请求Dubboe服务成功

部分源码解读

查看Dubbo服务的应用日志,如下:

2021-01-16 11:10:13.442  INFO 40787 --- [ain-EventThread] o.a.c.framework.imps.EnsembleTracker     : New config event received: {}
2021-01-16 11:10:14.042  INFO 40787 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8012 (http) with context path ''
2021-01-16 11:10:14.045  INFO 40787 --- [           main] o.d.s.e.a.d.s.TestApacheDubboApplication : Started TestApacheDubboApplication in 2.686 seconds (JVM running for 3.297)
2021-01-16 11:10:14.250  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/insert","pathDesc":"Insert a row of data","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboTestService","methodName":"insert","ruleName":"/dubbo/insert","parameterTypes":"org.dromara.soul.examples.dubbo.api.entity.DubboTest","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.269  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findById","pathDesc":"Query by Id","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboTestService","methodName":"findById","ruleName":"/dubbo/findById","parameterTypes":"java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.288  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findAll","pathDesc":"Get all data","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboTestService","methodName":"findAll","ruleName":"/dubbo/findAll","parameterTypes":"","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.307  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByIdsAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByIdsAndName","ruleName":"/dubbo/findByIdsAndName","parameterTypes":"java.util.List,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.326  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByArrayIdsAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByArrayIdsAndName","ruleName":"/dubbo/findByArrayIdsAndName","parameterTypes":"[Ljava.lang.Integer;,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.343  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByStringArray","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByStringArray","ruleName":"/dubbo/findByStringArray","parameterTypes":"[Ljava.lang.String;","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.360  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByListId","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByListId","ruleName":"/dubbo/findByListId","parameterTypes":"java.util.List","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.377  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSave","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSave","ruleName":"/dubbo/batchSave","parameterTypes":"java.util.List","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.393  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSaveAndNameAndId","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSaveAndNameAndId","ruleName":"/dubbo/batchSaveAndNameAndId","parameterTypes":"java.util.List,java.lang.String,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.408  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTest","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTest","ruleName":"/dubbo/saveComplexBeanTest","parameterTypes":"org.dromara.soul.examples.dubbo.api.entity.ComplexBeanTest","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 
2021-01-16 11:10:14.424  INFO 40787 --- [pool-2-thread-1] o.d.s.client.common.utils.RegisterUtils  : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTestAndName","pathDesc":"","rpcType":"dubbo","serviceName":"org.dromara.soul.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTestAndName","ruleName":"/dubbo/saveComplexBeanTestAndName","parameterTypes":"org.dromara.soul.examples.dubbo.api.entity.ComplexBeanTest,java.lang.String","rpcExt":"{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}","enabled":true} 

在应用启动的时候,我们通过输出的日志信息,看到了该应用提供的DubboService的元数据信息注册到Soul Admin控制台。元数据的数据格式如下:

{
  "appName": "dubbo",
  "contextPath": "/dubbo",
  "path": "/dubbo/insert",
  "pathDesc": "Insert a row of data",
  "rpcType": "dubbo",
  "serviceName": "org.dromara.soul.examples.dubbo.api.service.DubboTestService",
  "methodName": "insert",
  "ruleName": "/dubbo/insert",
  "parameterTypes": "org.dromara.soul.examples.dubbo.api.entity.DubboTest",
  "rpcExt": "{\"group\":\"\",\"version\":\"\",\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000,\"url\":\"\"}",
  "enabled": true
}

JSON属性说明:

属性 描述
appName 应用名称
contextPath contextPath
path dubbo service method name
pathDesc description
rpcType RPC类型 dubbo
serviceName Dubbo Service Name
methodName Dubbo Service Method Name
ruleName rule name,存储在rule表
parameterTypes method parameter types
rpcExt 扩展信息
enabled 是否开启,默认开启

我们看一下org.dromara.soul.examples.apache.dubbo.service.impl.DubboTestServiceImpl这个类的代码,如下:

@Override
@SoulDubboClient(path = "/insert", desc = "Insert a row of data")
public DubboTest insert(final DubboTest dubboTest) {
    dubboTest.setName("hello world Soul Apache Dubbo: " + dubboTest.getName());
    return dubboTest;
}

@SoulDubboClient这个Annotation是用来配置信元数据信息。通过ApacheDubboServiceBeanPostProcessor这个类实现了ApplicationListener接口,用来监听Spring Bean容器启动时的事件,将读取所有@DubboService的Spring Bean的元信息,并将这些数据异步的方式注册到Soul Admin。核心代码如下:

public ApacheDubboServiceBeanPostProcessor(final DubboConfig dubboConfig) {
    String contextPath = dubboConfig.getContextPath();
    String adminUrl = dubboConfig.getAdminUrl();
    if (StringUtils.isEmpty(contextPath)
            || StringUtils.isEmpty(adminUrl)) {
        throw new RuntimeException("apache dubbo client must config the contextPath, adminUrl");
    }
    this.dubboConfig = dubboConfig;
    // Soul Admin URL 负责注册元数据信息
    url = dubboConfig.getAdminUrl() + "/soul-client/dubbo-register";
    // 启动只有一个线程的线程池,负责异步将元数据注册到Soul Admin
    executorService = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
}

private void handler(final ServiceBean serviceBean) {
    Class clazz = serviceBean.getRef().getClass();
    if (ClassUtils.isCglibProxyClass(clazz)) {
        String superClassName = clazz.getGenericSuperclass().getTypeName();
        try {
            clazz = Class.forName(superClassName);
        } catch (ClassNotFoundException e) {
            log.error(String.format("class not found: %s", superClassName));
            return;
        }
    }
    final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
    for (Method method : methods) {
        // 读取@SoulDubboClient的配置元数据信息
        SoulDubboClient soulDubboClient = method.getAnnotation(SoulDubboClient.class);
        if (Objects.nonNull(soulDubboClient)) {
            // 将数据注册到Soul Admin
            RegisterUtils.doRegister(buildJsonParams(serviceBean, soulDubboClient, method), url, RpcTypeEnum.DUBBO);
        }
    }
}
    
**
 * 在Spring容器启动时,执行该方法
 * @param contextRefreshedEvent
 */
@Override
public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) {
    if (Objects.nonNull(contextRefreshedEvent.getApplicationContext().getParent())) {
        return;
    }
    // Fix bug(https://github.com/dromara/soul/issues/415), upload dubbo metadata on ContextRefreshedEvent
    // 读取所有@DubboService Annotation的Service
    Map serviceBean = contextRefreshedEvent.getApplicationContext().getBeansOfType(ServiceBean.class);
    for (Map.Entry entry : serviceBean.entrySet()) {
        executorService.execute(() -> handler(entry.getValue()));
    }
}

总结

  • 学习dubbo插件的使用
    • dubbo插件是将http协议 转换成dubbo协议 的插件
  • 解读部分源码
    • 核心类ApacheDubboServiceBeanPostProcessor阅读

你可能感兴趣的:(Soul API网关学习-Day03)