Sfoglia il codice sorgente

去除热部署,对缓存调试有影响,增加redis缓存机制

gaodm 6 anni fa
parent
commit
b3af3fc0a0

+ 6 - 6
admin-service/pom.xml

@@ -83,12 +83,12 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
     </dependencies>
 
     <build>

+ 6 - 6
bi-service/pom.xml

@@ -128,12 +128,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>

+ 6 - 6
config-server/pom.xml

@@ -46,12 +46,12 @@
             <!--<groupId>org.springframework.cloud</groupId>-->
             <!--<artifactId>spring-cloud-starter-bus-amqp</artifactId>-->
         <!--</dependency>-->
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

+ 7 - 7
config-server/src/main/resources/application.yml

@@ -30,13 +30,13 @@ spring:
 #    password: guest
 #    publisher-confirms: true
 #    virtual-host: /
-  #热部署设置
-  thymeleaf:
-    cache: true #关闭缓存,及时刷新
-    devtools:
-      restart:
-        enabled: true #热部署生效
-        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)
+#  #热部署设置
+#  thymeleaf:
+#    cache: true #关闭缓存,及时刷新
+#    devtools:
+#      restart:
+#        enabled: true #热部署生效
+#        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)
 
 # port
 server:

+ 7 - 7
config-server/src/main/resources/shared/application-dev.yml

@@ -55,10 +55,10 @@ spring:
       enabled: true
       trace:
         enabled: true
-  #热部署设置
-  thymeleaf:
-    cache: true #关闭缓存,及时刷新
-    devtools:
-      restart:
-        enabled: true #热部署生效
-        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)
+#  #热部署设置
+#  thymeleaf:
+#    cache: true #关闭缓存,及时刷新
+#    devtools:
+#      restart:
+#        enabled: true #热部署生效
+#        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)

+ 7 - 7
config-server/src/main/resources/shared/application-local.yml

@@ -55,10 +55,10 @@ spring:
       enabled: true
       trace:
         enabled: true
-  #热部署设置
-  thymeleaf:
-    cache: true #关闭缓存,及时刷新
-    devtools:
-      restart:
-        enabled: true #热部署生效
-        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)
+#  #热部署设置
+#  thymeleaf:
+#    cache: true #关闭缓存,及时刷新
+#    devtools:
+#      restart:
+#        enabled: true #热部署生效
+#        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)

+ 16 - 0
config-server/src/main/resources/shared/user-service-dev.yml

@@ -75,6 +75,22 @@ spring:
     publisher-confirms: true
     virtual-host: /
 
+  #redis
+  redis:
+    database:
+      cache: 0 # Redis缓存索引
+      sms: 1 # Redis短信索引
+    host: 192.168.2.236  #Redis服务器地址
+    port: 6379 # Redis服务器连接端口
+    password: lantone # Redis服务器连接密码(默认为空)
+    jedis:
+      pool:
+        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
+        max-idle: 5 # 连接池中的最大空闲连接
+        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
+        min-idle: 0 # 连接池中的最小空闲连接
+    timeout: 0 # 连接超时时间(毫秒)
+
 #mybatis
 mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml

+ 16 - 0
config-server/src/main/resources/shared/user-service-local.yml

@@ -75,6 +75,22 @@ spring:
     publisher-confirms: true
     virtual-host: /
 
