spring 集成redisearch

添加依赖

  • 创建一个spring boot 应用
  • 添加依赖
        <dependency>
            <groupId>com.redislabs</groupId>
            <artifactId>spring-redisearch</artifactId>
            <version>3.0.1</version>
        </dependency>
  • spring-redisearch 内部采用ettuce作为客户端来驱动redis,同样的还有jedis 和redisson

Redisearch 操作

获取实例

  • application 配置redis地址
spring:
  redis:
    host: 192.168.20.3

  • 使用StatefulRediSearchConnection操作redis,在spring中直接注入即可

创建索引

  • 通StatefulRediSearchConnection的sync方法获取一个redisearchCommands实例然后创建索引
  • CreateOptions 用于构建创建索引时设置的选项
  • Field 用于封装建立索引的字段
  • redisearchCommands.create 用于创建索引
RediSearchCommands<String, String> sync = statefulRediSearchConnection.sync();

CreateOptions<String,String> book = CreateOptions.<String,String>builder().prefix("Book").build();
Field<String> title = Field.text("name").build();
Field<String> author = Field.text("author").build();
sync.create("books-index",book,title,author);

查询数据

  • 同样使用redisearchCommands 提供的search方法查询
SearchResults<String, String> search = redisSearchCommands.search("books-index", fuzzy);

注意事项

  • 熟悉StatefulRediSearchConnection、CreateOption、redisearchCommands相关的api
  • 参考redis官网的相关redis命令,相应的组件中都有相应的封装

参考

https://www.educba.com/spring-boot-commandlinerunner/
https://redis.io/commands/ft.create/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个基于Spring Boot的开发工具,它为基于JVM的云应用程序开发提供了一组丰富的框架和库。Spring Cloud提供了许多有用的功能,例如服务发现、配置管理、负载均衡、断路器、智能路由等,这些功能可以帮助我们更轻松地构建分布式系统。下面是Spring集成Spring Cloud的步骤: 1.在pom.xml文件中添加Spring Cloud的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.2.5.RELEASE</version> </dependency> ``` 2.在启动类上添加@EnableEurekaServer注解,开启Eureka Server: ```java @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } ``` 3.在application.yml文件中配置Eureka Server: ```yaml server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false ``` 4.在需要注册到Eureka Server的服务中添加Eureka Client的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.5.RELEASE</version> </dependency> ``` 5.在启动类上添加@EnableDiscoveryClient注解,开启Eureka Client: ```java @SpringBootApplication @EnableDiscoveryClient public class ServiceApplication { public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } } ``` 6.在application.yml文件中配置Eureka Client: ```yaml server: port: 8080 spring: application: name: service eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 以上是Spring集成Spring Cloud的基本步骤,其中Eureka Server和Eureka Client是Spring Cloud中最常用的组件之一。除此之外,Spring Cloud还提供了许多其他有用的组件,例如Zuul、Ribbon、Hystrix等,可以根据具体需求进行选择和集成

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值