zhangqf 6 years ago
parent
commit
3e96f61993

+ 21 - 2
hotels_rpc/pom.xml

@@ -1,9 +1,28 @@
-<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	
   <groupId>com.tripto</groupId>
   <artifactId>hotels_rpc</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   
+  <!-- 项目属性 -->
+  <dependencies>
+  	<!-- fastjson json -->
+	<dependency>
+		<groupId>com.alibaba</groupId>
+		<artifactId>fastjson</artifactId>
+		<version>1.1.40</version>
+	</dependency>
+  
+  	<dependency>
+  		<groupId>redis.clients</groupId>
+  		<artifactId>jedis</artifactId>
+  		<version>2.5.1</version>
+  	</dependency>
+  </dependencies>
+  
   <repositories>
 	<repository>
 		<id>public</id>

+ 41 - 0
hotels_rpc/src/main/java/com/tripto/utils/LocationUtils.java

@@ -0,0 +1,41 @@
+package com.tripto.utils;
+
+/**
+ * 位置计算
+ * @author admin
+ */
+public class LocationUtils {
+
+    private static double EARTH_RADIUS = 6378.137;    
+    
+    private static double rad(double d) {    
+        return d * Math.PI / 180.0;    
+    }    
+    
+    /**   
+     * 通过经纬度获取距离(单位:米)   
+     * @param lat1   
+     * @param lng1   
+     * @param lat2   
+     * @param lng2   
+     * @return   
+     */    
+    public static int getDistance(double lat1, double lng1, double lat2,    
+                                     double lng2) {    
+        double radLat1 = rad(lat1);    
+        double radLat2 = rad(lat2);    
+        double a = radLat1 - radLat2;    
+        double b = rad(lng1) - rad(lng2);    
+        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)    
+                + Math.cos(radLat1) * Math.cos(radLat2)    
+                * Math.pow(Math.sin(b / 2), 2)));    
+        s = s * EARTH_RADIUS;    
+        s = Math.round(s * 10000d) / 10000d;
+        s = s*1000;
+        return (int)s;
+    }
+    
+    public static void main(String[] args) {
+    	System.out.println(getDistance(39.98883,116.541817,40.008839,116.538368)+"米");
+	}
+}

+ 188 - 0
hotels_rpc/src/main/java/com/tripto/utils/RedisUtil.java

@@ -0,0 +1,188 @@
+package com.tripto.utils;
+
+import java.util.Set;
+
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+import redis.clients.jedis.JedisPoolConfig;
+
+/**
+ * Redis工具类操作
+ * @ClassName: RedisUtil 
+ * @Description: TODO(这里用一句话描述这个类的作用) 
+ * @author zhangqifei 
+ * @date 2018年4月3日 下午12:19:59
+ */
+public class RedisUtil {
+
+	//Redis服务器IP
+    private static String ADDR = "101.200.40.164";
+    //Redis的端口号
+    private static int PORT = 12301;
+    //访问密码
+    private static String AUTH = "tripto!@#$1803";
+    //可用连接实例的最大数目,默认值为8;
+    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
+    private static int MAX_ACTIVE = 50;
+    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
+    private static int MAX_IDLE = 10;
+    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
+    private static int MAX_WAIT = 10000;
+    private static int TIMEOUT = 10000;
+    //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
+    private static boolean TEST_ON_BORROW = true;
+    private static JedisPool jedisPool = null;
+    /**
+     * 初始化Redis连接池
+     */
+    public static void Init(String h,int p,String a,int max_active,int max_idle,int max_wait,int timeout){
+        try {
+        	ADDR = h;
+        	PORT = p;
+        	AUTH = a;
+        	MAX_ACTIVE = max_active;
+        	MAX_IDLE = max_idle;
+        	MAX_WAIT = max_wait;
+        	TIMEOUT = timeout;
+        	
+            JedisPoolConfig config = new JedisPoolConfig();
+            config.setMaxTotal(MAX_ACTIVE);
+            config.setMaxIdle(MAX_IDLE);
+            config.setMaxWaitMillis(MAX_WAIT);
+            config.setTestOnBorrow(TEST_ON_BORROW);
+            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, 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 returnResource(final Jedis jedis) {
+        if (jedis != null) {
+            jedisPool.returnResource(jedis);
+        }
+    }
+	
+    /**
+     * 入Redis
+     * @param key   键
+     * @param value 值
+     * @param duration 过期时间(秒)
+     */
+    public static void addRedis(String key,String value,int duration) {
+    	Jedis jedis = getJedis();
+    	jedis.set(key, value);
+    	jedis.expire(key,duration);
+    	returnResource(jedis);
+    }
+    
+    /**
+     * 入Redis
+     * @param key   键
+     * @param value 值
+     */
+    public static void addRedis(String key,String value) {
+    	Jedis jedis = getJedis();
+    	jedis.set(key, value);
+    	returnResource(jedis);
+    }
+    
+    /**
+     * 读取Redis
+     * @param key
+     * @return
+     */
+    public static String getRedis(String key) {
+    	Jedis jedis = getJedis();
+    	String info = jedis.get(key);
+    	returnResource(jedis);
+    	return info;
+    }
+    
+    /**
+     * 统计当前缓存数量
+     * @return
+     */
+    public static int countKey() {
+    	int num = 0 ;
+    	Jedis jedis = getJedis();
+    	num = jedis.keys("*").size();
+    	returnResource(jedis);
+    	return num;
+    }
+    /** 
+     * 删除key,可以是一个,也可以是多个key 
+     * @param keys 
+     */  
+    public synchronized static void deleteKey(String ... keys ){  
+        getJedis().del(keys);  
+    }
+    
+    /** 
+     * 删除匹配的key<br> 
+     * 如以my为前缀的则 参数为"my*" 
+     * @param key 
+     */  
+    public synchronized static void deleteKeys(String pattern){  
+        //列出所有匹配的key  
+        Set<String> keySet = getJedis().keys(pattern);  
+        if(keySet == null || keySet.size()<=0){  
+            return;  
+        }  
+        String keyArr[] = new String[keySet.size()];  
+        int i =0;  
+        for (String keys : keySet) {  
+            keyArr[i] = keys;  
+            i++;  
+        }  
+        deleteKey(keyArr);  
+    }
+    
+    /** 
+     * 匹配所有KEY
+     * 如以my为前缀的则 参数为"my*" 
+     * @param key 
+     */  
+    public synchronized static String[] GetKeys(String pattern){  
+        //列出所有匹配的key  
+        Set<String> keySet = getJedis().keys(pattern);  
+        if(keySet == null || keySet.size()<=0){  
+            return null;  
+        }  
+        String keyArr[] = new String[keySet.size()];  
+        int i =0;  
+        for (String keys : keySet) {  
+            keyArr[i] = keys;  
+            i++;  
+        }  
+        return keyArr;
+    }
+    
+    public static void main(String[] args) {
+    	String[] sr = RedisUtil.GetKeys("CONTRO_10_Policy_*");
+    	for (int i = 0; i < sr.length; i++) {
+			System.out.println(sr[i]+"\t"+RedisUtil.getRedis(sr[i]).length());
+		}
+	}
+}