+  #redis
+  redis:
+    database:
+      cache: 0 # Redis缓存索引
+      sms: 1 # Redis短信索引
+    host: 127.0.0.1  #Redis服务器地址
+    port: 6379 # Redis服务器连接端口
+    password: # Redis服务器连接密码(默认为空)
+    jedis:
+      pool:
+        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
+        max-idle: 5 # 连接池中的最大空闲连接
+        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
+        min-idle: 0 # 连接池中的最小空闲连接
+    timeout: 0 # 连接超时时间(毫秒)
+
 #mybatis
 mybatis-plus:
   mapper-locations: classpath:/mapper/*Mapper.xml

+ 6 - 6
diagbotman-service/pom.xml

@@ -128,12 +128,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>

+ 6 - 6
eureka-server/pom.xml

@@ -47,12 +47,12 @@
             <groupId>net.logstash.logback</groupId>
             <artifactId>logstash-logback-encoder</artifactId>
         </dependency>
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
     </dependencies>
 
     <build>

+ 7 - 7
eureka-server/src/main/resources/bootstrap.yml

@@ -1,10 +1,10 @@
 spring:
   profiles:
     active: local
-  #热部署设置
-  thymeleaf:
-    cache: true #关闭缓存,及时刷新
-    devtools:
-      restart:
-        enabled: true #热部署生效
-        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)
+#  #热部署设置
+#  thymeleaf:
+#    cache: true #关闭缓存,及时刷新
+#    devtools:
+#      restart:
+#        enabled: true #热部署生效
+#        additional-paths: src/main/java #设置重启的目录,添加那个目录的文件需要restart(监听目录)

+ 6 - 6
feedback-service/pom.xml

@@ -128,12 +128,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>

+ 6 - 6
gateway-service/pom.xml

@@ -61,12 +61,12 @@
             <optional>true</optional>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!--swagger-->
         <dependency>

+ 6 - 6
knowledge-service/pom.xml

@@ -128,12 +128,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>

+ 6 - 6
log-service/pom.xml

@@ -128,12 +128,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>

+ 6 - 6
monitor-service/pom.xml

@@ -70,12 +70,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
     </dependencies>
 
     <build>

+ 6 - 6
uaa-service/pom.xml

@@ -80,12 +80,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>

+ 34 - 6
user-service/pom.xml

@@ -128,12 +128,12 @@
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
         </dependency>
 
-        <!--热部署引用-->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
+        <!--&lt;!&ndash;热部署引用&ndash;&gt;-->
+        <!--<dependency>-->
+            <!--<groupId>org.springframework.boot</groupId>-->
+            <!--<artifactId>spring-boot-devtools</artifactId>-->
+            <!--<optional>true</optional>-->
+        <!--</dependency>-->
 
         <!-- mybatis-plus begin -->
         <dependency>
@@ -161,6 +161,34 @@
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
         </dependency>
+        <!--redis设置-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>redis.clients</groupId>
+                    <artifactId>jedis</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>io.lettuce</groupId>
+                    <artifactId>lettuce-core</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+            <version>2.9.0</version>
+        </dependency>
+        <!--spring2.0集成redis所需common-pool2-->
+        <!-- 必须加上,jedis依赖此  -->
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-pool2</artifactId>
+            <version>2.6.0</version>
+        </dependency>
 
     </dependencies>
 

+ 166 - 0
user-service/src/main/java/com/diagbot/config/RedisConfig.java

@@ -0,0 +1,166 @@
+package com.diagbot.config;
+
+import com.diagbot.util.BeanUtil;
+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.springframework.beans.factory.annotation.Autowired;
+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.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
+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.StringRedisSerializer;
+import redis.clients.jedis.JedisPoolConfig;
+
+@Configuration
+@EnableCaching
+@Slf4j
+public class RedisConfig extends CachingConfigurerSupport {
+
+    @Value("${spring.redis.database.cache}")
+    private String databaseCache;
+    @Value("${spring.redis.database.sms}")
+    private String databaseSms;
+    @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.jedis.pool.max-active}")
+    private int maxActive;
+    @Value("${spring.redis.jedis.pool.max-idle}")
+    private int maxIdle;
+    @Value("${spring.redis.jedis.pool.max-wait}")
+    private long maxWaitMillis;
+    @Value("${spring.redis.jedis.pool.min-idle}")
+    private int minIdle;
+
+    @Autowired
+    private JedisConnectionFactory jedisConnectionFactory;
+
+    @Bean
+    public JedisPoolConfig getRedisConfig(){
+        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
+        jedisPoolConfig.setMaxTotal(maxActive);
+        jedisPoolConfig.setMaxIdle(maxIdle);
+        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
+        jedisPoolConfig.setMinIdle(minIdle);
+        return jedisPoolConfig;
+    }
+
+    @Bean(destroyMethod = "destroy")
+    public JedisConnectionFactory redisConnectionFactory() {
+        log.info("Create JedisConnectionFactory successful");
+        JedisConnectionFactory factory = new JedisConnectionFactory();
+        factory.setHostName(host);
+        factory.setPort(port);
+        factory.setTimeout(timeout);
+        factory.setPassword(password);
+        factory.setDatabase(Integer.valueOf(databaseCache));
+        JedisPoolConfig poolConfig = getRedisConfig();
+        factory.setPoolConfig(poolConfig);
+        return factory;
+    }
+
+    @Bean
+    @Override
+    public CacheManager cacheManager() {
+        // 初始化缓存管理器,在这里我们可以缓存的整体过期时间什么的,我这里默认没有配置
+        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
+                .RedisCacheManagerBuilder
+                .fromConnectionFactory(jedisConnectionFactory);
+        return builder.build();
+    }
+
+    @Bean(name = "springSessionDefaultRedisSerializer")
+    public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer() {
+        return new GenericJackson2JsonRedisSerializer();
+    }
+
+    @Bean
+    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
+        redisTemplate.setConnectionFactory(jedisConnectionFactory);
+
+        // value值的序列化
+        redisTemplate.setValueSerializer(getSerializer());
+        redisTemplate.setHashValueSerializer(getSerializer());
+        // key的序列化采用StringRedisSerializer
+        redisTemplate.setKeySerializer(new StringRedisSerializer());
+        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+
+    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对象,只是这是lambda表达式的写法,我感觉很好用,大家感兴趣可以去了解下
+        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;
+        };
+    }
+
+
+    /**
+     * sms验证使用的redis
+     *
+     * @param factory
+     * @return
+     */
+    @Bean(name = "redisTemplateForSms")
+    public RedisTemplate<String, Object> redisTemplateForSms(JedisConnectionFactory factory) {
+        return getRedisTemplate(factory, Integer.valueOf(databaseSms));
+    }
+
+    private RedisTemplate<String, Object> getRedisTemplate(JedisConnectionFactory factory, Integer database) {
+        JedisConnectionFactory factory2 = new JedisConnectionFactory();
+        BeanUtil.copyProperties(factory, factory2);
+        factory2.setDatabase(database);
+
+        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
+        redisTemplate.setConnectionFactory(factory2);
+
+        // value值的序列化
+        redisTemplate.setValueSerializer(getSerializer());
+        redisTemplate.setHashValueSerializer(getSerializer());
+        // key的序列化采用StringRedisSerializer
+        redisTemplate.setKeySerializer(new StringRedisSerializer());
+        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
+        redisTemplate.afterPropertiesSet();
+        return redisTemplate;
+    }
+}
+ 

