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文件中加入:

<dependency>
    <groupId>org.dromara</groupId>
    <artifactId>soul-spring-boot-starter-plugin-apache-dubbo</artifactId>
    <version>${project.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.8</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>

启动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<String, ServiceBean> serviceBean = contextRefreshedEvent.getApplicationContext().getBeansOfType(ServiceBean.class);
    for (Map.Entry<String, ServiceBean> entry : serviceBean.entrySet()) {
        executorService.execute(() -> handler(entry.getValue()));
    }
}

总结

  • 学习dubbo插件的使用
    • dubbo插件是将http协议 转换成dubbo协议 的插件
  • 解读部分源码
    • 核心类ApacheDubboServiceBeanPostProcessor阅读
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,029评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,238评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,576评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,214评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,324评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,392评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,416评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,196评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,631评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,919评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,090评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,767评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,410评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,090评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,328评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,952评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,979评论 2 351

推荐阅读更多精彩内容