yumeng 1 mês atrás
pai
commit
fe0fa1e2f5

+ 16 - 6
pom.xml

@@ -38,6 +38,13 @@
 <!--                </exclusion>-->
 <!--            </exclusions>-->
         </dependency>
+
+        <dependency>
+            <groupId>com.amazonaws</groupId>
+            <artifactId>aws-java-sdk-s3</artifactId>
+            <version>1.11.490</version>
+        </dependency>
+
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
@@ -69,18 +76,21 @@
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>
 
-        <!--mongodb-->
+        <!-- redis 缓存操作 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.mongodb</groupId>
-            <artifactId>mongodb-driver-sync</artifactId>
-            <version>4.10.2</version>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+            <version>2.9.0</version>
         </dependency>
-
         <!-- mybatis-plus -->
         <dependency>
             <groupId>com.baomidou</groupId>

+ 72 - 0
src/main/java/cn/com/ctop/track/config/FastJson2JsonRedisSerializer.java

@@ -0,0 +1,72 @@
+package cn.com.ctop.track.config;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.parser.ParserConfig;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.type.TypeFactory;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.SerializationException;
+import org.springframework.util.Assert;
+
+import java.nio.charset.Charset;
+
+/**
+ * Redis使用FastJson序列化
+ * 
+ * @author ruoyi
+ */
+public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
+{
+    @SuppressWarnings("unused")
+    private ObjectMapper objectMapper = new ObjectMapper();
+
+    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
+
+    private Class<T> clazz;
+
+    static
+    {
+        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
+    }
+
+    public FastJson2JsonRedisSerializer(Class<T> clazz)
+    {
+        super();
+        this.clazz = clazz;
+    }
+
+    @Override
+    public byte[] serialize(T t) throws SerializationException
+    {
+        if (t == null)
+        {
+            return new byte[0];
+        }
+        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
+    }
+
+    @Override
+    public T deserialize(byte[] bytes) throws SerializationException
+    {
+        if (bytes == null || bytes.length <= 0)
+        {
+            return null;
+        }
+        String str = new String(bytes, DEFAULT_CHARSET);
+
+        return JSON.parseObject(str, clazz);
+    }
+
+    public void setObjectMapper(ObjectMapper objectMapper)
+    {
+        Assert.notNull(objectMapper, "'objectMapper' must not be null");
+        this.objectMapper = objectMapper;
+    }
+
+    protected JavaType getJavaType(Class<?> clazz)
+    {
+        return TypeFactory.defaultInstance().constructType(clazz);
+    }
+}

+ 90 - 0
src/main/java/cn/com/ctop/track/controller/JingDongCpaController.java

@@ -0,0 +1,90 @@
+package cn.com.ctop.track.controller;
+
+import cn.com.ctop.track.util.RedisUtil;
+import cn.com.ctop.track.utils.Check;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+@Controller
+@RequestMapping("/jdCallback")
+public class JingDongCpaController {
+    private static Logger logger = Logger.getLogger(JingDongCpaController.class);
+    @Autowired
+    private RedisUtil redisUtil;
+
+
+
+
+    // 老链接 屏蔽5
+    @RequestMapping(value = "/click")
+    @ResponseBody
+    public JSONObject click(HttpServletRequest request,
+                            HttpServletResponse response) {
+        logger.info("点击回调数据:" + request.getQueryString());
+        JSONObject returnJson = new JSONObject();
+        String queryString = request.getQueryString();
+        if (Check.isNull(queryString)) {
+            returnJson.put("code", 200);
+            returnJson.put("message", "SUCCESS");
+            return returnJson;
+        }
+        try {
+            String cid = request.getParameter("cid");    //客户后台账户ID
+            if (Check.isNull(cid)) {
+                returnJson.put("code", 200);
+                returnJson.put("message", "SUCCESS");
+                return returnJson;
+            }
+            JSONObject paramJson = new JSONObject();
+            String callback = request.getParameter("callback");
+            paramJson.put("callback", callback);
+            String accountId = request.getParameter("p1");
+            paramJson.put("accountId", accountId);
+            redisUtil.add(cid, paramJson, 3, TimeUnit.DAYS);
+            returnJson.put("code", 200);
+            returnJson.put("message", "SUCCESS");
+        } catch (Exception e) {
+            returnJson.put("code", 500);
+            returnJson.put("success", false);
+            returnJson.put("result", e.getMessage());
+        }
+        return returnJson;
+    }
+
+
+    @RequestMapping(value = "/action")
+    @ResponseBody
+    public JSONObject action(HttpServletRequest request,
+                            HttpServletResponse response) {
+        logger.info("行为回调数据:" + request.getQueryString());
+        JSONObject returnJson = new JSONObject();
+        String queryString = request.getQueryString();
+        if (Check.isNull(queryString)) {
+            returnJson.put("code", 200);
+            returnJson.put("message", "SUCCESS");
+            return returnJson;
+        }
+        try {
+
+            returnJson.put("code", 200);
+            returnJson.put("message", "SUCCESS");
+        } catch (Exception e) {
+            returnJson.put("code", 500);
+            returnJson.put("success", false);
+            returnJson.put("result", e.getMessage());
+        }
+        return returnJson;
+    }
+
+
+}