+ 1 - 0
user-service/src/main/java/com/diagbot/exception/CommonExceptionHandler.java

@@ -32,6 +32,7 @@ public class CommonExceptionHandler {
         resp.code = -1;
         resp.msg = e.getMessage();
         log.error("【系统异常】:" + e.getMessage());
+        e.printStackTrace();
         return new ResponseEntity(resp, HttpStatus.OK);
     }
 

+ 47 - 0
user-service/src/main/java/com/diagbot/facade/UserFacade.java

@@ -8,11 +8,18 @@ import com.diagbot.entity.User;
 import com.diagbot.exception.CommonException;
 import com.diagbot.exception.ErrorCode;
 import com.diagbot.service.impl.UserServiceImpl;
+import com.diagbot.util.UserUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.CachePut;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.security.crypto.factory.PasswordEncoderFactories;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Component;
 
+import java.io.Serializable;
+import java.util.Date;
+
 /**
  * @Description: 用户业务层
  * @author: gaodm
@@ -32,6 +39,7 @@ public class UserFacade extends UserServiceImpl {
         return  user;
     }
 
+    @Cacheable(value = "UserName", key = "'username:'+#p0")
     public User getUserInfo(String username){
         return this.findByName(username);
     }
@@ -56,4 +64,43 @@ public class UserFacade extends UserServiceImpl {
         loginDTO.setToken(jwt.getAccess_token());
         return RespDTO.onSuc(loginDTO);
     }
+
+    private static final String CACHE_NAME = "UserInfo";
+
+    //缓存演示
+    @CachePut(value = CACHE_NAME, key = "'user:'+#p0['id']")
+    public User saveUser(User user){
+        PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
+        String entryPassword= passwordEncoder.encode(user.getPassword());
+        user.setPassword(entryPassword);
+        user.setGmtCreate(new Date());
+        user.setCreator(this.getUserInfo(UserUtils.getCurrentPrinciple()).getId().toString());
+        super.save(user);
+        return user;
+    }
+
+    @CachePut(value = CACHE_NAME, key = "'user:'+#p0['id']")
+    public User updateUser(User user){
+        PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
+        String entryPassword= passwordEncoder.encode(user.getPassword());
+        user.setPassword(entryPassword);
+        user.setGmtModified(new Date());
+        user.setModifier(this.getUserInfo(UserUtils.getCurrentPrinciple()).getId().toString());
+        super.updateById(user);
+        return user;
+    }
+
+    @Override
+    @Cacheable(value = CACHE_NAME, key = "'user:'+#p0")
+    public User getById(Serializable id){
+        return super.getById(id);
+    }
+
+    @Override
+    @CacheEvict(value = CACHE_NAME, key = "'user:'+#p0")
+    public boolean removeById(Serializable id){
+        return super.removeById(id);
+    }
+
+
 }

+ 2 - 1
user-service/src/main/java/com/diagbot/service/impl/UserServiceImpl.java

@@ -6,6 +6,7 @@ import com.diagbot.entity.User;
 import com.diagbot.mapper.UserMapper;
 import com.diagbot.service.UserService;
 import org.apache.ibatis.annotations.Param;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -22,7 +23,7 @@ import java.util.Map;
  */
 @Service
 public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
