作者:mobiledu2502876223 | 来源:互联网 | 2023-09-16 14:43
为什么要做请求复制?业务场景:微信公众号限制申请个数,现在一个公众号要是dev、int、uat、prod环境公用的,所有当有微信公众号回调场景时,只能在让微信公众号回调到p
为什么要做请求复制?
业务场景:微信公众号限制申请个数,现在一个公众号要是 dev、int、uat、prod 环境公用的,所有当有微信公众号回调场景时,只能在让微信公众号回调到 prod 环境,再由prod 环境把请求复制后转到其他环境。
1 获取运行环境
1.1 测试代码
编写一个类,从Spring 上下文中,读取ActiveProfiles
@Component
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {
private static ApplicationContext applicatiOnContext= null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void destroy() throws Exception {
applicationContext = null;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextHolder.applicationContext = applicationContext;
}
/**
* 获取当前环境
*/
public static String getActiveProfile() {
return applicationContext.getEnvironment().getActiveProfiles()[0];
}
}
启动类
@SpringBootApplication
public class RunApplication {
public static void main(String[] args) {
SpringApplication.run(RunApplication.class, args);
}
}
1.2 设置Active Profiles
要完成上面的业务需求,我们首先要知道当前的运行环境。提前是我们要设置 Active Profiles 参数,这样才能在 SpringContextHolder 类中的 getActiveProfile() 方法来获取到。
1.2.1 Idea 设置 Active Profiles 方式
![](https://img3.php1.cn/3cdc5/64c2/8fd/23caa513b6fac814.png)
启动 RunApplication 时,运行日志如下:
2022-05-20 12:19:33.261 INFO 7204 --- [ main] com.**.RunApplication : The following profiles are active: dev
1.2.2 JVM启动命令行方式
在使用JVM命令启动时候,可以添加如下命令来设置Active Profiles:-Dspring.profiles.active=dev
如果还是在Idea启动是测试,可以在 Environment 设置下里添加JVM命令。
![](https://img3.php1.cn/3cdc5/64c2/8fd/e2268305bd9af621.png)
启动 RunApplication 时,运行日志如下:
2022-05-20 12:19:33.261 INFO 7204 --- [ main] com.**.RunApplication : The following profiles are active: dev
1.2.3 测试用例设置 Active Profiles
如果使用SpringBootTest方式测试我们的代码,需要设置Active Profiles,可以使用 @ActiveProfiles 注解进行设置。
@SpringBootTest
@ActiveProfiles(value = "dev")
@RunWith(SpringRunner.class)
public class SpringServiceTest {
@Test
public void test() {
String activeProfile = SpringContextHolder.getActiveProfile();
System.out.println("activeProfile == " + activeProfile);
}
}
测试用例运行日志如下:
2022-05-20 17:26:14.773 INFO 936 --- [ main] com.**.test.SpringServiceTest : The following profiles are active: dev
......
activeProfile == dev
2 请求转发的拦截器
2.1 定义拦截器
我们使用AOP的方式,定义一个Intercepter,需要实现 HandlerInterceptor 接口。用来复制请求的参数,并且转发请求到其他环境的地址去。
@Component
public class WeChatRequestInterceptor implements HandlerInterceptor {
public static ThreadLocal