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;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容