基于ShenYu的专业网关开发
网关开发模式选型参考建议请看上文《网关开发模式简介》。
网关模块开发
安装相关盘古模块
- 盘古Parent
- 基础模块
- Dubbo模块
- 网关模块
<parent>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-parent</artifactId>
<version>latest.version.xxx</version>
<relativePath/>
</parent>
<dependency>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.gitee.pulanos.pangu</groupId>
<artifactId>pangu-gateway-spring-boot-starter</artifactId>
</dependency>
tip
基于ShenYu的网关开发模式不需要引入服务接口依赖包,网关会根据服务接口的元数据信息,泛化调用Dubbo服务接口。服务接口的元数据信息则根据Dubbo服务应用中的配置自动上传到ShenYu网关管理系统。此内容在下文会继续讲解。
本地配置
为便于理解,本文基于本地配置的方式编写。若改为标准的Nacos配置中心模式,请参阅:配置中心章节。
- application.yaml
- application-dev.yaml
spring:
profiles:
active: ${spring.profiles.active:dev}
server:
port: 9090
spring:
main:
allow-bean-definition-overriding: true
shenyu:
cross:
enabled: true
allowedHeaders:
allowedMethods: "*"
allowedOrigin: "*"
allowedExpose: "*"
maxAge: "18000"
allowCredentials: true
switchConfig:
local: true
file:
enabled: true
maxSize : 10
sync:
websocket:
urls: ${shenyu.websocket.urls:ws://localhost:9095/websocket}
dubbo:
parameter: multi
exclude:
enabled: false
paths:
- /favicon.ico
extPlugin:
path:
enabled: true
threads: 1
scheduleTime: 300
scheduleDelay: 30
scheduler:
enabled: false
type: fixed
threads: 16
upstreamCheck:
enabled: false
timeout: 3000
healthyThreshold: 1
unhealthyThreshold: 1
interval: 5000
printEnabled: true
printInterval: 60000
logging:
level:
root: info
org.springframework.boot: info
org.apache.ibatis: info
org.apache.shenyu: info
关键配置项说明
配置项 | 配置说明 |
---|---|
shenyu.sync.websocket.urls | 表示网关和ShenYu Admin之间使用Websocket的方式进行数据同步,这里是配置ShenYu Admin的提供的Websocket数据同步服务的地址(支持集群,逗号分割)。 |
caution
上表中提到到ShenYu Admin是ShenYu网关框架的配置&元数据管理后台。这里包含了网关模块自己的配置信息也包含了后台服务接口原数据信息,这理的配置信息和元数据信息需要和网关模块同步。ShenYu支持多种数据同步方案,Websocket只是盘古开发选用的一种缺省方案。关于网关的更多信息,请参阅:使用指南->网关系统章节。
调用微服务接口
基于ShenYu的网关开发模式既不需要引入服务接口JAR,也不需要编写具体的调用代码。完全由网关根据服务接口元数据进行Dubbo泛化调用。
那么问题来了,网关是如何知道Dubbo服务接口元数据的呢?
- 通过ShenYu Admin后台系统,『基础配置->元数据管理』菜单,手工新增每一个接口的元数据,然后将数据自动同步到网关模块。(不建议,量大的话太繁琐)
- 对Dubbo服务提供者增加ShenYu Client支持,通过在接口方法上使用注解
@ShenyuDubboClient
来自动采集上传接口元数据到ShenYu Admin,然后将数据自动同步到网关模块。(盘古开发建议使用的标准姿势)如何对Dubbo服务提供者增加ShenYu Client支持请参阅下文的『Dubbo服务端(网关采集接口元数据)』章节。
还有一个问题,网关要实现泛化调用是需要dubbo服务的注册中心的?
- 没错。通过ShenYu Admin后台系统『基础配置->插件管理』菜单,启用
dubbo插件
并填入注册中心地址。比如,我测试用的注册中心地址:nacos://169.188.88.140:1688?namespace=pangu-dev
。如下图所示。
启动入口
@SpringBootApplication
public class WebApiGatewayApplication {
public static void main(String[] args) {
PanGuApplicationBuilder.init(WebApiGatewayApplication.class).run(args);
}
}
Dubbo服务端(网关采集接口元数据)
网关需要对其代理的后端Dubbo服务进行泛化调用,就必须知道服务接口元数据。下文就将对一个普通Dubbo服务应用增加ShenYu Client支持完成自动采集服务接口元数据功能。
安装ShenYu Client依赖包
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-client-apache-dubbo</artifactId>
</dependency>
增加配置项
shenyu.client.register-type=http
shenyu.client.server-lists=${shenyu.server-lists:http://localhost:9095}
shenyu.client.props.contextPath=/dubbo
配置项 | 配置说明 |
---|---|
shenyu.client.register-type | 服务接口元数据采集方式,可选http直连模式或配置中心zookeeper、etcd、consul和nacos。盘古开发采集接口元数据缺省选择http直接ShenYu Admin的方式。 |
shenyu.client.server-lists | ShenYu Admin地址或配置中心地址。集群时多个地址用逗号分开。 |
shenyu.client.props.contextPath | 本服务在网关中的路由前缀,可自定义按需配置 |
自动上报服务接口元数据
在Dubbo服务实现类的方法上使用注解@ShenyuDubboClient
标记,表示该接口方法元数据自动上传到ShenYu Admin。如下代码所示。
@Service(version = "1.0.0", group = "pangu-examples-dubbo-gateway-service")
public class UserServiceImpl implements UserService {
@Override
@ShenyuDubboClient(path = "/findUserEntity", desc = "查询用户信息")
public UserEntity findUserEntity(Long id) {
...
return userEntity;
}
@Override
@ShenyuDubboClient(path = "/listUserOuts", desc = "查询用户信息列表")
public List<UserOut> listUserOuts(UserIn userIn) {
...
return userOuts;
}
}
重启服务提供者:
- 进入ShenYu Admin后台系统的『基础配置->元数据管理』菜单,会看到自动上报的服务元数据信息。如下图所示。
- 进入ShenYu Admin后台系统的『插件列表->proxy->dubbo』菜单,会看到自动注册的选择器和规则信息。如下图所示。
完整请求URL及参数说明
至此,就可以通过http的方式来请求我们的的dubbo服务了。
请求URL
如何确定dubbo服务对应的请求url地址呢?
由网关模块配置文件可知网关应用端口是9090
,dubbo服务的配置项shenyu.client.props.contextPath为/dubbo
,我们的dubbo方法通过@ShenyuDubboClient标记的path属性为:/findUserEntity
。综上,dubbo服务UserService#findUserEntity的完整请求地址为:http://localhost:9090/dubbo/findUserEntity
参数说明
通过http协议,post方式访问网关。在http body中传入json格式的参数。
请求示例
curl --location --request POST 'http://127.0.0.1:9090/dubbo/findUserEntity' \
--header 'Content-Type: application/json' \
--data '{id=1}'
{
"code": 200,
"message": "Access to success!",
"data": {
"name": "云南码农大熊",
"id": 1,
"userType": 2,
}
}
本文相关范例源码
- pangu-examples-dubbo-api:服务接口包
- pangu-examples-dubbo-gateway-service:服务提供者(支持网关)
- pangu-examples-webapi-gateway-based:服务消费者(基于ShenYu的网关模块)
下一步
继续阅读其它章节获取您想要的答案或通过我们的开发者社区寻求更多帮助。