-    @Override
+
     public User findByName(String username){
         return baseMapper.findByName(username);
     }

+ 1 - 1
user-service/src/main/java/com/diagbot/util/UserUtils.java

@@ -25,7 +25,7 @@ public class UserUtils {
     }
 
     /**
-     * 获取当前请求的用户Id
+     * 获取当前请求的用户名称
      * @return
      */
     public static String getCurrentPrinciple() {

+ 58 - 0
user-service/src/main/java/com/diagbot/web/UserController.java

@@ -11,6 +11,7 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -90,4 +91,61 @@ public class UserController {
 //    public void test(){
 //        rabbitTemplate.convertAndSend(RabbitConfig.queueName, "Hello from RabbitMQ!");
 //    }
+
+    /**
+     * 增加用户
+     * @param user 用户信息
+     * @return 增加是否成功
+     */
+    @ApiOperation(value = "增加用户", notes = "username和password为必选项")
+    @PostMapping("/addUser")
+    @SysLogger("addUser")
+    @Transactional
+    public RespDTO addUser(@RequestBody User user){
+        //参数判读省略,判读该用户在数据库是否已经存在省略
+        userFacade.saveUser(user);
+        //        throw new CommonException(ErrorCode.FAIL);
+        return RespDTO.onSuc(user);
+    }
+
+    /**
+     * 修改用户
+     * @param user 用户信息
+     * @return 修改是否成功
+     */
+    @ApiOperation(value = "修改用户", notes = "username和password为必选项")
+    @PostMapping("/updateUser")
+    @SysLogger("updateUser")
+    @Transactional
+    public RespDTO updateUser(@RequestBody User user){
+        //参数判读省略,判读该用户在数据库是否已经存在省略
+        userFacade.updateUser(user);
+        return RespDTO.onSuc(user);
+    }
+
+    /**
+     * 查询用户信息
+     * @param id 用户ID
+     * @return 用户信息
+     */
+    @ApiOperation(value = "查询用户", notes = "查询用户")
+    @GetMapping("/getUser")
+    @SysLogger("getUser")
+    @Transactional
+    public RespDTO getUser(Long id){
+        return RespDTO.onSuc(userFacade.getById(id));
+    }
+
+    /**
+     * 删除用户
+     * @param id 用户ID
+     * @return 删除是否成功
+     */
+    @ApiOperation(value = "删除用户", notes = "删除用户")
+    @PostMapping("/delUser")
+    @SysLogger("delUser")
+    @Transactional
+    public RespDTO delUser(Long id){
+        return RespDTO.onSuc(userFacade.removeById(id));
+    }
 }