目标
- 演示soul网关HTTP插件的使用
- 分析soul插件的整体工作流程,
1. HTTP插件的使用
1.1 在soul-bootstrap
项目中引入如下插件,然后重新启动soul网关,启动步骤参见 soul源码分析_0_阅读源码准备与soul基础
<!--if you use http proxy start this-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-divide</artifactId>
<version>${last.version}</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId>
<version>${last.version}</version>
</dependency>
1.2 将soul-client接入到你的项目中,此处以spring-boot
项目为例给下配置方式:
- (1)在你的项目中添加
soul-spring-boot-starter-client-springmvc
依赖:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-client-springmvc</artifactId>
<version>${last.version}</version>
</dependency>
- (2)在你的项目文件中添加如下配置(以
YAML
格式为例)
soul:
http:
adminUrl: http://localhost:9095
port: 你本项目的启动端口
contextPath: /http
appName: http
full: false
# adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加http://
# port: 你本项目的启动端口
# contextPath: 为你的这个mvc项目在soul网关的路由前缀,比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
# appName:你的应用名称,不配置的话,会默认取 `spring.application.name` 的值
# full: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
SpringMVC的接入方式参见官方文档:https://dromara.org/zh-cn/docs/soul/user-http.html
-
(3)在你的
controller
的接口上加上@SoulSpringMvcClient
注解。- 可以把注解加到
Controller
类上面,里面的path属性则为前缀,如果含有/**
代表你的整个接口需要被网关代理。
此处可以参考官方示例
soul-examples-http
中的代码。简单起见本文也将以
soul-examples-http
作为后端应用。 - 可以把注解加到
运行soul-examples-http
以soul-examples-http
作为示例。在上述工作准备完毕后,检查下soul-admin
管理端页面中的System Manage
->Plugin
中divide
插件状态、保证为开启状态,
然后启动soul-examples-http
,会发现后端程序启动时将接口注册到了soul
中,参考下面日志:
......