Exploring Nacos: Application Scenarios for Alibaba’s Open Source Framework

Before Nacos: Problems with Sensitive Information

spring.datasource.url= database connection address for production environmentspring.datasource.username=database user account for production environmentspring.datasource.password=database user password for production environment
spring.datasource.url=database connection address for development environmentspring.datasource.username=database user account for development environmentspring.datasource.password=database user password for development environment

Improving Sensitive Configuration Management with Nacos

1. Add the dependency

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>

2. Add the Nacos connection configuration in application.properties

nacos.config.server-addr=127.0.0.1:8848

3. Add the annotation of @NacosPropertySource

@SpringBootApplication
@NacosPropertySource(dataId = "mysql.properties")
public class SpringBootMySQLApplication {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

4. Create a configuration with the dataId “mysql.properties” in the locally launched Nacos console

Current Limiting and Downgrading with Nacos

1. Add the dependency

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>${latest.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
<version>${latest.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>${latest.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${latest.version}</version>
</dependency>

2. Simulate concurrent requests

final class RunTask implements Runnable {
@Override
public void run() {
while (!stop) {
Entry entry = null;
try {
entry = SphU.entry(resourceName);
// token acquired, means pass
pass.addAndGet(1);
} catch (BlockException e1) {
block.incrementAndGet();
} catch (Exception e2) {
// biz exception
} finally {
total.incrementAndGet();
if (entry != null) {
entry.exit();
}
}

Random random2 = new Random();
try {
TimeUnit.MILLISECONDS.sleep(random2.nextInt(50));
} catch (InterruptedException e) {
// ignore
}
}
}
}

3. Configure Nacos connection information, dataID, and so on, and set them as the data source for Sentinel

public class NacosDynamicFlowDemo {

private static final String KEY = "TestResource";

public static void main(String[] args) {
final String remoteAddress = "localhost";
final String groupId = "DEFAULT_GROUP";
final String dataId = "com.alibaba.nacos.demo.flow.rule";

ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());

// Assume we config: resource is `TestResource`, initial QPS threshold is 5.
FlowQpsRunner runner = new FlowQpsRunner(KEY, 1, 10000);
runner.simulateTraffic();
runner.tick();
}
}

4. Create a flow control configuration with com.alibaba.acos.demo.flow.rule as the dataId in the locally launched Nacos console

5. Run NacosDynamicFlowDemo

6. Modify the newly created flow control configuration in the Nacos console and change the value of the current-limit threshold count to 1.0

Dynamic Traffic Dispatch with Nacos

Key Takeaways

Alibaba Tech

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Tech

Alibaba Tech

5.3K Followers

First-hand & in-depth information about Alibaba's tech innovation in Artificial Intelligence, Big Data & Computer Engineering. Follow us on Facebook!