-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
557 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,81 @@ | ||
# redis-monitor-tools | ||
a tools analysis the redis monitor, support cluster | ||
redis monitor 分析工具,可以通过采样列出运行时使用频率最高的key和前缀。注意:运行期间会对redis服务端的读写性能会产生影响,因此如在生产环境使用,建议在业务低峰期,充分评估风险。具体可参考:https://redis.io/commands/monitor | ||
## 怎么使用 | ||
### 1. 编译 | ||
```bash | ||
git clone [email protected]:zxbu/redis-monitor-tools.git | ||
mvn package | ||
``` | ||
### 2. 运行 | ||
```bash | ||
java -jar redis-monitor.jar --redis.command.servers=127.0.0.1:6378,127.0.0.1:6379 --redis.command.numbers=1000 --redis.command.top=20 | ||
|
||
redis.command.servers: 可以填多个redis节点地址,用逗号分隔 | ||
redis.command.numbers: 最多纪录的monitor数量 | ||
redis.command.top: 排行榜Top数量 | ||
``` | ||
运行结束后,自动输出结果。 | ||
```bash | ||
----- Count ----- | ||
total : 100 | ||
'127.0.0.1:6378' : 58 ( 58.00% ) | ||
'127.0.0.1:6379' : 42 ( 42.00% ) | ||
|
||
|
||
----- Top keys ----- | ||
'Product:Add' : 32 ( 32.00% ) | ||
'Order:Search' : 20 ( 20.00% ) | ||
'Foo' : 14 ( 14.00% ) | ||
'Product:Delete' : 13 ( 13.00% ) | ||
|
||
|
||
----- Top actions ----- | ||
'GET' : 80 ( 88.00% ) | ||
'SSCAN' : 1 ( 1.00% ) | ||
|
||
|
||
----- Top prefixes ----- | ||
'Product' : 72 ( 72.00% ) | ||
'Order' : 1 ( 1.00% ) | ||
``` | ||
|
||
## How to use | ||
|
||
### 1. Building | ||
```bash | ||
git clone [email protected]:zxbu/redis-monitor-tools.git | ||
mvn package | ||
``` | ||
### 2. Run | ||
```bash | ||
java -jar redis-monitor.jar --redis.command.servers=127.0.0.1:6378,127.0.0.1:6379 --redis.command.numbers=1000 --redis.command.top=20 | ||
|
||
redis.command.servers: host1:port1,host2:port2. the redis server multiple address | ||
redis.command.numbers: the max monitor number was recorder | ||
redis.command.top: the top number | ||
``` | ||
|
||
auto echo result when end | ||
```bash | ||
----- Count ----- | ||
total : 100 | ||
'127.0.0.1:6378' : 58 ( 58.00% ) | ||
'127.0.0.1:6379' : 42 ( 42.00% ) | ||
|
||
|
||
----- Top keys ----- | ||
'Product:Add' : 32 ( 32.00% ) | ||
'Order:Search' : 20 ( 20.00% ) | ||
'Foo' : 14 ( 14.00% ) | ||
'Product:Delete' : 13 ( 13.00% ) | ||
|
||
|
||
----- Top actions ----- | ||
'GET' : 80 ( 88.00% ) | ||
'SSCAN' : 1 ( 1.00% ) | ||
|
||
|
||
----- Top prefixes ----- | ||
'Product' : 72 ( 72.00% ) | ||
'Order' : 1 ( 1.00% ) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.5.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>github.zxbu</groupId> | ||
<artifactId>redis-monitor-tools</artifactId> | ||
<version>1.0.0</version> | ||
<name>redis-monitor</name> | ||
<description>a tools analysis the redis monitor, support cluster</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>redis-monitor.jar</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
src/main/java/github/zxbu/redismonitor/RedisMonitorApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package github.zxbu.redismonitor; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class RedisMonitorApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(RedisMonitorApplication.class, args); | ||
} | ||
|
||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/github/zxbu/redismonitor/config/RedisConfigProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package github.zxbu.redismonitor.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import java.util.List; | ||
|
||
@ConfigurationProperties(prefix = "redis.command") | ||
public class RedisConfigProperties { | ||
private List<String> servers; | ||
|
||
private Integer numbers; | ||
|
||
private Integer top; | ||
|
||
public List<String> getServers() { | ||
return servers; | ||
} | ||
|
||
public void setServers(List<String> servers) { | ||
this.servers = servers; | ||
} | ||
|
||
public Integer getNumbers() { | ||
return numbers; | ||
} | ||
|
||
public Integer getTop() { | ||
return top; | ||
} | ||
|
||
public void setTop(Integer top) { | ||
this.top = top; | ||
} | ||
|
||
public void setNumbers(Integer numbers) { | ||
this.numbers = numbers; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/github/zxbu/redismonitor/config/RedisMonitorAutoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package github.zxbu.redismonitor.config; | ||
|
||
import github.zxbu.redismonitor.service.MonitorCommandService; | ||
import github.zxbu.redismonitor.service.QueryService; | ||
import github.zxbu.redismonitor.util.PercentUtil; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
import redis.clients.jedis.Jedis; | ||
import redis.clients.jedis.JedisMonitor; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.io.*; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@EnableConfigurationProperties(RedisConfigProperties.class) | ||
@Configuration | ||
public class RedisMonitorAutoConfig { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(RedisMonitorAutoConfig.class); | ||
private static AtomicInteger counter = new AtomicInteger(0); | ||
|
||
@Autowired | ||
private RedisConfigProperties redisConfigProperties; | ||
|
||
@Autowired | ||
private MonitorCommandService monitorCommandService; | ||
|
||
@Autowired | ||
private QueryService queryService; | ||
|
||
@PostConstruct | ||
public void init() { | ||
List<String> servers = redisConfigProperties.getServers(); | ||
servers.forEach(this::monitor); | ||
new Thread(this::input).start(); | ||
} | ||
private void input() { | ||
String s; | ||
InputStreamReader isr = new InputStreamReader(System.in); | ||
BufferedReader br = new BufferedReader(isr); | ||
try { | ||
s = br.readLine(); | ||
while (true) { | ||
if (s.equals("monitor") || s.equals("m")) { | ||
queryService.query(new BufferedWriter(new OutputStreamWriter(System.out))); | ||
} | ||
if (s.equals("exit")) { | ||
queryService.query(new BufferedWriter(new OutputStreamWriter(System.out))); | ||
System.exit(0); | ||
} | ||
s = br.readLine(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
|
||
private void monitor(String server) { | ||
String[] split = server.split(":"); | ||
new Thread(() -> { | ||
Jedis jedis = new Jedis(split[0], Integer.parseInt(split[1]), 10000); | ||
LOGGER.info("{} start monitor...", server); | ||
jedis.monitor(new JedisMonitor() { | ||
@Override | ||
public void onCommand(String command) { | ||
monitorCommandService.save(server, command); | ||
counter.incrementAndGet(); | ||
if (redisConfigProperties.getNumbers() > 0 && counter.get() % (redisConfigProperties.getNumbers() / 10) == 0) { | ||
LOGGER.info("now counter number is {} ({})", counter.get(), PercentUtil.format(counter.get(), redisConfigProperties.getNumbers())); | ||
if ((counter.get() >= redisConfigProperties.getNumbers())) { | ||
queryService.query(new BufferedWriter(new OutputStreamWriter(System.out))); | ||
System.exit(0); | ||
} | ||
} | ||
} | ||
|
||
}); | ||
}).start(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/github/zxbu/redismonitor/dao/MonitorCommandRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package github.zxbu.redismonitor.dao; | ||
|
||
|
||
import github.zxbu.redismonitor.model.MonitorCommand; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
@Service | ||
public class MonitorCommandRepository { | ||
private AtomicInteger idCounter = new AtomicInteger(1); | ||
private Map<String, MonitorCommand> repo = new ConcurrentHashMap<>(); | ||
public void save(MonitorCommand monitorCommand) { | ||
monitorCommand.setId(String.valueOf(idCounter.getAndIncrement())); | ||
repo.put(monitorCommand.getId(), monitorCommand); | ||
} | ||
|
||
public Iterable<MonitorCommand> findAll() { | ||
return new ArrayList<>(repo.values()); | ||
} | ||
} |
Oops, something went wrong.