好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot集成Redis的实现示例

前面一篇文章已经写了如何搭建一个单机版redis服务, 那么我们应该怎么在现有的系统中集成进来呢? 由于笔者使用的编程语言是java, 所以本篇文章主要描述springboot如何集成单redis节点完成数据的增删改查.

springboot环境

快速搭建一个springboot工程

进入 https://start.spring.io 网站, 使用该网站初始化一个springboot工程

添加相关依赖

因为使用spring initializer已经帮我们把redis的依赖建立好了; 但是由于我们要使用jedis客户端访问redis, 所以还需要添加jedis的依赖;

?

1

2

3

4

5

<dependency>

      <groupid>redis.clients</groupid>

      <artifactid>jedis</artifactid>

      <version> 2.9 . 0 </version> //版本号可以放在properties中作为属性, 这边用${jedis.version}来依赖

   </dependency>

配置redis节点信息

打开application.properties文件, 初始化的文件是空的; 我们将spring redis最基本的信息加入进去

?

1

2

spring.redis.host=localhost

spring.redis.port= 6379

将redis信息读入到程序中

新建一个java类命名为 standaloneredisconfig.java , 放在 com.xxx.example.config 包下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

package com.terrylmay.redis.example.config;

 

import org.springframework.boot.autoconfigure.condition.conditionalonproperty;

import org.springframework.boot.context.properties.configurationproperties;

import org.springframework.context.annotation.configuration;

 

@configuration

@configurationproperties (prefix = "spring.redis" )

@conditionalonproperty (name = { "spring.redis.host" })

public class standaloneredisconfig {

 

   string host;

 

   int port;

 

   public string gethost() {

     return host;

   }

 

   public void sethost(string host) {

     this .host = host;

   }

 

   public int getport() {

     return port;

   }

 

   public void setport( int port) {

     this .port = port;

   }

}

上面配置中的 @conditionalonproperty(name = {"spring.redis.host"}) 如果只是单机的redis则不需要添加该属性; 但是为了后面一套代码兼容多个redis部署模式, 使用该属性作为是否创建bean的条件; 如果是集群模式那么就不会使用 spring.redis.host 来作为连接字符串了;

配置jedis的连接池

将redis连接对象放入到spring容器中进行管理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

package com.terrylmay.redis.example;

 

import com.terrylmay.redis.example.config.standaloneredisconfig;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.boot.springapplication;

import org.springframework.boot.autoconfigure.springbootapplication;

import org.springframework.boot.autoconfigure.condition.conditionalonbean;

import org.springframework.context.annotation.bean;

import org.springframework.data.redis.connection.redisconnectionfactory;

import org.springframework.data.redis.connection.redisstandaloneconfiguration;

import org.springframework.data.redis.connection.jedis.jedisconnectionfactory;

import org.springframework.data.redis.core.stringredistemplate;

 

@springbootapplication (scanbasepackages = { "com.terrylmay.redis.example" })

public class redisexampleapplication {

 

   public static void main(string[] args) {

     springapplication.run(redisexampleapplication. class , args);

   }

 

   @autowired

   standaloneredisconfig standaloneredisconfig;

 

   @autowired

   redisconnectionfactory redisconnectionfactory;

 

   @bean

   @conditionalonbean (value = {standaloneredisconfig. class })

   public redisconnectionfactory standaloneredisconnectionfactory() {

     jedisconnectionfactory factory = new jedisconnectionfactory( new redisstandaloneconfiguration(standaloneredisconfig.gethost(), standaloneredisconfig.getport()));

     return factory;

   }

 

   @bean

   public stringredistemplate stringredistemplate() {

     return new stringredistemplate(redisconnectionfactory);

   }

}

这里的 @conditionalonbean(value = {standaloneredisconfig.class}) 与上面的 conditionalonproperty 是一个道理

这里的 scanbasepackages = {"com.terrylmay.redis.example"} 是为了以后将redis的客户端独立出一个工程而做的, 当然独立出来的工程base包名还要是这个才可以;

