HRM微服务项目day-02

HRM项目Day02-课程类型菜单树、集成redis缓存

1. 课程类型的菜单树展示

domain

@TableName("t_course_type")
public class CourseType extends Model<CourseType> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private Long createTime;
    private Long updateTime;
    /**
     * 存放子分类
     */
    @TableField(exist = false) //数据库不存在的字段
    private List<CourseType> children = new ArrayList<>();
    /**
     * 类型名
     */
    private String name;
    /**
     * 父ID
     */
    private Long pid;
    /**
     * 图标
     */
    private String logo;
    /**
     * 描述
     */
    private String description;
    private Integer sortIndex;
    /**
     * 路径
     */
    private String path;
    /**
     * 课程数量
     */
    private Integer totalCount;

    public List<CourseType> getChildren() {
        return children;
    }
    ...
}

@TableField(exist = false) mybatis-plus中的注解表示数据库中没有该字段,为临时属性。

service实现类中:

查询所有类型,手动封装成结构树,思路:先查询顶级节点(pid==0),在查询所有的子节点,通过每一个子节点去找到对应的父节点!

    public List<CourseType> treeData() {
        //1. 从数据库中查询出所有的数据
        List<CourseType> allCourseTypes = baseMapper.selectList(null);
        //2. pid==0的作为顶层节点
        List<CourseType> topNode = new ArrayList<>();
        for (CourseType everyCourseType : allCourseTypes) {
            if(everyCourseType.getPid().longValue()==0){
                topNode.add(everyCourseType);
            }else{
                //3. 不是顶层节点,每一个节点去寻找对应的父节点
                CourseType currentParentNode = null;
                for (CourseType tempNode : allCourseTypes) {
                                  if(everyCourseType.getPid().longValue()==tempNode.getId().longValue()){
                        currentParentNode = tempNode;
                        break;
                    }
                }
                //4.把子节点添加到父节点中
                if (currentParentNode!=null){
                    currentParentNode.getChilderen().add(everyCourseType);
                }
            }
        }
        return topNode;
    }

2. 解决跨源请求

在zuul网关中配置

@Configuration
public class GlobalCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 允许的域,不要写*,否则cookie就无法使用了
        config.addAllowedOrigin("http://127.0.0.1:6001");
config.addAllowedOrigin("http://localhost:6001");
        //2) 是否发送Cookie信息
        config.setAllowCredentials(true);
        //3) 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)允许的头信息
        config.addAllowedHeader("*");
        //2.添加映射路径,我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new
                UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}

2. 课程类型集成Redis

1.为什么要做缓存?

因为查询的频率高,且数据不易改变,可以用redis做缓存,提高读的效率!

2.为什么要用redis做缓存

redis作为一个中央缓存,可以作为一个独立的服务,相较于mybatis的缓存,它不用集成到项目中,不会在项目中抢占资源,最重要的是,它不存在数据不同步的问题!如果用mybatis的缓存,他需要在每个项目中集成,当一个请求访问某一个服务,对数据进行操作,可能会造成缓存不同步的问题,且会浪费大量的网络开销,性能不好!

3.搭建思路

①:创建redis服务作为一个独立的服务

hrm-redis-parent

​ ---hrm-redis-feign :放feign的客户端接口,所有需要集成redis的服务,只需要依赖此模块,建立于feign的通讯

​ ---hrm-redis-server-2030:redis的服务模块,直接操作redis缓存中心!

②:注册到eureka注册中心

③:注册到配置中心,配置文件从云端拉取

④:集成jedis用来在java代码中操作redis

         <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

⑤:hrm-redis-server-2030中创建redis工具类

public enum RedisUtils {
    INSTANCE;
    static JedisPool jedisPool = null;

    static {
        //1 创建连接池配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //2 进行配置-四个配置
        config.setMaxIdle(1);//最小连接数
        config.setMaxTotal(11);//最大连接数
        config.setMaxWaitMillis(10 * 1000L);//最长等待时间
        config.setTestOnBorrow(true);//测试连接时是否畅通
        //3 通过配置对象创建连接池对象
        Properties properties = null;
        try {
            properties = new Properties();
            properties.load(RedisUtils.class.getClassLoader().getResourceAsStream("redis.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String host = properties.getProperty("redis.host");
        String port = properties.getProperty("redis.port");
        String password = properties.getProperty("redis.password");
        String timeout = properties.getProperty("redis.timeout");

        jedisPool = new JedisPool(config, host, Integer.valueOf(port),Integer.valueOf(timeout), password);
    }

    //获取连接
    public Jedis getSource() {
        return jedisPool.getResource();
    }

    //关闭资源
    public void closeSource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }

    }

    /**
     * 设置字符值
     *
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        Jedis jedis = getSource();
        jedis.set(key, value);
        closeSource(jedis);
    }

    /**
     * 设置
     * @param key
     * @param value
     */
    public void set(byte[] key, byte[] value) {
        Jedis jedis = getSource();
        jedis.set(key, value);
        closeSource(jedis);
    }

    /**
     *
     * @param key
     * @return
     */
    public byte[]  get(byte[] key) {
        Jedis jedis = getSource();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeSource(jedis);
        }
        return null;

    }

