SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!

最近项目组有个新需求:disable掉某个接口。我当时第一个思路就是用Spring的AOP做切面拦截。但我的香港技术leader却抛给我Togglz,让我拿Togglz去做disable,然后第二天开始他接近一个月的带薪长假。。。

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片

但毕竟到了“可以打我骂我但不能扣我工资”的年纪,我开始着手玩这Togglz的东东(转载请注明出处: Michael孟良

Togglz的资料很少,找到了像点人话的简介:如果一个story在当前迭代中无法完成,那样需要给它加上toggle, 这样只需在生产环境将toggle关闭,不为担心未完成的功能被release出去;另一方面,如果发现新的实现、体验不被欢迎,那么只需将toggle关闭就可以快速地返回到旧的实现了。为了满足上面的需求,我们选择了 togglz。
下面我在一个简单的SpringBoot上做一个AOP和一个Togglz:
首先做一个清纯的SpringBoot:

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
SpringBoot
MyApplication.java是启动类

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }}

MainController.java是controller,接口入口
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MainController {@Autowired private MyService myService; @RequestMapping("/") @ResponseBody String home() throws Exception { System.out.println("springboot项目启动成功!"); return myService.addSalary(); } }

MyService.java是service端,里面有个addSalary()的方法
import org.springframework.stereotype.Service; @Service public class MyService { public String addSalary() throws Exception { System.out.println("加人工!"); return "加人工!"; } }

点击:http://localhost:8080/springboot/

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片

下面AOP和Togglz的demo都是对上面的addSalary()进行disable。
基于上面SpringBoot加上AOP:

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
Sring-AOP
MyAnnotation.java:自定义注解
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation { String name(); }

MyAspect.java:自定义切面
import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect // 1:通过改注解声明一个切面 @Component // 2让切面称为spring管理的bean public class MyAspect { //切面 @Around("@annotation(com.m.springboot.aop.MyAnnotation)") //6:直接拦截方法名 public Objectbefore(ProceedingJoinPoint joinPoint) throws Throwable{ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截,"+method.getName()); return null; //return joinPoint.proceed(); } }

其他方法不变,只需要在MyService的addSalary()上加一个@MyAnnotation
@MyAnnotation(name = "addSalary!") public String addSalary() throws Exception { System.out.println("加人工!"); return "加人工!"; }

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
成功拦截
OK。成功拦截。
下面我们来看看在原来的SpringBoot上加上Togglz:

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
Togglz
MyFeatures.java:对象的特征
import org.togglz.core.Feature; import org.togglz.core.annotation.Label; import org.togglz.core.context.FeatureContext; public enum MyFeatures implements Feature {@Label("Employee Management Feature") EMPLOYEE_MANAGEMENT_FEATURE; public boolean isActive() { return FeatureContext.getFeatureManager().isActive(this); }}

ToggleConfiguration.java: Togglz的配置,这里主要将上面写的MyFeatures注入到配置中
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.togglz.core.manager.EnumBasedFeatureProvider; import org.togglz.core.spi.FeatureProvider; @Configuration public class ToggleConfiguration { @Bean public FeatureProvider featureProvider() { return new EnumBasedFeatureProvider(MyFeatures.class); } }

在yml上加多:
togglz: features: EMPLOYEE_MANAGEMENT_FEATURE: enabled: false

这里的意思是将之前的设置的开关EMPLOYEE_MANAGEMENT_FEATURE,我们默认是关的。
而在之前的addSalary()我们这样写:
public String addSalary() throws Exception { if (MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE.isActive()) { System.out.println("加人工!"); return "加人工!"; }else { return null; } }

不要觉得土,我们隔壁村的同事确实这样玩的(允悲.jpg)
当然,最后后台只打印了“springboot启动!”说明也是成功的。
.


笔者写完后沉思怎么优化,两种技术能不能相互结合?尝试后答案是:
SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
完全OJBK

项目结构:
SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
SpringAOP整合Togglz
代码由上往下看:
MyAnnotation.java:
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import com.m.springboot.togglz.MyFeatures; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MyAnnotation { MyFeatures value(); }

MyAspect.java:
import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import com.m.springboot.togglz.MyFeatures; @Aspect // 1:通过改注解声明一个切面 @Component // 2让切面称为spring管理的bean public class MyAspect { //切面 @Around("@annotation(com.m.springboot.aop.MyAnnotation)") //6:直接拦截方法名 public Objectbefore(ProceedingJoinPoint joinPoint) throws Throwable{ if (MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE.isActive()) { return joinPoint.proceed(); }else { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); System.out.println("方法规则式拦截,"+method.getName()); return "方法规则式拦截,"+method.getName(); } } }

MyFeatures.java:
import org.togglz.core.Feature; import org.togglz.core.annotation.Label; import org.togglz.core.context.FeatureContext; public enum MyFeatures implements Feature {@Label("Employee Management Feature") EMPLOYEE_MANAGEMENT_FEATURE; public boolean isActive() { return FeatureContext.getFeatureManager().isActive(this); }}

ToggleConfiguration.java:
import java.io.File;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.togglz.core.Feature; import org.togglz.core.manager.EnumBasedFeatureProvider; import org.togglz.core.manager.TogglzConfig; import org.togglz.core.repository.StateRepository; import org.togglz.core.repository.file.FileBasedStateRepository; import org.togglz.core.spi.FeatureProvider; import org.togglz.core.user.UserProvider; import org.togglz.servlet.user.ServletUserProvider; @Configuration public class ToggleConfiguration { @Bean public FeatureProvider featureProvider() { return new EnumBasedFeatureProvider(MyFeatures.class); } }

MyService.java:
import org.springframework.stereotype.Service; import com.m.springboot.aop.MyAnnotation; import com.m.springboot.togglz.MyFeatures; @Service public class MyService {@MyAnnotation(value = https://www.it610.com/article/MyFeatures.EMPLOYEE_MANAGEMENT_FEATURE) public String addSalary() throws Exception { System.out.println("加人工!"); return "加人工!"; } }

当application.yml:
server: port: 8080 togglz: features: EMPLOYEE_MANAGEMENT_FEATURE: enabled: true

console:
springboot启动!
加人工!
当application.yml:
server: port: 8080 togglz: features: EMPLOYEE_MANAGEMENT_FEATURE: enabled: false

console:
springboot启动!
方法规则式拦截,addSalary
但如果想更加方便,不用改配置文件、再重启服务器这么麻烦,可以再配置一个togglz的后台 SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
togglz后台
只要按这个按钮,就能在生产环境控制你想要控制的功能,你的周末健身时光不再被打扰!!!
PS:这里有个大坑:

SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!
文章图片
yml文件追加 上面的yml我项目根本读不了,后来我在启动类(MyApplication.java)里面重新加载TogglzConsoleServlet才有反应。好了有反应了,又说我没权限,MD,唯有往togglz的底层慢慢读,后来读出它控制权限的一部分:
servletContext.getInitParameter("org.togglz.console.SECURED")

于是我遍在启动类里面顺便设置它为false 。OK
@Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addServlet("togglzConsoleServlet","org.togglz.console.TogglzConsoleServlet").addMapping("/togglz/*"); servletContext.setInitParameter("org.togglz.console.SECURED","false"); }

【SpringAOP整合Togglz!你的周末健身时光不再被打扰!!!】OK,So far so good。SpringAOP和Togglz完美整合,后台控制是我做demo时加上去的,项目暂时没有放上去,如果需要了可能要在我togglz后台的权限上再下点功夫。下面是所有代码下载地址:
https://github.com/MichaelYipInGitHub/Spring-AOP-Togglz
谢谢观看~

    推荐阅读