+ 150 - 0
src/main/java/cn/com/ctop/track/util/OssDemo.java

@@ -0,0 +1,150 @@
+package cn.com.ctop.track.util;
+
+
+
+import com.amazonaws.ClientConfiguration;
+import com.amazonaws.auth.AWSCredentials;
+import com.amazonaws.auth.AWSCredentialsProvider;
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.client.builder.AwsClientBuilder;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.AmazonS3Client;
+import com.amazonaws.services.s3.model.AmazonS3Exception;
+import com.amazonaws.services.s3.model.ObjectMetadata;
+import com.amazonaws.services.s3.model.S3Object;
+import com.amazonaws.services.s3.model.S3ObjectInputStream;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @description: todo
+ * @author: sunrui62
+ * @create: 2023/3/23 2:23 下午
+ **/
+public class OssDemo {
+    private static AmazonS3 s3;
+
+    private static final String accessKey = "JDC_F2DA04429F388D7E2167871292BB";
+    private static final String secretKey = "1BEEB211BC8DFBE30A17FAD6750571EA";
+    private static final String endpoint = "s3.cn-north-1.jdcloud-oss.com";
+    private static final String region = "oss-match-upload";
+    static {
+        s3 = init(accessKey, secretKey, endpoint, region);
+    }
+
+
+    private static final String bucketName = "test-space-sr";
+    public static void main(String[] args) throws Exception{
+        // 上传文件
+        String uploadFilePath = "cps/sunrui_test2/20230322/imei/input/qianfan.csv.0001";
+        uploadStream(bucketName, uploadFilePath, "imei1\nimei2");
+        // 获取文件内容
+        String content = getObjectContent(bucketName, uploadFilePath);
+        System.out.println(content);
+
+        // 阻塞获取output内容
+        while (true) {
+            boolean continueFlag = true;
+            try {
+                String successFilePath = "cps/sunrui_test2/20230322/imei/output/SUCCESS";
+                String successContent = getObjectContent(bucketName, successFilePath);
+                System.out.println("获取到文件,内容是:" + successContent);
+                continueFlag = false;
+            } catch (AmazonS3Exception e) {
+                System.out.println("文件还没有生成,等会接着获取");
+                Thread.sleep(10000);
+            } catch (Exception e){
+                e.printStackTrace();
+                continueFlag = false;
+            }
+            if (!continueFlag){
+                break;
+            }
+        }
+    }
+
+    /**
+     * 初始化客户端
+     * @param accessKey
+     * @param secretKey
+     * @param endpoint
+     * @return
+     */
+    private static AmazonS3 init(String accessKey, String secretKey, String endpoint, String region) {
+        ClientConfiguration config = new ClientConfiguration();
+        AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(endpoint, region);
+
+        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
+        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
+
+        AmazonS3 s3 = AmazonS3Client.builder()
+                .withEndpointConfiguration(endpointConfig)
+                .withClientConfiguration(config)
+                .withCredentials(awsCredentialsProvider)
+                .disableChunkedEncoding()
+                .build();
+        return s3;
+    }
+
+
+    /**
+     * @param bucketName
+     * @param filePath
+     * @param data
+     */
+    public static void uploadStream(String bucketName, String filePath, String data) {
+        byte[] byteData = data.getBytes(StandardCharsets.UTF_8);
+        InputStream bais = new ByteArrayInputStream(byteData);
+        ObjectMetadata objectMetadata = new ObjectMetadata();
+        s3.putObject(bucketName, filePath, bais, objectMetadata);
+    }
+
+    /**
+     * 获取文件内容
+     * @param bucketName
+     * @param filePath
+     * @return
+     * @throws Exception
+     */
+    private static String getObjectContent(String bucketName, String filePath) throws Exception{
+        S3Object s3Object = s3.getObject(bucketName, filePath);
+        S3ObjectInputStream s3is = s3Object.getObjectContent();
+
+        BufferedReader bufferedReader = getReader(s3is, null);
+        StringBuilder stringBuilder = new StringBuilder();
+        String line;
+        while ((line = bufferedReader.readLine()) != null) {
+            stringBuilder.append(line);
+            stringBuilder.append(System.lineSeparator());
+        }
+
+        return stringBuilder.toString();
+    }
+
+    /**
+     * 获得一个Reader
+     *
+     * @param in      输入流
+     * @param charset 字符集
+     * @return BufferedReader对象
+     */
+    private static BufferedReader getReader(InputStream in, Charset charset) {
+        if (null == in) {
+            return null;
+        }
+        InputStreamReader reader;
+        if (null == charset) {
+            reader = new InputStreamReader(in);
+        } else {
+            reader = new InputStreamReader(in, charset);
+        }
+        return new BufferedReader(reader);
+    }
+
+}

