jdk17+springboot3+redis-stack整合

springboot3+redis-stack

1.基本准备

-1. 安装并启动redis-stack,防止与本地使用的redis端口冲突,使用6380端口
	docker run -d --name redis-stack-server -p 6380:6379 redis/redis-stack-server:latest
-2.启动后如图:
	![启动成功](https://img-blog.csdnimg.cn/5bca33c92f774ea18b8b27b076d02504.png#pic_center)

更详细介绍参考 [redis官方文档](https://redis.io/docs/getting-started/install-stack/docker/) `

2.使用maven搭建springboot项目,完整依赖如下:

<dependencies>
    <dependency>
        <groupId>com.redis.om</groupId>
        <artifactId>redis-om-spring</artifactId>
        <version>0.8.7-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
         <version>3.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
         <version>3.1.3</version>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
        <version>1.18.24</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
         <version>3.1.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>snapshots-repo</id>
        <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
</repositories>

3.项目结构

  • 项目结构图
    项目输出
  • 配置文件application.properties

spring.data.redis.host=127.0.0.1
spring.data.redis.port=6380
spring.data.redis.database=0
server.port=6333

4.代码块

  • controller
package org.micro.redistack.controller;

import org.micro.redistack.domain.Person;
import org.micro.redistack.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harry
 * @date 2023/9/15
 */
@RestController
@RequestMapping("/api/person/v1")
public class DataQueryController {

    @Autowired
    PersonRepository personRepository;

    @GetMapping("/all")
    public List<Person> findAll() {
        return personRepository.findAll();
    }

    @GetMapping("/{id}")
    public Person findById(@PathVariable("id") String id) {
        return personRepository.findById(id).get();
    }

    @GetMapping("/query")
    public List<Person> query(@RequestParam("minAge") Integer minAge, @RequestParam("maxAge") Integer maxAge ) {
        return personRepository.findByAgeBetween(minAge,maxAge);
    }
}
  • 实体
package org.micro.redistack.domain;

import java.util.Set;

import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.geo.Point;

import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;


@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Data
@Builder
@NoArgsConstructor
@Document
public class Person {
    // Id Field, also indexed
    @Id
    @Indexed
    private String id;

    // Indexed for exact text matching
    @Indexed @NonNull
    private String firstName;

    @Indexed @NonNull
    private String lastName;

    //Indexed for numeric matches
    @Indexed @NonNull
    private Integer age;

    @Indexed @NonNull
    private Double salary;

    //Indexed for Full Text matches
    @Searchable @NonNull
    private String personalStatement;

    //Indexed for Geo Filtering
    @Indexed @NonNull
    private Point homeLoc;

    // Nest indexed object
    @Indexed @NonNull
    private Address address;

    @Indexed @NonNull
    private Set<String> skills;
}
package org.micro.redistack.domain;

import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;

import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor(staticName = "of")
public class Address {

    @NonNull
    @Indexed
    private String houseNumber;

    @NonNull
    @Searchable(nostem = true)
    private String street;

    @NonNull
    @Indexed
    private String city;

    @NonNull
    @Indexed
    private String state;

    @NonNull
    @Indexed
    private String postalCode;

    @NonNull
    @Indexed
    private String country;
}
  • 数据访问层
package org.micro.redistack.repository;

import com.redis.om.spring.repository.RedisDocumentRepository;
import org.micro.redistack.domain.Person;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author harry
 * @date 2023/9/14
 */
@Repository
public interface PersonRepository extends RedisDocumentRepository<Person,String> {

    List<Person> findByAgeBetween(int minAge, int maxAge);
}

  • 项目启动类
package org.micro.redistack;

import com.redis.om.spring.annotations.EnableRedisDocumentRepositories;
import org.micro.redistack.domain.Address;
import org.micro.redistack.domain.Person;
import org.micro.redistack.repository.PersonRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.geo.Point;

import java.util.List;
import java.util.Set;

/**
 * @author harry
 * @date 2023/9/14
 */
@EnableRedisDocumentRepositories(basePackages = {"com.redis.om.*","org.micro.redistack"})
@SpringBootApplication
public class RediStackStarter {
    public static void main(String[] args) {
        SpringApplication.run(RediStackStarter.class, args);
    }

//    @Bean
//    CommandLineRunner loadTestData(PersonRepository repo) {
//        return args -> {
//            repo.deleteAll();
//            String thorSays = "The Rabbit Is Correct, And Clearly The Smartest One Among You.";
//            // Serendipity, 248 Seven Mile Beach Rd, Broken Head NSW 2481, Australia
//            Address thorsAddress = Address.of("248", "Seven Mile Beach Rd", "Broken Head", "NSW", "2481", "Australia");
//            Person thor = Person.of("Chris", "Hemsworth", 38, thorSays, new Point(153.616667, -28.716667), thorsAddress, Set.of("hammer", "biceps", "hair", "heart"));
//            repo.save(thor);
//        };
//    }

    @Bean
    CommandLineRunner loadTestData(PersonRepository repo) {
        return args -> {
            repo.deleteAll();

            String thorSays = "The Rabbit Is Correct, And Clearly The Smartest One Among You.";
            String ironmanSays = "Doth mother know you weareth her drapes?";
            String blackWidowSays = "Hey, fellas. Either one of you know where the Smithsonian is? I’m here to pick up a fossil.";
            String wandaMaximoffSays = "You Guys Know I Can Move Things With My Mind, Right?";
            String gamoraSays = "I Am Going To Die Surrounded By The Biggest Idiots In The Galaxy.";
            String nickFurySays = "Sir, I’m Gonna Have To Ask You To Exit The Donut";

            // Serendipity, 248 Seven Mile Beach Rd, Broken Head NSW 2481, Australia
            Address thorsAddress = Address.of("248", "Seven Mile Beach Rd", "Broken Head", "NSW", "2481", "Australia");

            // 11 Commerce Dr, Riverhead, NY 11901
            Address ironmansAddress = Address.of("11", "Commerce Dr", "Riverhead", "NY",  "11901", "US");

            // 605 W 48th St, New York, NY 10019
            Address blackWidowAddress = Address.of("605", "48th St", "New York", "NY", "10019", "US");

            // 20 W 34th St, New York, NY 10001
            Address wandaMaximoffsAddress = Address.of("20", "W 34th St", "New York", "NY", "10001", "US");

            // 107 S Beverly Glen Blvd, Los Angeles, CA 90024
            Address gamorasAddress = Address.of("107", "S Beverly Glen Blvd", "Los Angeles", "CA", "90024", "US");

            // 11461 Sunset Blvd, Los Angeles, CA 90049
            Address nickFuryAddress = Address.of("11461", "Sunset Blvd", "Los Angeles", "CA", "90049", "US");

            Person thor = Person.of("Chris", "Hemsworth", 38, 12345.32d,thorSays, new Point(153.616667, -28.716667), thorsAddress, Set.of("hammer", "biceps", "hair", "heart"));
            Person ironman = Person.of("Robert", "Downey", 56, 1500d,ironmanSays, new Point(40.9190747, -72.5371874), ironmansAddress, Set.of("tech", "money", "one-liners", "intelligence", "resources"));
            Person blackWidow = Person.of("Scarlett", "Johansson", 37, 8500d,blackWidowSays, new Point(40.7215259, -74.0129994), blackWidowAddress, Set.of("deception", "martial_arts"));
            Person wandaMaximoff = Person.of("Elizabeth", "Olsen", 32,685d, wandaMaximoffSays, new Point(40.6976701, -74.2598641), wandaMaximoffsAddress, Set.of("magic", "loyalty"));
            Person gamora = Person.of("Zoe", "Saldana", 43, 566d,gamoraSays, new Point(-118.399968, 34.073087), gamorasAddress, Set.of("skills", "martial_arts"));
            Person nickFury = Person.of("Samuel L.", "Jackson", 73, 255125d,nickFurySays, new Point(-118.4345534, 34.082615), nickFuryAddress, Set.of("planning", "deception", "resources"));

            repo.saveAll(List.of(thor, ironman, blackWidow, wandaMaximoff, gamora, nickFury));
        };
    }
}

文中示例代码皆来自官方文档,按上面配置后就实现了springboot3与redis-stack的结合。更多redis-stack的使用持续关注后续文章。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值