项目介绍
ShenYu是由 ShenYu Admin和 ShenYu Bootstrap 两块组成。
ShenYu Admin是网关的后台管理系统,能够可视化管理所有插件、选择器和规则,设置用户、角色,控制资源。
ShenYu Bootstrap这部分才是网关核心部分,引入各个插件包。官方给出的一个网关demo,你也可以自己取其他名字作为网关羡慕,引入自己需要的插件包,以及插件的拓展。
我选择网关:ShenYu
,[注册中心]/配置中心:nacos
,微服务框架:dubbo
。
下载源码
git config --global core.longpaths true
项目地址:https://github.com/apache/shenyu
启动Admin
- 修改存储方式,修改
application.yml
,使用mysql
spring:
profiles:
active: mysql
- 修改同步方式
shenyu:
sync:
nacos:
url: localhost:8848
namespace: public
- 修改
application-mysql.yml
配置信息,如果数据库表没同步,手动同步。数据库表在schema.sql
spring:
datasource:
url: jdbc:mysql://localhost:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
- 运行
ShenyuAdminBootstrap
- 访问
http://localhost:9095/
,admin/123456
登录 - 设置
dubbo
插件,设置注册方式nacos://localhost:8848?namespace=public
启动bootstrap
- 修改配置
application.yml
shenyu:
sync:
nacos:
url: localhost:8848
namespace: public
- 引入nacos
<!-- apache shenyu data sync start use nacos-->
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-sync-data-nacos</artifactId>
<version>${project.version}</version>
</dependency>
- 启动
ShenyuBootstrapApplication
- 网关接口地址:
http://localhost:9195/
注册服务到网关
- 打开
shenyu-examples
- 选择
shenyu-examples-apache-dubbo-service-annotation
,主要修改注册的配置为nacos,以及dubbo注册中心同步改为nacos即可
shenyu:
register:
registerType: nacos #zookeeper #etcd #nacos #consul
serverLists: localhost:8848 #localhost:2181 #http://localhost:2379 #localhost:8848
props:
username: admin
password: 123456
client:
dubbo:
props:
contextPath: /dubbo
appName: dubbo
dubbo:
application:
name: test-dubbo-service
registry:
address: nacos://127.0.0.1:8848
protocol:
name: dubbo
port: 20888
scan:
base-packages: org.apache.shenyu.examples.apache.dubbo.service.annotation.impl
- 启动
TestApacheDubboAnnotationApplication
访问
在Admin中可以看到注册的接口信息。PluginList->Proxy-Dubbo
随意访问一个接口即可看到返回结果,则表示网关正常使用。http://localhost:9195/dubbo/findAll
返回结果:
{"name":"hello world shenyu Apache, findAll","id":"1729901691"}