+ 232 - 0
src/main/java/cn/com/ctop/track/util/RedisUtil.java

@@ -0,0 +1,232 @@
+package cn.com.ctop.track.util;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * redis 工具类
+ * @Author Scott
+ *
+ */
+@Component
+@Slf4j
+public class RedisUtil {
+
+
+	@Autowired
+	private StringRedisTemplate redisTemplate;
+
+
+	private final String DEFAULT_KEY_PREFIX = "";
+	private final int EXPIRE_TIME = 1;
+	private final TimeUnit EXPIRE_TIME_TYPE = TimeUnit.DAYS;
+
+
+	/**
+	 * 数据缓存至redis
+	 *
+	 * @param key
+	 * @param value
+	 * @return
+	 */
+	public <K, V> void add(K key, V value) {
+		try {
+			if (value != null) {
+				redisTemplate
+						.opsForValue()
+						.set(DEFAULT_KEY_PREFIX + key, JSON.toJSONString(value));
+			}
+		} catch (Exception e) {
+			log.error(e.getMessage(), e);
+			throw new RuntimeException("数据缓存至redis失败");
+		}
+	}
+
+	/**
+	 * 数据缓存至redis并设置过期时间
+	 *
+	 * @param key
+	 * @param value
+	 * @return
+	 */
+	public <K, V> void add(K key, V value, long timeout, TimeUnit unit) {
+		try {
+			if (value != null) {
+				redisTemplate
+						.opsForValue()
+						.set(DEFAULT_KEY_PREFIX + key, JSON.toJSONString(value), timeout, unit);
+			}
+		} catch (Exception e) {
+			log.error(e.getMessage(), e);
+			throw new RuntimeException("数据缓存至redis失败");
+		}
+	}
+
+	/**
+	 * 写入 hash-set,已经是key-value的键值,不能再写入为hash-set
+	 *
+	 * @param key    must not be {@literal null}.
+	 * @param subKey must not be {@literal null}.
+	 * @param value  写入的值
+	 */
+	public <K, SK, V> void addHashCache(K key, SK subKey, V value) {
+		redisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
+	}
+
+	/**
+	 * 写入 hash-set,并设置过期时间
+	 *
+	 * @param key    must not be {@literal null}.
+	 * @param subKey must not be {@literal null}.
+	 * @param value  写入的值
+	 */
+	public <K, SK, V> void addHashCache(K key, SK subKey, V value, long timeout, TimeUnit unit) {
+		redisTemplate.opsForHash().put(DEFAULT_KEY_PREFIX + key, subKey, value);
+		redisTemplate.expire(DEFAULT_KEY_PREFIX + key, timeout, unit);
+	}
+
+	/**
+	 * 获取 hash-setvalue
+	 *
+	 * @param key    must not be {@literal null}.
+	 * @param subKey must not be {@literal null}.
+	 */
+	public <K, SK> Object getHashCache(K key, SK subKey) {
+		return  redisTemplate.opsForHash().get(DEFAULT_KEY_PREFIX + key, subKey);
+	}
+
+
+	/**
+	 * 从redis中获取缓存数据,转成对象
+	 *
+	 * @param key   must not be {@literal null}.
+	 * @param clazz 对象类型
+	 * @return
+	 */
+	public <K, V> V getObject(K key, Class<V> clazz) {
+		String value = this.get(key);
+		V result = null;
+		if (!StringUtils.isEmpty(value)) {
+			result = JSONObject.parseObject(value, clazz);
+		}
+		return result;
+	}
+
+	/**
+	 * 从redis中获取缓存数据,转成list
+	 *
+	 * @param key   must not be {@literal null}.
+	 * @param clazz 对象类型
+	 * @return
+	 */
+	public <K, V> List<V> getList(K key, Class<V> clazz) {
+		String value = this.get(key);
+		List<V> result = Collections.emptyList();
+		if (!StringUtils.isEmpty(value)) {
+			result = JSONArray.parseArray(value, clazz);
+		}
+		return result;
+	}
+
+	/**
+	 * 功能描述:Get the value of {@code key}.
+	 *
+	 * @param key must not be {@literal null}.
+	 * @return java.lang.String
+	 * @date 2021/9/19
+	 **/
+	public <K> String get(K key) {
+		String value;
+		try {
+			value = redisTemplate.opsForValue().get(DEFAULT_KEY_PREFIX + key);
+		} catch (Exception e) {
+			log.error(e.getMessage(), e);
+			throw new RuntimeException("从redis缓存中获取缓存数据失败");
+		}
+		return value;
+	}
+
+	/**
+	 * 删除key
+	 */
+	public void delete(String key) {
+		redisTemplate.delete(key);
+	}
+
+	/**
+	 * 批量删除key
+	 */
+	public void delete(Collection<String> keys) {
+		redisTemplate.delete(keys);
+	}
+
+	/**
+	 * 序列化key
+	 */
+	public byte[] dump(String key) {
+		return redisTemplate.dump(key);
+	}
+
+	/**
+	 * 是否存在key
+	 */
+	public Boolean hasKey(String key) {
+		return redisTemplate.hasKey(key);
+	}
+
+	/**
+	 * 设置过期时间
+	 */
+	public Boolean expire(String key, long timeout, TimeUnit unit) {
+		return redisTemplate.expire(key, timeout, unit);
+	}
+
+	/**
+	 * 设置过期时间
+	 */
+	public Boolean expireAt(String key, Date date) {
+		return redisTemplate.expireAt(key, date);
+	}
+
+
+	/**
+	 * 移除 key 的过期时间,key 将持久保持
+	 */
+	public Boolean persist(String key) {
+		return redisTemplate.persist(key);
+	}
+
+	/**
+	 * 返回 key 的剩余的过期时间
+	 */
+	public Long getExpire(String key, TimeUnit unit) {
+		return redisTemplate.getExpire(key, unit);
+	}
+
+	/**
+	 * 返回 key 的剩余的过期时间
+	 */
+	public Long getExpire(String key) {
+		return redisTemplate.getExpire(key);
+	}
+
+
+	/**
+	 *  返回 满足前缀 的key
+	 * @param pattern
+	 * @return
+	 */
+	public Set<String> kesy(String pattern){
+		return redisTemplate.keys(pattern);
+	}
+
+}