    /**
     * 设置字符值
     *
     * @param key
     */
    public String get(String key) {
        Jedis jedis = getSource();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeSource(jedis);
        }

        return null;

    }
}

⑥:创建controller,连接redis

@RestController
@RequestMapping("/redis")
public class RedisController {
    @PostMapping("/set")
    public AjaxResult set(@RequestParam("key") String key, @RequestParam("value")String value){
        RedisUtils.INSTANCE.set(key, value);
        return AjaxResult.me();
    }
    @GetMapping("/get/{key}")
    public AjaxResult get(@PathVariable("key") String key){
        String result = RedisUtils.INSTANCE.get(key);
        return AjaxResult.me().setResultObj(result);
    }
}

⑦:启动服务,用postman进行测试

⑧:hrm-redis-feign模块中集成feign,所有服务需要集成redis,通过feign与redis建立通讯

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

⑨:创建feign的客户端接口 @FeignClient目标服务的服务名,和目标服务的服务方法

@FeignClient(value = "redis-server")
public interface RedisFeignClient {
    @PostMapping("/redis/set")
    AjaxResult set(@RequestParam("key") String key, @RequestParam("value")String value);
    @GetMapping("/redis/get/{key}")
    AjaxResult get(@PathVariable("key") String key);
}

⑩:在需要集成redis的服务中开启feign建立与feign的通讯 @EnableFeignClients 扫面feign接口的路径!

@SpringBootApplication
@MapperScan("com.hanfengyi.course.mapper")
@EnableTransactionManagement
@EnableFeignClients("com.hanfengyi.feign")
public class CourseService2020 {
    public static void main(String[] args) {
        SpringApplication.run(CourseService2020.class);
    }
}

3.课程类型对redis的操作

思路:在访问课程中心时,发送请求,请求通过feign与redis建立通讯,对redis进行操作,在执行查询的时候,先查询redis,如果redis有数据,就直接返回,没有从数据库中查询,查询后,同步到redis缓存中,再返回结果!

①:导入fastjson包,因为redis是以string类型的<key,value>存储,如果要存储对象,必须转换为json格式字符串

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

②:修改获得课程类型的方法,同时对添加、修改、删除的方法增强,每次操作完,都要重新查询数据库,同步到redis

@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {
    @Autowired
    private RedisFeignClient redisFeignClient;

    private List<CourseType> putDataIntoRedis(){
       List<CourseType> allTypes = baseMapper.selectList(null);
       redisFeignClient.set(RedisKeyConstants.COURSE_TYPE, JSONObject.toJSONString(allTypes));
       return allTypes;
    }
    @Override
    public List<CourseType> treeData() {
        List<CourseType> allTypes = null;
        //1. 从redis中获取数据
        AjaxResult course_type = redisFeignClient.get(RedisKeyConstants.COURSE_TYPE);
        //2. 有数据转换为List
        if(course_type.isSuccess() && course_type.getResultObj()!=null){
            String courseTypeForJson = course_type.getResultObj().toString();
            allTypes = JSON.parseArray(courseTypeForJson,CourseType.class);
        }else{
            allTypes = putDataIntoRedis();
        }
        //1. 查询出所有的类型
       // List<CourseType> allTypes = baseMapper.selectList(null);
        //2. 创建list存放顶层节点
        List<CourseType> topNode = new ArrayList<>();
        //3.查询顶层节点
        for (CourseType everyType : allTypes) {
            if(everyType.getPid().longValue()==0){
                topNode.add(everyType);
            }else {
                //4. 当前节点是父节点
                CourseType currentParentNode = null;
                for (CourseType tempType : allTypes) {
                    if(everyType.getPid().longValue()==tempType.getId().longValue()){
                        currentParentNode = tempType;
                        break;
                    }
                }
                if(currentParentNode!=null){
                    currentParentNode.getChildren().add(everyType);
                }
            }
        }
        return topNode;
    }

    @Override
    public boolean insert(CourseType entity) {
        boolean insertResult = super.insert(entity);
        putDataIntoRedis();
        return insertResult;
    }

    @Override
    public boolean deleteById(Serializable id) {
        boolean deleteResult = super.deleteById(id);
        putDataIntoRedis();
        return deleteResult;
    }

    @Override
    public boolean updateById(CourseType entity) {
        boolean updateResult = super.updateById(entity);
        putDataIntoRedis();
        return updateResult;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容