Pārlūkot izejas kodu

知识库维护加入缓存和消息体系

gaodm 5 gadi atpakaļ
vecāks
revīzija
75eca2195a

+ 22 - 1
config-server/src/main/resources/shared/knowledgeman-service-dev.yml

@@ -61,7 +61,13 @@ spring:
       bindings:
         outputLog:
           destination: myLog
-  #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        outputWord:
+          destination: myWord
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        inputWord:
+          destination: myWord
+          group: wordReceiveGroup
 
   #mq
   rabbitmq:
@@ -72,6 +78,21 @@ spring:
     publisher-confirms: true
     virtual-host: /
 
+  #redis
+  redis:
+    database:
+      cache: 6 # Redis缓存索引
+    host: 192.168.2.236  #Redis服务器地址
+    port: 6379 # Redis服务器连接端口
+    password: lantone # Redis服务器连接密码(默认为空)
+    lettuce:
+      pool:
+        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
+        max-idle: 5 # 连接池中的最大空闲连接
+        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
+        min-idle: 0 # 连接池中的最小空闲连接
+    timeout: 20000 # 连接超时时间(毫秒)
+
 #mybatis
 mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml

+ 22 - 1
config-server/src/main/resources/shared/knowledgeman-service-local.yml

@@ -61,7 +61,13 @@ spring:
       bindings:
         outputLog:
           destination: myLog
-  #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        outputWord:
+          destination: myWord
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        inputWord:
+          destination: myWord
+          group: wordReceiveGroup
 
   #mq
   rabbitmq:
@@ -72,6 +78,21 @@ spring:
     publisher-confirms: true
     virtual-host: /
 
+  #redis
+  redis:
+    database:
+      cache: 9 # Redis缓存索引
+    host: 192.168.2.236  #Redis服务器地址
+    port: 6379 # Redis服务器连接端口
+    password: lantone # Redis服务器连接密码(默认为空)
+    lettuce:
+      pool:
+        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
+        max-idle: 5 # 连接池中的最大空闲连接
+        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
+        min-idle: 0 # 连接池中的最小空闲连接
+    timeout: 20000 # 连接超时时间(毫秒)
+
 #mybatis
 mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml

+ 22 - 1
config-server/src/main/resources/shared/knowledgeman-service-pro.yml

@@ -61,7 +61,13 @@ spring:
       bindings:
         outputLog:
           destination: myLog
-  #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        outputWord:
+          destination: myWord
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        inputWord:
+          destination: myWord
+          group: wordReceiveGroup
 
   #mq
   rabbitmq:
@@ -72,6 +78,21 @@ spring:
     publisher-confirms: true
     virtual-host: /
 
+  #redis
+  redis:
+    database:
+      cache: 6 # Redis缓存索引
+    host: 192.168.2.236  #Redis服务器地址
+    port: 6379 # Redis服务器连接端口
+    password: lantone # Redis服务器连接密码(默认为空)
+    lettuce:
+      pool:
+        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
+        max-idle: 5 # 连接池中的最大空闲连接
+        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
+        min-idle: 0 # 连接池中的最小空闲连接
+    timeout: 20000 # 连接超时时间(毫秒)
+
 #mybatis
 mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml

+ 22 - 1
config-server/src/main/resources/shared/knowledgeman-service-test.yml

@@ -61,7 +61,13 @@ spring:
       bindings:
         outputLog:
           destination: myLog
-  #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        outputWord:
+          destination: myWord
+        #          contentType: text/plain      # 实体 json string 在传递的类型装换 查看 http://docs.spring
+        inputWord:
+          destination: myWord
+          group: wordReceiveGroup
 
   #mq
   rabbitmq:
@@ -72,6 +78,21 @@ spring:
     publisher-confirms: true
     virtual-host: /
 
+  #redis
+  redis:
+    database:
+      cache: 6 # Redis缓存索引
+    host: 192.168.2.236  #Redis服务器地址
+    port: 6379 # Redis服务器连接端口
+    password: lantone # Redis服务器连接密码(默认为空)
+    lettuce:
+      pool:
+        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
+        max-idle: 5 # 连接池中的最大空闲连接
+        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
+        min-idle: 0 # 连接池中的最小空闲连接
+    timeout: 20000 # 连接超时时间(毫秒)
+
 #mybatis
 mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml

+ 11 - 0
knowledgeman-service/pom.xml

@@ -158,6 +158,17 @@
             <version>3.8.0</version>
         </dependency>
 
+        <!--redis设置-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <!-- 必须加上,jedis依赖此  -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>net.sourceforge.jexcelapi</groupId>
             <artifactId>jxl</artifactId>

+ 169 - 0
knowledgeman-service/src/main/java/com/diagbot/config/RedisConfigurer.java

