能否让Enterprise Integrator中REST API的重定向链接可配置/动态化?
实现WSO2 EI重定向URL的动态配置方案
针对你在WSO2 Enterprise Integrator中部署的REST API,要实现重定向URL的可配置化,避免每次修改代码重新部署,这里提供几种实用方案:
方案一:使用本地条目(Local Entry)
这是最便捷的方式,本地条目可在EI管理控制台直接修改,无需重启或重部署API。
步骤1:创建本地条目
在EI控制台的配置 > 本地条目中,创建两个本地条目:
- 名称:
RedirectURL_Exists,值:https://wso2.com/(用户存在时的重定向地址) - 名称:
RedirectURL_NotExists,值:https://www.youtube.com/(用户不存在时的重定向地址)
步骤2:修改API配置
将原API中硬编码的URL替换为读取本地条目的表达式:
<api xmlns="http://ws.apache.org/ns/synapse" name="DBLookupAPI" context="/dblookup"> <resource methods="GET" uri-template="/{UserCode}"> <inSequence> <log level="custom"> <property name="Value" expression="get-property('uri.var.UserCode')"/> </log> <dblookup> <connection> <pool> <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver> <url>jdbc:sqlserver://10.1.1.111\test;databaseName=UserDB</url> <user>admin</user> <password>admin</password> </pool> </connection> <statement> <sql>select UserCode from UserDB.dbo.Users where UserCode =?;</sql> <parameter expression="get-property('uri.var.UserCode')" type="CHAR"/> <result name="foundnr" column="UserCode"/> </statement> </dblookup> <log level="custom"> <property name="Value" expression="get-property('foundnr')"/> </log> <filter source="boolean(get-property('foundnr'))" regex="true"> <then> <log> <property name="Message" value="Name Exists Lets redirect"/> </log> <property name="HTTP_SC" value="302"/> <!-- 读取用户存在时的重定向URL --> <property name="Location" expression="get-property('RedirectURL_Exists')" scope="transport"/> </then> <else> <log> <property name="Message" value="Name Does Not Exist Lets redirect"/> </log> <property name="HTTP_SC" value="302"/> <!-- 读取用户不存在时的重定向URL --> <property name="Location" expression="get-property('RedirectURL_NotExists')" scope="transport"/> </else> </filter> <respond/> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
之后要修改重定向地址,直接在控制台更新对应本地条目即可,无需改动API代码。
方案二:使用EI注册表存储配置
如果需要对配置进行版本管理或集中存储,可以将URL放在EI注册表中:
步骤1:在注册表中创建配置资源
在EI控制台的注册表 > 配置注册表中,创建两个资源:
- 路径:
/_system/config/redirect/exists_url,内容:https://wso2.com/ - 路径:
/_system/config/redirect/not_exists_url,内容:https://www.youtube.com/
步骤2:修改API中的Location属性
将表达式改为读取注册表资源:
<!-- 用户存在时的重定向URL --> <property name="Location" expression="get-property('registry', 'conf:/redirect/exists_url')" scope="transport"/> <!-- 用户不存在时的重定向URL --> <property name="Location" expression="get-property('registry', 'conf:/redirect/not_exists_url')" scope="transport"/>
方案三:从数据库读取重定向配置
如果需要更灵活的动态配置,甚至可以将重定向URL存储在数据库中,比如新增一个RedirectConfig表:
CREATE TABLE RedirectConfig ( ConfigKey VARCHAR(50) PRIMARY KEY, ConfigValue VARCHAR(255) NOT NULL ); INSERT INTO RedirectConfig VALUES ('USER_EXISTS_URL', 'https://wso2.com/'); INSERT INTO RedirectConfig VALUES ('USER_NOT_EXISTS_URL', 'https://www.youtube.com/');
然后在API的dblookup中介之后,新增一个dblookup查询配置表,获取对应的URL:
<!-- 查询用户存在时的重定向URL --> <dblookup> <connection> <pool> <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver> <url>jdbc:sqlserver://10.1.1.111\test;databaseName=UserDB</url> <user>admin</user> <password>admin</password> </pool> </connection> <statement> <sql>select ConfigValue from UserDB.dbo.RedirectConfig where ConfigKey = 'USER_EXISTS_URL';</sql> <result name="redirect_exists_url" column="ConfigValue"/> </statement> </dblookup> <!-- 查询用户不存在时的重定向URL --> <dblookup> <connection> <pool> <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver> <url>jdbc:sqlserver://10.1.1.111\test;databaseName=UserDB</url> <user>admin</user> <password>admin</password> </pool> </connection> <statement> <sql>select ConfigValue from UserDB.dbo.RedirectConfig where ConfigKey = 'USER_NOT_EXISTS_URL';</sql> <result name="redirect_not_exists_url" column="ConfigValue"/> </statement> </dblookup>
之后在filter的then和else块中,使用这些查询到的属性:
<property name="Location" expression="get-property('redirect_exists_url')" scope="transport"/> <!-- 和 --> <property name="Location" expression="get-property('redirect_not_exists_url')" scope="transport"/>
内容的提问来源于stack exchange,提问作者Kelisha Govender
相关产品推荐
相关产品推荐

