避免重复造*,并且官方文档比较丰富,提供了中文,涉及到官网有的直接提供链接。
简介
https://shenyu.apache.org/zh/projects/shenyu/overview/
快速开始
环境搭建
官方文档:
https://shenyu.apache.org/zh/projects/shenyu/shenyu-set-up/
官方提供多种方式,为了比较直观,我们采用本地构建的方式
下载代码:
git clone https://github.com/apache/incubator-shenyu.git
cd shenyu
编译代码
大家都知道,有时我们会写一下通用的maven 工程给其他工程依赖,这时本地运行时,需要把那些被依赖的工程 install 到本地maven 仓库,当然你也可以上传到私服,具体不做阐述,请读者自行百度。
mvn clean install -Dmaven.javadoc.skip=true -B -Drat.skip=true -Djacoco.skip=true -DskipITs -DskipTests
使用idea 打开 incubator-shenyu,加载完毕后执行下面操作。
Shenyu-Admin
网关的后台管理系统
修改shenyu-admin 模块的 application.yml 默认是采用mysql数据库,也可以用h2,下面我们用mysql 的方式
修改application.yml:
datasource:
url: jdbc:mysql://localhost:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: wushanghui
修改之前确保mysql 能访问。
运行启动类 ShenyuAdminBootstrap
http://localhost:9095/#/user/login
用户名密码为: admin/123456
Shenyu-Bootstrap
网关系统,这个才是提供网关服务的应用。
运行shenyu-Bootstrap 模块的启动类 ShenyuBootstrapApplication
http://localhost:9195/
访问后发现页面显示:
{
"code": -107,
"message": "Can not find selector, please check your configuration!",
"data": null
}
找不到选择器,请检查您的配置!
默认是没有能访问url地址的
查看 HealthFilter 类发现,"/actuator/health", “/health_check” 这两个健康检查的地址可以访问
Dubbo快速开始
官方中提供了多种服务的接入方式,下面我们使用dubbe 的方式来演示下:
官方文档地址:https://shenyu.apache.org/zh/projects/shenyu/quick-start-dubbo/
后面会用到 zookeeper,需提前下载启动,具体不做阐述,请读者自行下载启动。
找到运行的示例,如果没有识别为maven项目,在idea中添加下
运行shenyu-examples 模块下 shenyu-examples-dubbo 模块下 shenyu-examples-apache-dubbo-service 应用,这里我们使用apache 的dubbo。
在这个示例中有两个dubbe 服务类DubboTestServiceImpl 和 DubboMultiParamServiceImpl
运行启动类 TestAlibabaDubboApplication :
在控制台可以看到以下日志:
2021-07-05 14:11:35.770 INFO 22984 --- [or_consumer_-19] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findAll","pathDesc":"Get all data","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"findAll","ruleName":"/dubbo/findAll","parameterTypes":"","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:35.770 INFO 22984 --- [or_consumer_-20] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findList","pathDesc":"Find list","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"findList","ruleName":"/dubbo/findList","parameterTypes":"","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:35.770 INFO 22984 --- [or_consumer_-18] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findById","pathDesc":"Query by Id","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"findById","ruleName":"/dubbo/findById","parameterTypes":"java.lang.String","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:35.770 INFO 22984 --- [or_consumer_-17] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/insert","pathDesc":"Insert a row of data","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboTestService","methodName":"insert","ruleName":"/dubbo/insert","parameterTypes":"org.apache.shenyu.examples.dubbo.api.entity.DubboTest","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:35.861 INFO 22984 --- [or_consumer_-21] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSave","pathDesc":"batchSave","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSave","ruleName":"/dubbo/batchSave","parameterTypes":"java.util.List","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:35.954 INFO 22984 --- [or_consumer_-22] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByListId","pathDesc":"findByListId","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByListId","ruleName":"/dubbo/findByListId","parameterTypes":"java.util.List","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:36.048 INFO 22984 --- [or_consumer_-23] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByIdsAndName","pathDesc":"findByIdsAndName","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByIdsAndName","ruleName":"/dubbo/findByIdsAndName","parameterTypes":"java.util.List,java.lang.String","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:36.138 INFO 22984 --- [or_consumer_-24] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTestAndName","pathDesc":"saveComplexBeanTestAndName","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTestAndName","ruleName":"/dubbo/saveComplexBeanTestAndName","parameterTypes":"org.apache.shenyu.examples.dubbo.api.entity.ComplexBeanTest,java.lang.String","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:36.231 INFO 22984 --- [or_consumer_-25] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/batchSaveAndNameAndId","pathDesc":"batchSaveAndNameAndId","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"batchSaveAndNameAndId","ruleName":"/dubbo/batchSaveAndNameAndId","parameterTypes":"java.util.List,java.lang.String,java.lang.String","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:36.325 INFO 22984 --- [or_consumer_-26] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/saveComplexBeanTest","pathDesc":"saveComplexBeanTest","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"saveComplexBeanTest","ruleName":"/dubbo/saveComplexBeanTest","parameterTypes":"org.apache.shenyu.examples.dubbo.api.entity.ComplexBeanTest","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:36.418 INFO 22984 --- [or_consumer_-27] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByArrayIdsAndName","pathDesc":"findByArrayIdsAndName","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByArrayIdsAndName","ruleName":"/dubbo/findByArrayIdsAndName","parameterTypes":"[Ljava.lang.Integer;,java.lang.String","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
2021-07-05 14:11:36.511 INFO 22984 --- [or_consumer_-28] o.a.s.r.client.http.utils.RegisterUtils : dubbo client register success: {"appName":"dubbo","contextPath":"/dubbo","path":"/dubbo/findByStringArray","pathDesc":"findByStringArray","rpcType":"dubbo","serviceName":"org.apache.shenyu.examples.dubbo.api.service.DubboMultiParamService","methodName":"findByStringArray","ruleName":"/dubbo/findByStringArray","parameterTypes":"[Ljava.lang.String;","rpcExt":"{\"loadbalance\":\"random\",\"retries\":2,\"timeout\":10000}","enabled":true,"host":"172.16.1.161","port":20888,"registerMetaData":false}
这个纯粹提供dubbo 服务的。
开启dubbo 插件使用
- 在 shenyu-admin 插件管理中,把dubbo 插件设置为开启。
- 在 dubbo 插件中配置你的注册地址,或者其他注册中心的地址。
测试
使用postman来测试提供的dubbo服务:
比如测试 /findById
@Service("dubboTestService")
public class DubboTestServiceImpl implements DubboTestService {
@Override
@ShenyuDubboClient(path = "/findById", desc = "Query by Id")
public DubboTest findById(final String id) {
DubboTest dubboTest = new DubboTest();
dubboTest.setId(id);
dubboTest.setName("hello world shenyu Apache, findById");
return dubboTest;
}
.....
}
看清楚我们测试的地址 http://localhost:9195/dubbo/findById?id=1
是网关的地址,是网关帮我们访问的dubbo 服务。