1. Redis
Redis 想必大多数工程师已经耳熟能详,简单提一下, Redis是一个开放源代码(BSD许可)的内存中数据结构存储,用作数据库,缓存和消息代理。 它支持数据结构,例如字符串,哈希,列表,集合,带范围查询的排序集合,位图,超日志,带有半径查询和流的地理空间索引。 Redis具有内置的复制,Lua脚本,LRU逐出,事务和不同级别的磁盘持久性,并通过Redis Sentinel和Redis Cluster自动分区提供了高可用性。
可以在这些类型上运行原子操作,例如追加到字符串。 在哈希中增加值; 将元素推送到列表; 计算集的交集,并集和差; 或在排序集中获得排名最高的成员。
先下载并启动 redis
$ wget http://download.redis.io/releases/redis-5.0.8.tar.gz
$ tar xzf redis-5.0.8.tar.gz
$ cd redis-5.0.8
$ make
$ cd src
$ ./redis-server --daemonize yes
详细的内容和命令不作赘述,参见 Redis Cheat Sheets
Redis 支持 Hashmap 这样的数据结构, 相当于字典
$redis-cli -c -p 9001
127.0.0.1:9001> hsetnx $service_$timeslot $api_satisfied_count 0
-> Redirected to slot [16015] located at 127.0.0.1:9003
(integer) 1
127.0.0.1:9003> hincrby $service_$timeslot $api_satisfied_count 1
(integer) 1
127.0.0.1:9003> hget $service_$timeslot $api_satisfied_count
"1"
127.0.0.1:9003> hincrby $service_$timeslot $api_satisfied_count 10
(integer) 11
127.0.0.1:9003> hget $service_$timeslot $api_satisfied_count
"11"
2. 应用程序性能指标 (APDEX)
假设响应时间在T秒之内是令人满意的,在 F 秒之外是令人沮丧的
- 1) 满意的 satisfied
这代表响应时间小于设定的阈值(T秒),用户感觉满意。
- 2) 可容忍的 tolerating
这代表响应时间大于T秒 并小于F秒,性能不佳但是还可以继续使用,用户感觉仍可容忍。
- 3)失望的 Frustrated
这代表响应时间超过F秒,用户难以接受,放弃继续使用,用户感觉失望。
3. APDEX 可以能做什么
就我所知,APDEX 用来做报警和流量分配是非常有用的,试想一个微服务有若干个下游服务,如果对下游服务的请求总是出错,或者很慢,需要考虑进行 Failover 或者做熔断。
试举一例,我们有一个任务服务 task_service, 它有一个重要的 api 为 create_task, 我们可以通过 Redis 来记录当前时间段中这个服务的这个 API 的 APDEX 值
from pytz import timezone
from datetime import datetime
import sys
import redis
from rediscluster import RedisCluster
from redis.client import Redis
from loguru import logger
logger.add(sys.stderr,
format="{time} {message}",
filter="client",
level="INFO")
logger.add('logs/redis_client_{time:YYYY-MM-DD}.log',
format="{time} {level} {message}",
filter="client",
level="ERROR")
class Apdex:
_satisfied_count = 0.0
_tolerating_count = 0.0
_frustrated_count = 0.0
def get_apdex_value(self):
sum = self._satisfied_count + self._tolerating_count + self._frustrated_count
if sum == 0:
return 1.0
else:
return (self._satisfied_count + self._tolerating_count/2)/sum
def __str__(self):
return "{},{},{}".format(self._satisfied_count, self._tolerating_count, self._frustrated_count)
class RedisClient:
def __init__(self, connection_string, password=None):
self.startup_nodes = []
nodes = connection_string.split(',')
for node in nodes:
host_port = node.split(':')
self.startup_nodes.append({'host': host_port[0], 'port': host_port[1]})
self.password = password
logger.debug(self.startup_nodes)
self.redis_pool = None
self.redis_instance = None
self.redis_cluster = None
def connect(self):
if(len(self.startup_nodes) < 2):
host = self.startup_nodes[0].get('host')
port = self.startup_nodes[0].get('port')
if self.password:
self.redis_pool = redis.ConnectionPool(host=host, port=port, db=0)
else:
self.redis_pool = redis.ConnectionPool(host=host, port=port, password = self.password, db=0)
self.redis_instance = Redis(connection_pool=self.redis_pool, decode_responses=True)
return self.redis_instance
#, skip_full_coverage_check=True
self.redis_cluster = RedisCluster(startup_nodes=self.startup_nodes, password=self.password)
return self.redis_cluster
def test_hashset(serviceName, apiName, timeSlot, redisHosts="localhost:6379"):
logger.info("--- test_hashset ---")
client = RedisClient(redisHosts)
conn = client.connect()
key_of_last_5min = "{}_{}".format(serviceName, timeSlot)
conn.hsetnx(key_of_last_5min, apiName + "_satisfied_count", 0)
conn.hsetnx(key_of_last_5min, apiName + "_tolerating_count", 0)
conn.hsetnx(key_of_last_5min, apiName + "_frustrated_count", 0)
conn.expire(key_of_last_5min, 300)
conn.hincrby(key_of_last_5min, apiName + "_satisfied_count", 10)
conn.hincrby(key_of_last_5min, apiName + "_tolerating_count", 20)
conn.hincrby(key_of_last_5min, apiName + "_frustrated_count", 30)
def test_hashget(serviceName, timeslot, redisHosts="localhost:6379"):
logger.info("--- test_hashget ---")
client = RedisClient(redisHosts)
conn = client.connect()
key = "{}_{}".format(serviceName, timeslot)
logger.info("key is " + key)
values = conn.hgetall(key)
apdex = Apdex()
for key, value in values.items():
logger.info("{}={}".format(key.decode("utf-8"), value.decode("utf-8")))
sub_key = key.decode("utf-8")
if sub_key.endswith('_satisfied_count'):
apdex._satisfied_count = int(value)
elif sub_key.endswith('_tolerating_count'):
apdex._tolerating_count = int(value)
elif sub_key.endswith('_frustrated_count'):
apdex._frustrated_count = int(value)
logger.debug(apdex)
logger.info("apdex value is {}", apdex.get_apdex_value())
def main():
current_time = datetime.now(timezone('UTC'))
time_slot = int(current_time.timestamp() / 60)
serviceName = "task_service"
print("{}_{}".format(serviceName, time_slot))
test_hashset("task_service", "create_task", time_slot)
test_hashget(serviceName, time_slot)
if __name__ == "__main__":
main()
运行结果如下
python RedisClient.py
task_service_26509679
2020-05-27 19:59:41.457 | INFO | __main__:test_hashset:64 - --- test_hashset ---
2020-05-27 19:59:41.457 | DEBUG | __main__:__init__:43 - [{'host': 'localhost', 'port': '6379'}]
2020-05-27 19:59:41.472 | INFO | __main__:test_hashget:81 - --- test_hashget ---
2020-05-27 19:59:41.473 | DEBUG | __main__:__init__:43 - [{'host': 'localhost', 'port': '6379'}]
2020-05-27 19:59:41.473 | INFO | __main__:test_hashget:87 - key is task_service_26509679
2020-05-27 19:59:41.475 | INFO | __main__:test_hashget:91 - create_task_satisfied_count=10
2020-05-27 19:59:41.476 | INFO | __main__:test_hashget:91 - create_task_tolerating_count=20
2020-05-27 19:59:41.476 | INFO | __main__:test_hashget:91 - create_task_frustrated_count=30
2020-05-27 19:59:41.476 | DEBUG | __main__:test_hashget:100 - 10,20,30
2020-05-27 19:59:41.476 | INFO | __main__:test_hashget:101 - apdex value is 0.3333333333333333
网友评论