redis 查找附近的人

儿童定位手表,有个交友功能,查找附近的人,用redis的geo来实现比较简单,其实是一个ZSET(有序集合)

redis 版本要大于3.2

查看redis 版本    /usr/bin/redis-server      --version

注意引入的jar版本:可能运行时候会报错,这时要检查jar包的版本,可能版本冲突导致报错

public class Coordinate {

    //经度
    private double longitude;
    
    //纬度
    private double latitude;
    
    //用户id
    private String key;
    
    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }

}
public class RedisUtil {
    
       private static JedisPool jedisPool = null;
        // Redis服务器IP
        private static String ADDR = "xx.xxx.xx.xx";
        // Redis的端口号
        private static int PORT = 6379;
        // 访问密码
        private static String AUTH = "xxxxxx";
 
        /**
                   * 初始化Redis连接池
         */
        static {
            try {
                JedisPoolConfig config = new JedisPoolConfig();
                // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
                config.setBlockWhenExhausted(true);
                // 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
                config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
                // 是否启用pool的jmx管理功能, 默认true
                config.setJmxEnabled(true);
                // 最大空闲连接数, 默认8个 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
                config.setMaxIdle(8);
                // 最大连接数, 默认8个
                config.setMaxTotal(200);
                // 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
                config.setMaxWaitMillis(1000 * 100);
                // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
                config.setTestOnBorrow(true);
                jedisPool = new JedisPool(config, ADDR, PORT, 3000, AUTH);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /**
                   * 获取Jedis实例
         * 
         * @return
         */
        public synchronized static Jedis getJedis() {
            try {
                if (jedisPool != null) {
                    Jedis resource = jedisPool.getResource();
                    return resource;
                } else {
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
 
        /**
                    * 释放jedis资源
         * 
         * @param jedis
         */
        public static void close(final Jedis jedis) {
            if (jedis != null) {
                jedis.close();
            }
        }
 
        public static void main(String[] args) {
            Jedis jedis = RedisUtil.getJedis();
            
//            //添加经纬度
//            Coordinate coordinate=new Coordinate();
//            coordinate.setLatitude(31.244803);  //维度
//            coordinate.setLongitude(121.483671); //经度
//            coordinate.setKey("zhangsan");  //可以作为用户表的id
//            
//            
//            //添加经纬度
//            Coordinate coordinate1=new Coordinate();
//            coordinate1.setLatitude(31.245321);  //维度
//            coordinate1.setLongitude(121.485015); //经度
//            coordinate1.setKey("lisi");  //可以作为用户表的id
//            
//            //添加经纬度
//            Coordinate coordinate2=new Coordinate();
//            coordinate2.setLatitude(31.245456);  //维度
//            coordinate2.setLongitude(121.485285); //经度
//            coordinate2.setKey("wangwu");  //可以作为用户表的id
//            
//            addReo(coordinate);
//            addReo(coordinate1);
//            addReo(coordinate2);
            
            
//            Coordinate query = new Coordinate();
//            query.setLongitude(121.485285);
//            query.setLatitude(31.245456);
//            
//            List<GeoRadiusResponse> result = geoQuery(query);
//            for(GeoRadiusResponse res : result) {
//                System.out.println(res.getMemberByString());
//            }
            
            
            RedisUtil.close(jedis);
            
        }
 
        /**
                    * 添加坐标
         * key 经度  维度  距离
         * return m 表示单位为米*/
        public static Long addReo(Coordinate coordinate) {
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                //第一个参数可以理解为表名
                return jedis.geoadd("test",coordinate.getLongitude(),coordinate.getLatitude(),coordinate.getKey());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                if (null != jedis)
                    jedis.close();
            }
            return null;
        }
        /**
                    * 查询附近人
         * key 经度  维度  距离
         * return GeoRadiusResponse*/
        public static List<GeoRadiusResponse> geoQuery(Coordinate coordinate) {
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                //200F GeoUnit.KM表示km 
                return jedis.georadius("test",coordinate.getLongitude(),coordinate.getLatitude(),100F,GeoUnit.M, GeoRadiusParam.geoRadiusParam().withDist());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                if (null != jedis)
                    jedis.close();
            }
            return null;
        }
 
}

引用相关jar

转载自:https://blog.csdn.net/liaodehong/article/details/59104451

猜你喜欢

转载自www.cnblogs.com/moris5013/p/10615349.html