栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot + Redis集群 整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot + Redis集群 整合

resources 下的application.yml

#集群版
spring:
  redis:
    cluster:
      nodes:
        #list
        - 192.168.132.139:8001
        - 192.168.132.139:8002
        - 192.168.132.139:8003
        - 192.168.132.139:8004
        - 192.168.132.139:8005
        - 192.168.132.139:8006
    lettuce:
      pool:
        max-active: 30

Vo 

heroVo:

package com.etoak.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class HeroVo {
    //英雄名称
    private String hero;
    //武力值
    private Double power;
}

server:

package com.etoak.service;


import com.etoak.vo.HeroVo;

import java.util.List;

public interface RankService {
    
    void  addHero(String key, String hero, double power);

    
    List getByIndex(String key,int start,int end);
    
    List getByPower(String key, double min,double max);

}

impl:

package com.etoak.service.impl;


import com.etoak.service.RankService;
import com.etoak.vo.HeroVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RankServiceImpl  implements RankService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void addHero(String key, String hero, double power) {
        stringRedisTemplate.opsForZSet().add(key, hero, power);

    }

    @Override
    public List getByIndex(String key, int start, int end) {
        //zset命令:
        Set> typedTuples = stringRedisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
        List resultList = new ArrayList<>();
        typedTuples.forEach(x ->{
            resultList.add(new HeroVo(x.getValue(),x.getScore()));
        });
        return resultList;
    }

    @Override
    public List getByPower(String key, double min, double max) {
        //zset:命令
        return stringRedisTemplate.opsForZSet()
                .reverseRangeByScoreWithScores(key, min, max)
                .stream()
                .map(x->{
                    return  new HeroVo(x.getValue(),x.getScore());
                }).collect(Collectors.toList());
    }
}

controller:

package com.etoak.controller;

import com.etoak.service.RankService;
import com.etoak.vo.HeroVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/rank")
public class RankController {

    @Autowired
    RankService rankService;

    public static final String KEY="rank:hero";

    private Random random = new Random();

    
    @RequestMapping("/{hero}")
    public String addHero(@PathVariable String hero) {
        int power = random.nextInt(1001);
        rankService.addHero(KEY,hero,power);
        return "success";
    }
    //
    @RequestMapping("/index/{start}/{end}")
    public List getByIndex(@PathVariable int start,@PathVariable int end) {
        return rankService.getByIndex(KEY,start,end);
    }

    //根据武力值得到
    @RequestMapping("/power/{min}/{max}")
    public List getByPower(@PathVariable double min,@PathVariable double max) {
        return  rankService.getByPower(KEY,min,max);
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390237.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号