ソースを参照

redis bug调整

wangb 2 年 前
コミット
fd2a1bac72

+ 13 - 0
web/backmanagerconfig-xktj/pom.xml

@@ -55,6 +55,19 @@
             <artifactId>hutool-all</artifactId>
             <version>5.1.1</version>
         </dependency>
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+            <version>3.2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.data</groupId>
+            <artifactId>spring-data-redis</artifactId>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 44 - 0
web/backmanagerconfig-xktj/src/main/java/com/gyee/backconfig/cache/RedisAutoConfiguration.java

@@ -0,0 +1,44 @@
+package com.gyee.backconfig.cache;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
+
+import java.net.UnknownHostException;
+
+@Configuration
+@ConditionalOnClass(RedisOperations.class)
+@EnableConfigurationProperties(RedisProperties.class)
+public class RedisAutoConfiguration {
+
+
+    @Bean
+    @ConditionalOnMissingBean(name = "redisTemplate")
+    public RedisTemplate<Object, Object> redisTemplate(
+            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
+        RedisTemplate<Object, Object> template = new RedisTemplate<>();
+        template.setConnectionFactory(redisConnectionFactory);
+        return template;
+
+    }
+
+
+    @Bean
+    @ConditionalOnMissingBean
+    public StringRedisTemplate stringRedisTemplate(
+            RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
+        StringRedisTemplate template = new StringRedisTemplate();
+        template.setConnectionFactory(redisConnectionFactory);
+        return template;
+
+    }
+
+
+}

+ 46 - 0
web/backmanagerconfig-xktj/src/main/java/com/gyee/backconfig/cache/RedisConfig.java

@@ -0,0 +1,46 @@
+package com.gyee.backconfig.cache;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+/**
+
+/**
+ *@ClassName RedisConfig
+ *@Description TODO
+ *@Author 谢生杰
+ *@Date 2020/9/14 10:42
+ *@Version 1.0
+ **/
+@Configuration
+public class RedisConfig {
+    @Bean
+    @SuppressWarnings("all")
+    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
+        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+        template.setConnectionFactory(factory);
+        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
+        ObjectMapper om = new ObjectMapper();
+        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
+        jackson2JsonRedisSerializer.setObjectMapper(om);
+        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
+        // key采用String的序列化方式
+        template.setKeySerializer(stringRedisSerializer);
+        // hash的key也采用String的序列化方式
+        template.setHashKeySerializer(stringRedisSerializer);
+        // value序列化方式采用jackson
+        template.setValueSerializer(jackson2JsonRedisSerializer);
+        // hash的value序列化方式采用jackson
+        template.setHashValueSerializer(jackson2JsonRedisSerializer);
+        template.afterPropertiesSet();
+        return template;
+    }
+}

+ 4 - 3
web/backmanagerconfig-xktj/src/main/java/com/gyee/backconfig/service/auto/impl/ProBasicWindpowerstationServiceImpl.java

@@ -1,6 +1,5 @@
 package com.gyee.backconfig.service.auto.impl;
 
-import cn.hutool.core.math.MathUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.TypeReference;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -10,7 +9,9 @@ import com.gyee.backconfig.model.auto.ProBasicWindpowerstation;
 import com.gyee.backconfig.mapper.auto.ProBasicWindpowerstationMapper;
 import com.gyee.backconfig.service.auto.IProBasicWindpowerstationService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.gyee.backconfig.service.auto.RedisService;
 import com.gyee.backconfig.until.realtimedate.EdosUtil;
+import com.gyee.backconfig.until.realtimedate.MathUtil;
 import com.gyee.backconfig.vo.WindpowerstationAdmVo;
 import com.gyee.common.model.PointData;
 import com.gyee.common.model.StringUtils;
