本文共 19124 字,大约阅读时间需要 63 分钟。
本章主要讲解redis的使用案例,相对于以往的技术,redis在这些领域将大大简化或者提高程序的便利和稳定。比如日志记录,相对于以往的文件记录方式将更加灵活,便于数据操作,
-
- 日志记录
以往的日志记录采用本地文件存储的方式,这种方式有一种弊端,由于是文本存储,各个服务器之间很难协调,很难对多个数据文件进行聚合,导致之后的数据分析,将显得很不方便,相对而言,由于redis数据库可以在不同的服务器之间通信,加上自带数据处理函数,所以将更加方便。
对不同的数据分析,不同的日志有不同的好处,为了方便管理或者处理,需要对相应的日志做分类处理。比如对于一个网站而言,日志是很多的,如果大量记录是不现实的,但是,很多情况下,比如我们服务器突然出现问题,或者说现在我们需要立刻查看最近的操作的数据情况,这是,我们就可以使用最新的日志,这些日志只会记录最近发生的响应,而不会记录很久之前的数据情况,这样就便于我们查错,或者查看最近的操作情况。
- 最新日志:分别记录最近的debug\info\warning\error\critical等日志情况,便于查看(这里使用的是列表存储,方便添加和删除日志)
常见日志:常见日志即我们认为重要的日志,比如用户的操作,加入购物车,购买等信息。这些都是很重要的日志信息,可用于数据挖掘。但是文章中并没有给出这个例子,而是,对不同的日志信息进行排序,对每个动作进行打分,从而只保存经常操作的那些动作,即表明这些操作是最重要的,有很高的商业价值。(同样是列表)
- 计数器和数据统计
计数器是什么?这里的计数器并不是电子电路里面的时钟计数器,而是一种数据记录,比如用户的登陆次数,百度上某个医疗广告的点击量,整个百度网页每个渲染页面的点击量等等,这些都是很重要的价值信息。能够从中发掘大量的商业价值。
数据统计。在统计学中,有几个重要的数据衡量指标,分别是最大值、最小值、平均值、数量、总分数、标准差等等;比如对一个网站进行优化,我么你需要记录网站各个页面的响应时间,这些数据有利于我们有正对性的进行优化。
本行计数器案例主要模拟,分别在1秒、5秒、一分钟、一小时等,网站的点击量,由于需要进行排序,所以使用的是有序表和哈希集结合
数据统计主要是模拟网页的加载时间
IP地址等excel本地数据查询
我们经常会使用搜索,但是这里搜索并不是百度搜索,而是有现成的数据。比如我们一个存储数据的excel表,亦或是文本等等。例如,有时候我们会查询某个ip所对应的网址,但是这个数据很大,如果存储在一般的关系型数据库,查询将会有点慢,因此,这时候就可以使用redis数据库,大大加快查询速度。
服务配置信息动态更改
开发一个应用会存在很多配置文件,只写配置文件并不是一成不变,以往的人工配置,会出现,一旦一个配置文件发生改变,就需要人工取重新配置,这样大大降低了效率。因此,我们这里考虑使用程序自动化配置,即将配置信息保存在redis中,将其写入到程序中作为守护线程,每当配置文件发生改变都会自动进行配置
文中的样例为:redis配置redis的连接配置,会将redis的连接信息保存在redis中,自动检查新的配置文件,然后更新配置到程序中
详细代码:
import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import org.apache.commons.csv.CSVFormat;import org.apache.commons.csv.CSVParser;import org.apache.commons.csv.CSVRecord;import org.javatuples.Pair;import redis.clients.jedis.Jedis;import redis.clients.jedis.Pipeline;import redis.clients.jedis.Transaction;import redis.clients.jedis.Tuple;import redis.clients.jedis.ZParams;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.text.Collator;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.UUID;/** * @author: ZouTai * @date: 2018/7/6 * @description: * @create: 2018-07-06 15:23 */public class Chapter05 { public static final String INFO = "info"; public static final SimpleDateFormat TIMESTAMP = new SimpleDateFormat("EEE MMM dd HH:00:00 YYYY"); public static final SimpleDateFormat ISO_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:00:00"); public static final Collator COLLATOR = Collator.getInstance(); private static final int[] PRECISION = new int[]{ 1, 5, 60, 300, 3600, 18000, 86400}; public static void main(String[] args) throws InterruptedException { new Chapter05().run(); } private void run() throws InterruptedException { Jedis conn = new Jedis("localhost"); conn.select(15); // 测试1:redis记录日志// testLogRecent(conn);// testLogCommon(conn); // 测试2:计数器和统计数据// testCounters(conn);// testStats(conn);// testAccessTime(conn); // 测试3:查找ip所在的城市及其详细信息// testIpLookup(conn); // 测试4:服务发现与配置// testIsUnderMaintenance(conn); testConfig(conn); } private void testConfig(Jedis conn) throws InterruptedException { System.out.println("首先,创建一个配置文件map:"); Map config = new HashMap (); config.put("db", 15); // 添加到redis中 setConfig(conn, "redis", "test", config); Jedis conn2 = redisConnection("test"); System.out.println("新的连接是否存在:" + (conn2.info() != null)); } private static final Map REDIS_CONNECTIONS = new HashMap (); private static final Map > CONFIGS = new HashMap >(); private static final Map CHECKED = new HashMap (); private Jedis redisConnection(String component) { Jedis configConn = REDIS_CONNECTIONS.get("config"); if (configConn == null) { configConn = new Jedis("localhost"); configConn.select(15); REDIS_CONNECTIONS.put("config", configConn); } String key = "config:redis:" + component; Map oldConfig = CONFIGS.get(key); Map newConfig = getConfig(configConn, "redis", component); // 判断配置文件是否相等,不相等,更改当前配置文件 if (!newConfig.equals(oldConfig)) { Jedis conn = new Jedis("localhost"); conn.select(((Double) newConfig.get("db")).intValue()); REDIS_CONNECTIONS.put(key, conn); } return REDIS_CONNECTIONS.get(key); } /** * 从redis获取新的配置文件,与当前程序中的配置文件CONFIGS对比 */ @SuppressWarnings("unchecked") private Map getConfig(Jedis conn, String type, String component) { String key = "config:" + type + ":" + component; long wait = 1000; if (CHECKED.get(key) == null || CHECKED.get(key) < System.currentTimeMillis() - wait) { CHECKED.put(key, System.currentTimeMillis()); String value = conn.get(key); Map config = null; if (value != null) { Gson gson = new Gson(); config = (Map ) gson.fromJson(value, new TypeToken
转载地址:http://ceepi.baihongyu.com/