+ 21 - 0
src/main/java/cn/com/ctop/track/utils/BaseResponse.java

@@ -0,0 +1,21 @@
+package cn.com.ctop.track.utils;
+
+
+import com.fasterxml.jackson.annotation.JsonAlias;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class BaseResponse implements Serializable {
+    @JsonAlias("StatusCode")
+    private Integer statusCode;
+    @JsonAlias("StatusMessage")
+    private String StatusMessage;
+
+    public static BaseResponse ok() {
+        return new BaseResponse(0, "ok");
+    }
+}

+ 22 - 20
src/main/resources/application.yml

@@ -81,6 +81,7 @@ spring:
       datasource:
         master:
           url: jdbc:mysql://172.30.0.36:3390/track?characterEncoding=UTF-8&useUnicode=true&allowMultiQueries=true&useSSL=false
+        #  url: jdbc:mysql://139.186.172.149:3390/track?characterEncoding=UTF-8&useUnicode=true&allowMultiQueries=true&useSSL=false
           username: hcst
           password: hcst@2025
           driver-class-name: com.mysql.jdbc.Driver
@@ -90,26 +91,27 @@ spring:
   #          username: hcst
   #          password: hcst@2020
   #          driver-class-name: com.mysql.jdbc.Driver
-  # mongodb 配置
-  data:
-    mongodb:
-      #      uri: mongodb://root:hcst2024..@192.168.0.99:27017/admin?authSource=admin&readPreference=primaryPreferred&maxpoolsize=100&minpoolsize=10&maxidletimems=0&maxlifetimems=0&maxconnecting=5&waitqueuetimeoutms=120000
-      host: 172.30.0.5
-      port: 27017
-      #root:只在admin数据库中可用。超级账号,超级权限
-      username: root
-      password: hcst2024..
-      database: admin
-      authentication-database: admin
-      #      maxpoolsize: 100 # 池中允许的最大连接数。一旦池耗尽,任何需要连接的操作都将在阻塞队列中等待直到获取到连接, 默认: 100
-      #      minpoolsize: 10 # 池中允许的最小连接数。这些连接在空闲时将保留在池中,并且池将确保它至少包含这个最小数量 默认: 0
-      #      maxidletimems: 0 # 池连接的最大空闲时间。零值表示对空闲时间没有限制。超过其空闲时间的池连接将被关闭并在必要时由新连接替换。单位为毫秒
-      #      maxlifetimems: 0 # 池连接可以存活的最长时间。零值表示寿命没有限制。超过其生命周期的池连接将被关闭并在必要时由新连接替换。单位为毫秒
-      #      maxconnecting: 5 # 同一时刻允许的最大并行连接数。默认值是: 2。
-      #      waitqueuetimeoutms: 120000 # 阻塞队列中请求的最大等待时间,超过这个时间的请求将被拒绝。单位为毫秒,默认为: 120s
-
-      #从连接池获取连接的最大等待时间(毫秒)
-      max-wait-time: 22000
+  redis:
+    # 地址
+    host: 172.30.0.16
+    # 端口,默认为6379
+    port: 6379
+    # 数据库索引
+    database: 1
+    # 密码 hcst@2024
+    password: hcst@2024
+    # 连接超时时间
+    timeout: 10s
+    lettuce:
+      pool:
+        # 连接池中的最小空闲连接
+        min-idle: 0
+        # 连接池中的最大空闲连接
+        max-idle: 8
+        # 连接池的最大数据库连接数
+        max-active: 8
+        # #连接池最大阻塞等待时间(使用负值表示没有限制)
+        max-wait: -1ms