java 服务屏蔽开关系统,可以手工降级服务,关闭服务 基于spring AOP机制,可以在特殊情况下屏蔽相关service类的某些返回,并且支持定义默认返回结果,随机屏蔽某些异常服务。 通过启动一个内置的http server来监听外部指令。
对当前应用的影响。代码请查看 https://github.com/zhwj184/autoswitch
使用指南:
1.在spring配置文件中添加如下,其中switch-service-pointcut是添加紧急情况下需要屏蔽的方法列表
<aop:config proxy-target-class="true"></aop:config> <bean id="switchInteceptor" class="org.autoswitch.SwitchInteceptor"> </bean> <bean id="switch-service-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="patterns"> <list> <value>org.autoswitch.test.*</value> </list> </property> </bean> <aop:config> <aop:advisor advice-ref="switchInteceptor" pointcut-ref="switch-service-pointcut"/> </aop:config> <bean id="wwitchControlHttpServer" class="org.autoswitch.SwitchControlHttpServer" init-method="init"></bean> <bean id="testService" class="org.autoswitch.test.TestServiceImpl" /> <bean id="testController" class="org.autoswitch.test.TestController" />
例如下面的service,上面注释分别是在应用启动后手工屏蔽该服务调用,以后每次调用直接用参数的jsonResult反序列后返回, classmethod是具体到某个方法名称,status为open关闭该服务,close表示重新打开服务,jsonResult是mock返回结果的json串, 如果是基本类型,则必须用ret作为key,其他list,bean之类的就直接用json串,type表示如果list有泛型的话则是返回的类完整类型;
public class TestServiceImpl implements TestService{ //http://localhost:8080/control/a.htm?classmethod=org.autoswitch.test.TestServiceImpl.hello&status=open&jsonResult=1 public void hello(){ System.out.println("hello"); } //http://localhost:8080/control/a.htm?classmethod=org.autoswitch.test.TestServiceImpl.sayHello&status=open&jsonResult={ret:%22goodbuy%22} public String sayHello(){ return "sayHello"; } //http://localhost:8080/control/a.htm?classmethod=org.autoswitch.test.TestServiceImpl.getNames&status=open&jsonResult=[{"catList":[],"id":1,"name":"aaa"},{"catList":[],"id":1,"name":"aaa"},{"catList":[],"id":1,"name":"aaa"}]&type=org.autoswitch.test.TestBean public List<TestBean> getNames(){ return null; } // http://localhost:8080/control/a.htm?classmethod=org.autoswitch.test.TestServiceImpl.getBeans&status=open&jsonResult={"catList":["123","456","789"],"id":1,"name":"aaa"} public TestBean getBeans(){ return null; }}
3调用示例代码
public class MainTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-bean.xml"); TestService testControl = (TestService) context.getBean("testService"); try{ testControl.hello(); System.out.println(testControl.sayHello()); List<TestBean> list = testControl.getNames(); for(TestBean bean: list){ System.out.println(bean.getId() + bean.getName() + bean.getCatList()); } TestBean bean = testControl.getBeans(); System.out.println(bean.getId() + bean.getName() + bean.getCatList()); }catch(Exception e){} for(int i = 0; i < 10; i++){ try{ // testControl.hello(); System.out.println(testControl.sayHello()); // List<TestBean> list = testControl.getNames(); // for(TestBean bean: list){ // System.out.println(bean.getId() + bean.getName() + bean.getCatList()); // } // TestBean bean = testControl.getBeans(); // System.out.println(bean.getId() + bean.getName() + bean.getCatList()); }catch(Exception e){ e.printStackTrace(); } } } }
4.输出
Listening on port 8080 hello sayHello Incoming connection from /127.0.0.1 New connection thread goodbuy goodbuy Incoming connection from /127.0.0.1 New connection thread sayHello sayHello sayHello sayHello sayHello sayHello sayHello sayHello
这里只是提供一种示例,如果要在生产环境中使用,则需要对并发控制,返回结果的序列化,方法名称一致参数不一致等各种情况进行控制, 同时还需要对权限,后台管理系统等可以做优化。
思路比较简单,就是使用spring 的aop机制。
public class SwitchInteceptor implements MethodInterceptor{ @Override public Object invoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); String classMethod = method.getDeclaringClass().getName() + "." + method.getName(); if(ClassMethodStatusManager.getInstance().isOpen(classMethod)){ Class retClass = method.getReturnType(); if(retClass.getName().equals("void")){ return 1; } Object obj = SimpleReturnObjectComplier.getRetInstance(classMethod, retClass); return obj; } return invocation.proceed(); } }
相关推荐
微服务熔断及服务降级方案
华为E1308降级服务器
基于Java开发的简单、易用、高性能的服务降级系统,支持限流、熔断和降级等功能,服务端必备!!
分布式的设计以及hystrix的一些Q&A, 其中如果有其他的第三方工具再进行补充
软件界面再往下会看到“系统”菜单,点进去会看到“安装固件更新”,继续点击进去会看到“允许固件降级”开关,打开那个开关,上面会弹出不同时间的固件版本信息,找到版本信息末尾编号为AUA1的固件选中,然后右下角...
断路由出现的原因是: 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC)。为了保证其高可用,单个服务又必须集群部署。...为了解决这个问题,就出现断路器模型,把服务进行降级处理。
固件降级用于取得telnet权限,解决新版固件关闭telnet的问题。
vivo机型售后系统降级工具 售后工具 vivo机型专用 专门为系统降级用的软件 需要的友友可以下载使用
中兴B860AV1.1-T2 降级包。ADB二维码 ,无法用计算器算ADB密码 ,TTL锁死, 唯一降级包, 唯一降级包, 唯一降级包
springcloud feign整合hystrix 示例,希望对大家有所帮助
PSP降级工具 著名黑客someone发布了6.60官方系统降级程序6xx_downgrader_v4,该降级程序支持PSP1000、2000、3000和PSPgo的主板为01G,02G,03G,04G,05G,07G,09G。 6.XX Downgrader是一款给官方系统的PSP进行降级的...
SpringCloud(Hystrix服务降级)
springcloud熔断降级组件hystrix详解代码练习,由于平台上传限制,所以分为了两个部分上传。此为第一部分。 第二部分:https://download.csdn.net/download/keenstyle/12232210
联想工程师专用小工具 网卡驱动降级工具V3.30.1联想工程师专用小工具 网卡驱动降级工具V3.30.1联想工程师专用小工具 网卡驱动降级工具V3.30.1联想工程师专用小工具 网卡驱动降级工具V3.30.1联想工程师专用小工具 ...
华为安卓降级
互联网高并发解决方案-基于Hystrix实现服务隔离与降级互联网高并发解决方案-基于Hystrix实现服务隔离与降级互联网高并发解决方案-基于Hystrix实现服务隔离与降级互联网高并发解决方案-基于Hystrix实现服务隔离与降级...
高并发下的服务降级、限流实战 基于分布式架构下分布式锁的解决方案实战 分布式架构实现分布式定时调度 分布式架构-中间件 分布式消息通信 消息中间件在分布式架构中的应用 ActiveMQ ActiveMQ高可用集群企业...
计算机后端-Java-Java高并发从入门到面试教程-务降级与服.zip
K2降级为XP系统后,由于系统内多个K2使用了同一镜像文件,会导致Control Team网络不通,进而影响该播控正常连接以及服务器的迁移,同时还会引起迁移等软件对其他服务器存储状态的检查错误。解决该问题的办法为重新对...
37_基于dubbo如何做服务治理、服务降级以及重试?