Business services often include multiple systems and feature modules. To make these services dynamic, backend programs use switches to control logic. This lets you change the logic while the system is running. This topic describes how to add a feature switch.
Prerequisites
You have connected a new application. For more information, see Connect using an SDK and Connect using a Spring Boot Starter.
Connect using the Java SDK
For applications connected using the Java SDK, follow these steps to add a feature switch.
- Define the feature switch.
Add the
com.taobao.csp.switchcenter.annotation.AppSwitchannotation to the field. The field modifier must bepublic static.For example:
public class CommonTypeSwitch { @AppSwitch(des = "String type switch", level = Level.p2) public static String stringSwitch = "string"; @AppSwitch(des = "Integer type switch", level = Level.p1) public static Integer integerSwitch = 2; @AppSwitch(des = "Boolean type switch", level = Level.p4) public static Boolean booleanSwitch = true; @AppSwitch(des = "AtomicInteger type switch", level = Level.p1) public static AtomicInteger atomicIntegerSwitch = new AtomicInteger(21); @AppSwitch(des = "AtomicBoolean type switch", level = Level.p1) public static AtomicBoolean atomicBooleanSwitch = new AtomicBoolean(true); @AppSwitch(des = "AtomicLong type switch", level = Level.p1) public static AtomicLong atomicLongSwitch = new AtomicLong(4L); @AppSwitch(des = "Generic String List type switch", level = Level.p1) public static List<String> stringListSwitch = new ArrayList<String>(); @AppSwitch(des = "Generic <Integer, String> Map switch", level = Level.p4) public static Map<Integer, String> INT_STRING_MAP = new HashMap<Integer, String>(); @SuppressWarnings("deprecation") @AppSwitch(des = "Date type switch", level = Level.p1) public static Date dateTypeSwitch = new Date(114, 6, 3); @AppSwitch(des = "BigInteger type switch", level = Level.p1) public static BigInteger bigIntegerTypeSwitch = BigInteger.valueOf(38888); @AppSwitch(des = "BigDecimal type switch", level = Level.p1) public static BigDecimal bigDecimalTypeSwitch = BigDecimal.valueOf(3.00000000001); @AppSwitch(des = "Enumeration type switch", level = Level.p1) public static EnumType enumTypeSwitch = EnumType.ITEM1; @AppSwitch(des = "LinkedList with generic List<Integer>", level = Level.p1) public static List<List<Integer>> LIST_INT_LINKEDLIST = new LinkedList<List<Integer>>(); @AppSwitch(des = "HashMap with generic <Map<String, Integer>, Map<String, Integer>>", level = Level.p1) public static Map<Integer, Map<String, Map<String, Integer>>> MAP_MAP_HASHMAP = new HashMap<Integer, Map<String, Map<String, Integer>>>(); } - Call the registration method.
/* The application calls this method to complete registration. Ensure that this method is called only once when the application starts. Calling it multiple times throws an exception. The application name is optional. If you do not specify it, the value of the project.name startup parameter is used. The constant class parameter is a variable argument, so you can register multiple constant classes. If the constant class does not have the com.taobao.csp.switchcenter.annotation.NameSpace annotation, the full class path name is used as the namespace by default. */ SwitchManager.init("appName", CommonTypeSwitch.class); - Configure startup parameters.
- Private network
// Replace AppName with your custom application name. ahas.namespace=default project.name=AppName - Public network
// Replace AppName with your custom application name and <license> with the actual value. ahas.namespace=default project.name=AppName ahas.license=<license>Note Only connections in a public network environment require a license. You can view and save the license on the New Application Connection page. For more information, see View and save a license.
- Private network
- Redeploy your application.
Connect using Spring Boot
For applications connected using Spring Boot, follow these steps to add a feature switch.
- Define the feature switch.
- Add the
@Switchannotation to the relevant switch class. - Add the
com.alibaba.csp.ahas.switchcenter.annotation.Switchannotation to the relevant constant class, and add thecom.taobao.csp.switchcenter.annotation.AppSwitchannotation to the corresponding field. The field modifier must bepublic static.
@Switch public class SwitchConfig { @AppSwitch(des="Boolean type switch", level=Level.p2, callback=TestCallback.class) public static boolean test_switch = false; } - Add the
- Configure startup parameters.
Add the following configuration items to the application.properties file.
- Private network
# Specify the AHAS environment to connect to. ahas.namespace=default # Customize your application name. project.name=AppName - Public network
# Specify the AHAS environment to connect to. ahas.namespace=default # Customize your application name. project.name=AppName # Configure the license information. ahas.license=<license>Note Only connections in a public network environment require a license. You can view and save the license on the New Application Connection page. For more information, see View and save a license.
- Private network
- Restart your application.
Results
After the switch is added, go to the Feature Switch page and click the resource card of the target application. On the Switch List page of the application, you can view information about the new switch.
More information
To customize the group for a feature switch, add the com.taobao.csp.switchcenter.annotation.NameSpace annotation in your code. If you do not define a custom group, the class name is used as the default group name. For example:
package com.taobao.csp.switchcenter.example;
import com.taobao.csp.switchcenter.annotation.AppSwitch;
import com.taobao.csp.switchcenter.annotation.NameSpace;
import com.taobao.csp.switchcenter.bean.Switch.Level;
@NameSpace(nameSpace="customNamespace") // customNamespace is the custom group name.
public class PrimitiveTypeSwitch // PrimitiveTypeSwitch is the default group name.
{
@AppSwitch(des="int type switch", level=Level.p1)
public static int primitiveIntSwitch=1;
@AppSwitch(des="double type switch", level=Level.p1)
public static double primitiveDoubleSwitch=1.121;
}
com.taobao.csp.switchcenter.bean.Switch.Level indicates the importance level of the switch. There are four levels: p1, p2, p3, and p4. p1 is the highest importance level, and p4 is the lowest.