@@ -0,0 +1,169 @@
+package com.diagbot.config;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.interceptor.KeyGenerator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
+import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
+import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.RedisSerializationContext;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.time.Duration;
+
+@Configuration
+@EnableCaching
+@Slf4j
+public class RedisConfigurer extends CachingConfigurerSupport {
+
+    @Value("${spring.redis.database.cache}")
+    private String databaseCache;
+    @Value("${spring.redis.host}")
+    private String host;
+    @Value("${spring.redis.password}")
+    private String password;
+    @Value("${spring.redis.port}")
+    private int port;
+    @Value("${spring.redis.timeout}")
+    private int timeout;
+    @Value("${spring.redis.lettuce.pool.max-active}")
+    private int maxActive;
+    @Value("${spring.redis.lettuce.pool.max-idle}")
+    private int maxIdle;
+    @Value("${spring.redis.lettuce.pool.max-wait}")
+    private long maxWaitMillis;
+    @Value("${spring.redis.lettuce.pool.min-idle}")
+    private int minIdle;
+
+    @Autowired
+    @Qualifier("factoryForCache")
+    private LettuceConnectionFactory lettuceConnectionFactory;
+
+    @Bean
+    public GenericObjectPoolConfig getRedisConfig() {
+        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
+        poolConfig.setMaxTotal(maxActive);
+        poolConfig.setMaxIdle(maxIdle);
+        poolConfig.setMaxWaitMillis(maxWaitMillis);
+        poolConfig.setMinIdle(minIdle);
+        return poolConfig;
+    }
+
+    @Bean
+    @Override
+    public CacheManager cacheManager() {
+        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
+                // 设置 key为string序列化
+                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
+                // 设置value为json序列化
+                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getSerializer()))
+                // 不缓存空值
+                .disableCachingNullValues();
+        RedisCacheManager cacheManager = RedisCacheManager.builder(lettuceConnectionFactory)
+                .cacheDefaults(redisCacheConfiguration)
+                .transactionAware()
+                .build();
+        cacheManager.afterPropertiesSet();
+        log.info("RedisCacheManager config success");
+        return cacheManager;
+    }
+
+    @Bean(name = "springSessionDefaultRedisSerializer")
+    public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer() {
+        return new GenericJackson2JsonRedisSerializer();
+    }
+
+    /**
+     * 缓存使用的redis
+     *
+     * @return
+     */
+    @Bean("factoryForCache")
+    @Primary
+    public LettuceConnectionFactory redisConnectionFactory() {
+        return getRedisConnectionFactory(Integer.valueOf(databaseCache));
+    }
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate() {
+        return getRedisTemplate(lettuceConnectionFactory);
+    }
+
+    private Jackson2JsonRedisSerializer getSerializer() {
+        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);
+        return jackson2JsonRedisSerializer;
+    }
+
+    @Bean
+    @Override
+    public KeyGenerator keyGenerator() {
+        //  设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
+        // 使用:进行分割,可以很多显示出层级关系
+        // 这里其实就是new了一个KeyGenerator对象
+        return (target, method, params) -> {
+            StringBuilder sb = new StringBuilder();
+            sb.append(target.getClass().getName());
+            sb.append(":");
+            sb.append(method.getName());
+            for (Object obj : params) {
+                sb.append(":" + String.valueOf(obj));
+            }
+            String rsToUse = String.valueOf(sb);
+            log.info("自动生成Redis Key -> [{}]", rsToUse);
+            return rsToUse;
+        };
+    }
+
+    private LettuceConnectionFactory getRedisConnectionFactory(Integer database) {
+        RedisStandaloneConfiguration connection = new RedisStandaloneConfiguration();
+        connection.setHostName(host);
+        connection.setPort(port);
+        connection.setPassword(password);
+        connection.setDatabase(database);
+        GenericObjectPoolConfig poolConfig = getRedisConfig();
+        LettuceClientConfiguration builder = LettucePoolingClientConfiguration.builder()
+                .commandTimeout(Duration.ofMillis(timeout))
+                .poolConfig(poolConfig)
+                .shutdownTimeout(Duration.ZERO)
+                .build();
+        LettuceConnectionFactory factory = new LettuceConnectionFactory(connection, builder);
+        return factory;
+    }
+
+    private RedisTemplate<String, Object> getRedisTemplate(LettuceConnectionFactory factory) {
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
+        redisTemplate.setConnectionFactory(factory);
+
+        // value值的序列化
+        redisTemplate.setValueSerializer(getSerializer());
+        redisTemplate.setHashValueSerializer(getSerializer());
+        // key的序列化采用StringRedisSerializer
+        redisTemplate.setKeySerializer(new StringRedisSerializer());
+        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+}
+ 

+ 9 - 0
knowledgeman-service/src/main/java/com/diagbot/rabbit/MyProcessor.java

@@ -20,4 +20,13 @@ public interface MyProcessor {
 
     @Output(OUTPUT_LOG)
     MessageChannel outputLog();
+
+    String INPUT_WORD = "inputWord";
+    String OUTPUT_WORD = "outputWord";
+
+    @Input(INPUT_WORD)
+    SubscribableChannel inputWord();
+
+    @Output(OUTPUT_WORD)
+    MessageChannel outputWord();
 }

+ 18 - 0
knowledgeman-service/src/main/java/com/diagbot/rabbit/MyReceiver.java

@@ -0,0 +1,18 @@
+package com.diagbot.rabbit;
+
+import org.springframework.cloud.stream.annotation.EnableBinding;
+import org.springframework.cloud.stream.annotation.StreamListener;
+
+/**
+ * @Description: 消费者
+ * @author: gaodm
+ * @time: 2018/8/29 14:02
+ */
+@EnableBinding({ MyProcessor.class })
+public class MyReceiver {
+
+    @StreamListener(MyProcessor.INPUT_WORD)
+    public void inputWord(String message) {
+        System.out.println("Received <" + "重新加载词库" + ">" + message);
+    }
+}

+ 8 - 0
knowledgeman-service/src/main/java/com/diagbot/rabbit/MySender.java

@@ -24,4 +24,12 @@ public class MySender {
     public void outputLogSend(SysLog sysLog) {
         outputLog.send(MessageBuilder.withPayload(GsonUtil.toJson(sysLog)).build());
     }
+
+    @Autowired
+    @Qualifier("outputWord")
+    MessageChannel outputWord;
+
+    public void outputWordSend(String type) {
+        outputWord.send(MessageBuilder.withPayload(type).build());
+    }
 }