Spring Boot集成Redis java实现配置
Spring Boot集成Redis实现自动配置
转自:http://blog.csdn.net/a67474506/article/details/52595053
通过上面一些笔记的介绍, 我们可以自己动手去集成Redis, 没有什么难度的
还有Spring Boot官方提供了Spring-Data-Redis的Starter POM
有兴趣的可以查看下这篇资料
http://blog.didispace.com/springbootredis/?utm_source=tuicool&utm_medium=referral
手动集成Redis
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.ibigsea</groupId>
- <artifactId>springboot-redis-demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <boot.version>1.3.5.RELEASE</boot.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <version>${boot.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.8.2</version>
- </dependency>
- </dependencies>
- </project>
整个项目的结构图是这样的
RedisConfiguration.java
- package com.ibigsea.springboot_redis_demo.config;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- @Configuration
- public class RedisConfiguration {
- @Bean(name= "jedis.pool")
- @Autowired
- public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
- @Value("${jedis.pool.host}")String host,
- @Value("${jedis.pool.port}")int port) {
- return new JedisPool(config, host, port);
- }
- @Bean(name= "jedis.pool.config")
- public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
- @Value("${jedis.pool.config.maxIdle}")int maxIdle,
- @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(maxTotal);
- config.setMaxIdle(maxIdle);
- config.setMaxWaitMillis(maxWaitMillis);
- return config;
- }
- }
RedisClient.java
- package com.ibigsea.springboot_redis_demo.config;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- * 就一个小Demo 随便写下
- * @author bigsea
- *
- */
- @Component
- public class RedisClient {
- @Autowired
- private JedisPool jedisPool;
- public void set(String key, String value) throws Exception {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- jedis.set(key, value);
- } finally {
- //返还到连接池
- jedis.close();
- }
- }
- public String get(String key) throws Exception {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- return jedis.get(key);
- } finally {
- //返还到连接池
- jedis.close();
- }
- }
- }
App.java
- package com.ibigsea.springboot_redis_demo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.ibigsea.springboot_redis_demo.config.RedisClient;
- /**
- * 是Spring Boot项目的核心注解,主要是开启自动配置
- */
- @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
- @RestController
- public class App {
- @Autowired
- private RedisClient redisClinet;
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- @RequestMapping("/set")
- public String set(String key, String value) throws Exception{
- redisClinet.set(key, value);
- return "success";
- }
- @RequestMapping("/get")
- public String get(String key) throws Exception {
- return redisClinet.get(key);
- }
- }
application.yml
- jedis :
- pool :
- host : 127.0.0.1
- port : 6379
- config :
- maxTotal: 100
- maxIdle: 10
- maxWaitMillis : 100000
- server :
- port : 8082
运行结果:
实现自己的jedis-spring-boot-starter
一些自动配置的东西这里面有讲
http://blog.csdn.net/a67474506/article/details/52013634
首先我们创建一个jedis-spring-boot-starter
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.ibigsea</groupId>
- <artifactId>jedis-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <boot.version>1.3.5.RELEASE</boot.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.8.2</version>
- </dependency>
- </dependencies>
- </project>
目录结构是这样的
JedisProperties.java
- package com.ibigsea.jedis.autoconfigure;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- @ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)
- public class JedisProperties {
- public static final String JEDIS_PREFIX = "jedis";
- private String host;
- private int port;
- private int maxTotal;
- private int maxIdle;
- private int maxWaitMillis;
- 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;
- }
- public int getMaxTotal() {
- return maxTotal;
- }
- public void setMaxTotal(int maxTotal) {
- this.maxTotal = maxTotal;
- }
- public int getMaxIdle() {
- return maxIdle;
- }
- public void setMaxIdle(int maxIdle) {
- this.maxIdle = maxIdle;
- }
- public int getMaxWaitMillis() {
- return maxWaitMillis;
- }
- public void setMaxWaitMillis(int maxWaitMillis) {
- this.maxWaitMillis = maxWaitMillis;
- }
- }
JedisAutoConfiguration.java
- package com.ibigsea.jedis.autoconfigure;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import com.ibigsea.jedis.client.RedisClient;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- @Configuration
- @EnableConfigurationProperties(JedisProperties.class)//开启属性注入,通过@autowired注入
- @ConditionalOnClass(RedisClient.class)//判断这个类是否在classpath中存在
- public class JedisAutoConfiguration {
- @Autowired
- private JedisProperties prop;
- @Bean(name="jedisPool")
- public JedisPool jedisPool() {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(prop.getMaxTotal());
- config.setMaxIdle(prop.getMaxIdle());
- config.setMaxWaitMillis(prop.getMaxWaitMillis());
- return new JedisPool(config, prop.getHost(), prop.getPort());
- }
- @Bean
- @ConditionalOnMissingBean(RedisClient.class)//容器中如果没有RedisClient这个类,那么自动配置这个RedisClient
- public RedisClient redisClient(@Qualifier("jedisPool")JedisPool pool) {
- RedisClient redisClient = new RedisClient();
- redisClient.setJedisPool(pool);
- return redisClient;
- }
- }
RedisClient.java
- package com.ibigsea.jedis.client;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- * 就一个小Demo 随便写下
- * @author bigsea
- *
- */
- public class RedisClient {
- private JedisPool jedisPool;
- public void set(String key, String value) throws Exception {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- jedis.set(key, value);
- } finally {
- //返还到连接池
- jedis.close();
- }
- }
- public String get(String key) throws Exception {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- return jedis.get(key);
- } finally {
- //返还到连接池
- jedis.close();
- }
- }
- public JedisPool getJedisPool() {
- return jedisPool;
- }
- public void setJedisPool(JedisPool jedisPool) {
- this.jedisPool = jedisPool;
- }
- }
还要在spring.factories里面配置
- # Auto Configure
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.ibigsea.jedis.autoconfigure.JedisAutoConfiguration
新建一个工程测试下
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.ibigsea</groupId>
- <artifactId>test-starter</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>test-starter</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <boot.version>1.3.5.RELEASE</boot.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.ibigsea</groupId>
- <artifactId>jedis-spring-boot-starter</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>${boot.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <version>${boot.version}</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
application.yml
- jedis :
- host : 127.0.0.1
- port : 6379
- maxTotal: 100
- maxIdle: 10
- maxWaitMillis : 100000
- server :
- port : 8082
App.java
- package com.ibigsea.test_starter;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.ibigsea.jedis.client.RedisClient;
- @SpringBootApplication
- @RestController
- public class App {
- @Autowired
- private RedisClient redisClient;
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- @RequestMapping("/set")
- public String set(String key, String value) throws Exception{
- redisClient.set(key, value);
- return "success";
- }
- @RequestMapping("/get")
- public String get(String key) throws Exception {
- return redisClient.get(key);
- }
- }
测试结果
这里只是简单的写了下一个小demo,有兴趣的小伙伴可以自己去扩展更多的
比如实现集群连接, 分片连接,API扩展的更多一些
相关推荐
- spring boot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring
- Spring boot2.0以上集成Druid连接池及监控信息配置
- Spring boot 和ActiveMQ集成报错:java.io.NotSerializableException
- Spring Boot集成Redis java实现配置
- Spring boot集成Swagger2实现自动生成API文档
- Spring Boot + Spring Cloud 实现权限管理系统:集成 Shiro 框架
- spring boot集成mybatis--注解配置
- spring boot maven Quartz 实现 job 任务加 springboot 集成 quartz集群 总结quartz Cron 语法介绍 以及采坑记录
- 在spring boot中使用redis(一):集成redis
- SpringBoot(十一): Spring Boot集成Redis
- Spring集成redis
- spring 集成redis