@@ -120,8 +121,8 @@ public class ProBasicWindpowerstationServiceImpl extends ServiceImpl<ProBasicWin
                 vo.setSystemManufact(i.getSystemManufact());//厂家
                 vo.setAddress(i.getAddress());//地址
                 if(null != pointList && pointList.size()>=2){
-                    vo.setRfdl(com.gyee.backconfig.until.realtimedate.MathUtil.twoBit(pointList.get(0).getPointValueInDouble()));
-                    vo.setRfdl(com.gyee.backconfig.until.realtimedate.MathUtil.twoBit(pointList.get(1).getPointValueInDouble()));
+                    vo.setRfdl(MathUtil.twoBit(pointList.get(0).getPointValueInDouble()));
+                    vo.setRfdl(MathUtil.twoBit(pointList.get(1).getPointValueInDouble()));
                 }
                 admvos.add(vo);
             } catch (Exception e) {

+ 0 - 49
web/backmanagerconfig-xktj/src/main/java/com/gyee/backconfig/service/auto/impl/RedisService.java

@@ -1,49 +0,0 @@
-package com.gyee.backconfig.service.auto.impl;
-
-public interface RedisService {
-
-    /**
-     * set存数据
-     * @param key
-     * @param value
-     * @return
-     */
-    boolean set(String key, String value);
-
-    /**
-     * get获取数据
-     * @param key
-     * @return
-     */
-    String get(String key);
-
-//    void getList(String key,int start,int end);
-
-    /**
-     * 设置有效天数
-     * @param key
-     * @param expire
-     * @return
-     */
-    boolean expire(String key, long expire);
-
-    /**
-     * 移除数据
-     * @param key
-     * @return
-     */
-    boolean remove(String key);
-
-
-    /**
-     * 插入对象
-     */
-    void setObject(Object o);
-
-
-    boolean hasKey(String key);
-
-
-    void select(Integer dbIndex);
-
-}

+ 125 - 0
web/backmanagerconfig-xktj/src/main/java/com/gyee/backconfig/service/auto/impl/RedisServiceImpl.java

@@ -0,0 +1,125 @@
+package com.gyee.backconfig.service.auto.impl;
+
+import com.gyee.backconfig.service.auto.RedisService;
+import org.springframework.dao.DataAccessException;
+import org.springframework.data.redis.connection.RedisConnection;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.lang.reflect.Field;
+import java.util.concurrent.TimeUnit;
+
+@Service("redisService")
+public class RedisServiceImpl implements RedisService {
+
+    @Resource
+    private RedisTemplate<String, ?> redisTemplate;
+
+    @Override
+    public boolean set(final String key, final String value) {
+        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
+            @Override
+            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
+                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
+                connection.set(serializer.serialize(key), serializer.serialize(value));
+                return true;
+            }
+        });
+        return result;
+    }
+
+    @Override
+    public String get(final String key) {
+        String result = redisTemplate.execute(new RedisCallback<String>() {
+            @Override
+            public String doInRedis(RedisConnection connection) throws DataAccessException {
+                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
+                byte[] value = connection.get(serializer.serialize(key));
+                return serializer.deserialize(value);
+            }
+        });
+        return result;
+    }
+
+
+
+    @Override
+    public boolean expire(final String key, long expire) {
+        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public boolean remove(final String key) {
+        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
+            @Override
+            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
+                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
+                connection.del(key.getBytes());
+                return true;
+            }
+        });
+        return result;
+    }
+
+    @Override
+    public boolean hasKey(final String key) {
+        try {
+            return redisTemplate.hasKey(key);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    @Override
+    public void setObject(Object obj) {
+        Field[] fields = obj.getClass().getDeclaredFields();
+        for(int i = 0 , len = fields.length; i < len; i++) {
+            // 对于每个属性,获取属性名
+            String varName = fields[i].getName();
+            try {
+                // 获取原来的访问控制权限
+                boolean accessFlag = fields[i].isAccessible();
+                // 修改访问控制权限
+                fields[i].setAccessible(true);
+                // 获取在对象f中属性fields[i]对应的对象中的变量
+                Object o;
+                try {
+                    o = fields[i].get(obj);
+                    if (o!=null){
+                        set(varName, String.valueOf(o));
+                    }else {
+                        set(varName,"");
+                    }
+
+                    //System.err.println("传入的对象中包含一个如下的变量:" + varName + " = " + o);
+                } catch (IllegalAccessException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+                // 恢复访问控制权限
+                fields[i].setAccessible(accessFlag);
+            } catch (IllegalArgumentException ex) {
+                ex.printStackTrace();
+            }
+        }
+
+    }
+
+    public void select(Integer dbIndex) {
+        if (dbIndex == null || dbIndex > 15 || dbIndex < 0) {
+            dbIndex = 0;
+        }
+        LettuceConnectionFactory jedisConnectionFactory = (LettuceConnectionFactory) redisTemplate
+                .getConnectionFactory();
+        jedisConnectionFactory.setDatabase(dbIndex);
+        redisTemplate.setConnectionFactory(jedisConnectionFactory);
+        jedisConnectionFactory.afterPropertiesSet();
+//        jedisConnectionFactory.resetConnection();
+
+    }
+}

+ 6 - 6
web/backmanagerconfig-xktj/src/main/resources/application-test.yml

@@ -6,8 +6,8 @@ server:
 spring:
   application:
     name: back-config
-#  main:
-#    allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
+  main:
+    allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
 #  cloud:
 #    nacos:
 #      discovery:
@@ -18,10 +18,10 @@ spring:
 #      username: nacos
 #      password: nacos
 #  #redis集群
-#  redis:
-#    host: 10.81.3.155
-#    port: 6379
-#    timeout: 100000
+  redis:
+    host: 120.46.148.180
+    port: 6379
+    timeout: 100000
 #    #    集群环境打开下面注释,单机不需要打开
 #    #    cluster:
 #    #      集群信息