|
| 1 | +package we.plugin.pathcache; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.springframework.context.annotation.Configuration; |
| 8 | + |
| 9 | +import we.config.ManualApiConfig; |
| 10 | +import we.plugin.PluginConfig; |
| 11 | +import we.plugin.auth.ApiConfig; |
| 12 | +import we.plugin.requestbody.RequestBodyPlugin; |
| 13 | + |
| 14 | +/** |
| 15 | + * 定义 DemoApiConfig 继承 ManualApiConfig,并注解为 Configuration,然后实现 setApiConfigs |
| 16 | + * 方法,在方法中添加路由配置; 本类仅为方便开发和测试,正式环境应该通过管理后台配置路由 |
| 17 | + */ |
| 18 | +@Configuration |
| 19 | +public class PathCacheConfig extends ManualApiConfig { |
| 20 | + |
| 21 | + @Override |
| 22 | + public List<ApiConfig> setApiConfigs() { |
| 23 | + |
| 24 | + List<ApiConfig> apiConfigs = new ArrayList<>(); |
| 25 | + |
| 26 | + // 一个路由配置 |
| 27 | + ApiConfig ac = new ApiConfig(); |
| 28 | + // 路由 id,建议从 1000 开始 |
| 29 | + ac.id = 1000; |
| 30 | + // 前端服务名 |
| 31 | + ac.service = "cache"; |
| 32 | + // 前端路径 |
| 33 | + ac.path = "/test"; |
| 34 | + // 路由类型,此处为反向代理 |
| 35 | + ac.type = ApiConfig.Type.REVERSE_PROXY; |
| 36 | + // 被代理接口的地址 |
| 37 | + ac.httpHostPorts = Collections.singletonList("http://10.100.113.28:9090"); |
| 38 | + // 被代理接口的路径 |
| 39 | + ac.backendPath = "/web/check"; |
| 40 | + ac.pluginConfigs = new ArrayList<>(); |
| 41 | + |
| 42 | + // 如果你的插件需要访问请求体,则首先要把 RequestBodyPlugin.REQUEST_BODY_PLUGIN 加到 ac.pluginConfigs |
| 43 | + // 中,就像下面这样 |
| 44 | + PluginConfig pc1 = new PluginConfig(); |
| 45 | + pc1.plugin = RequestBodyPlugin.REQUEST_BODY_PLUGIN; |
| 46 | + ac.pluginConfigs.add(pc1); |
| 47 | + |
| 48 | + PluginConfig pc2 = new PluginConfig(); |
| 49 | + pc2.plugin = PathCacheFilter.PATH_CACHE_ID; // 应用 id 为 demoPlugin 的插件 |
| 50 | + ac.pluginConfigs.add(pc2); |
| 51 | + |
| 52 | + apiConfigs.add(ac); |
| 53 | + |
| 54 | + log.info("set api configs end"); |
| 55 | + return apiConfigs; // 返回路由配置 |
| 56 | + } |
| 57 | +} |
0 commit comments