因为还没有看redis支持的数据结构, 那么现在只是把redis字符串模板类放到spring 容器中, 后续再增加其他数据类型的支持;

创建操作redis的接口 以及实现

创建 icacheprovider.java 接口:

?

1

2

3

4

5

6

7

package com.terrylmay.redis.example.provider;

 

public interface icacheprovider {

   void setstring(string key, string value);

 

   string getstring(string key);

}

jedis版本的实现:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.terrylmay.redis.example.provider.impl;

 

import com.terrylmay.redis.example.provider.icacheprovider;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.data.redis.core.stringredistemplate;

import org.springframework.stereotype.component;

 

@component

public class jediscacheprovider implements icacheprovider {

 

   @autowired

   stringredistemplate stringredistemplate;

 

   @override

   public void setstring(string key, string value) {

     stringredistemplate.opsforvalue().set(key, value);

   }

 

   @override

   public string getstring(string key) {

     return stringredistemplate.opsforvalue().get(key);

   }

}

这样基本上一个可以操作redis的java程序就已经就绪了; 那么我们需要验证一下, 当然如果在主工程中写一个类去验证也是没有问题的, 比如创建一个bean, 并且放到被 postcontruct 注解的方法里面;

但是更加专业的做法是写一个测试程序来测试, 下面看一下该测试程序应该怎么写

ut测试程序可用性

因为创建工程的时候, 就已经有一个测试类在test目录下面了, 我们增加我们想要的功能

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.terrylmay.redis.example;

 

import com.terrylmay.redis.example.provider.icacheprovider;

import org.junit. assert ;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.boot.test.context.springboottest;

import org.springframework.test.context.junit4.springjunit4classrunner;

 

@runwith (springjunit4classrunner. class )

@springboottest (classes = {redisexampleapplication. class })

public class redisexampleapplicationtests {

 

   @autowired

   icacheprovider jediscacheprovider;

 

   @test

   public void contextloads() {

     jediscacheprovider.setstring( "name" , "terrylmay" );

     system.out.println(jediscacheprovider.getstring( "name" ));

     assert .assertequals( "terrylmay" , jediscacheprovider.getstring( "name" ));

   }

}

注: 程序中不要有打印, 使用logger或者直接断言来处理 (本来想用markdown语法来标红的, 但是发现简书竟然不支持html的写法; 没办法只能用``来搞定了)

开发过程中遇到的问题

一、在写好所有的程序之后, 跑测试用例, 但是始终都是报nosuchbeanexception

caused by: org.springframework.beans.factory.nosuchbeandefinitionexception:no qualifying bean of type 'com.terrylmay.redis.example.config.standaloneredisconfig' available: expected at least 1 bean which qualifies as autowire candidate. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}

原因共有三点:

1、写了 scanbasepackages 来扫描包下面的bean, 扫描的包与类所在的包不一样, 只有一个字符之差 com.terrylmay.redis.example 与 com.terrlmay.redis.example , 当然这时候 idea 会报错, 只是我不认识那个错而已; idea报错如图所示:

2、按照网上的application.properties属性的读取方式, 只使用了一个注解:
@configurationproperties(prefix = "spring.redis") 但是点进该注解里面看, 它其实并没有component注解的功能; 所以增加了 @configuration 注解

3、第三个原因不仔细断然不会发现这个错误

我理解的是只要工程里面父工程是 spring-boot-starter-parent , 那么就不应该存在这类jar包没有依赖的问题, 打开文档

依赖可粘贴版:

?

1

2

3

4

5

<dependency>

   <groupid>org.springframework.boot</groupid>

   <artifactid>spring-boot-configuration-processor</artifactid>

   <optional> true </optional>

</dependency>

到这里, 一个能够使用redis的java工程就已经就绪了; 最终的代码全部在github的 spring-redis-example 仓库下, 欢迎star与pr

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault.com/a/1190000017151786

查看更多关于SpringBoot集成Redis的实现示例的详细内容...

  阅读:11次