spring boot 多数据源集成redis为什么报数据源装载失败

1 添加redis支持
在pom.xml中添加
&dependency&
&groupId&org.springframework.boot&/groupId&
&artifactId&spring-boot-starter-redis&/artifactId&
&/dependency&
2 redis配置
@Configuration
@EnableCaching
public class RedisCacheConfig {
public CacheManager cacheManager(
@SuppressWarnings(&rawtypes&) RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
public RedisTemplate&String, String& redisTemplate(
RedisConnectionFactory factory) {
final StringRedisTemplate template = new StringRedisTemplate(factory);
template.setValueSerializer(new Jackson2JsonRedisSerializer&SysUser&(
SysUser.class)); //请注意这里
3 redis服务器配置
# REDIS (RedisProperties)
spring.redis.database= # database name
spring.redis.host=localhost # server host
spring.redis.password= # server password
spring.redis.port=6379 # connection port
spring.redis.pool.max-idle=8 # pool settings ...
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.sentinel.master= # name of Redis server
spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
*此处的dao操作使用的是spring data jpa,使用@Cacheable可以在任意方法上,*比如@Service或者@Controller的方法上
public interface SysUserRepo1 extends CustomRepository&SysUser, Long& {
@Cacheable(value = &usercache&)
public SysUser findByUsername(String username);
@Controller
public class TestController {
@Autowired
SysUserRepo1 sysUserRepo1;
@RequestMapping(&/test&)
public @ResponseBody String test(){
final SysUser loaded = sysUserRepo1.findByUsername(&wyf&);
final SysUser cached = sysUserRepo1.findByUsername(&wyf&);
return &ok&;
效果如图:
相关 [spring boot redis] 推荐:
- ITeye博客
SysUser.class)); //请注意这里. 3 redis服务器配置. /**
*此处的dao操作使用的是spring data jpa,使用@Cacheable可以在任意方法上,*比如@Service或者@Controller的方法上
public interface SysUserRepo1 extends CustomRepository&SysUser, Long& {.
- 企业架构 - ITeye博客
spring多年以来一直都是java平台开发web应用的主流技术,在标准的J2EE架构之外提供了一个轻量级的解决方案. 虽然spring提供了很多功能,简化了java平台的企业应用开发,降低了开发工作量,但相比较其它语言的一些框架(例如ruby on rails,python Django)来说,基于spring 的开发仍然比较复杂,尤其是新建一个项目时,需要进行各种配置,重复的工作量较大.
- Juven Xu
微服务的流行,相比较以前一个大型应用程序搞定所有需求,我们现在更倾向于把大型应用程序切分成多个微服务,服务之间通过 RPC 调用. 微服务架构的好处非常多,例如稳定的服务变化较少,不会被非稳定服务所影响;不同的服务更方便交给不同的人管理;发布、扩容等操作也更加有针对性. 不过这也不是没有代价的,额外的成本最主要的可能就是运维成本.
- InfoQ cn
SpringSource
发布了用于将Redis轻松集成到Java应用中的开源
库的首个稳定版. Redis是个由VMWare/SpringSource资助的键值存储,为一些高性能网站如GitHub与StackOverflow等所用. Redis是新近涌现的NoSQL数据存储之一,它关注于简单性与性能(整个数据集放在内存中).
- CSDN博客推荐文章
使用redis做缓存的思路是在spring的项目中配置拦截器,在service层做切面,在findXXX或者getXXX等方法上进行拦截判断是否缓存即可. 1.环境:spring 3.1.2 + spring data redis 1.0.0+ jedis 2.1.0. 2.spring配置文件配置:.
- 开源软件 - ITeye博客
1.下载相关jar包,并引入工程:. 2.将以下XML配置引入spring. 3.将shardedJedisPool注入相关的类中即可使用. * 设置一个key的过期时间(单位:秒). * @param key key值. * @param seconds 多少秒后过期. * @return 1:设置了过期时间
0:没有设置过期时间/不能设置过期时间.
- 编程语言 - ITeye博客
我们希望能够将数据库查询结果缓存到Redis中,这样在第二次做同样的查询时便可以直接从redis取结果,从而减少数据库读写次数. 必须要做到与业务逻辑代码完全分离. 从缓存中读出的数据必须与数据库中的数据一致. 如何为一个数据库查询结果生成一个唯一的标识. Key),能唯一确定一个查询结果,同一个查询结果,一定能映射到同一个.
- Java - 编程语言 - ITeye博客
java中使用redis总是需要处理redis连接的获取,释放等操作,每次使用都会使代码变的特别丑陋,模仿spring中aop的实现,用动态代理写一个 连接自动获取和释放的工具. JedisManageSupport 抽象类 类似于 aop的切入点,所有继承了该类(一般都是service层)的类,可以使用提供的获取redis的方法获取redis,并且不需要释放.
- SotongDJ - LinuxTOY
Red Hat 协同 Linux 基金会以及 Canonical 发布了针对 UEFI 中安全启动对于 Linux 系统影响的评估白皮书. 前段时间某些朝内站点上关于 UEFI 将禁止其他非 Win 系统的谣言总算以 M$ 方面澄清的方式告一段落. 现在来自 Linux 阵营的参与者们作出了自己的回应,对于 Secure Boot 进行了评估.
- ITeye资讯频道
redis-monitor是一个Web可视化的 redis 监控程序. 使用 Flask 来开发的,代码结构非常简单,适合移植到公司内网使用. redis 服务器信息,包括 redis 版本、上线时间、 os 系统信息等等. 实时的消息处理信息,例如处理 command 数量、连接总数量等. 内存占用、 cpu 消耗实时动态图表.
坚持分享优质有趣的原创文章,并保留作者信息和版权声明,任何问题请联系:@。Redis整合Spring项目搭建实例
发表于 15:05|
作者Emmanouil Gkatziouras
摘要:本文你将学习到如何使用注解的方式,运用jedis驱动来将Redis缓存整合到你的Spring项目中。
本文介绍了如何使用注解的方式,将Redis缓存整合到你的Spring项目。
首先我们将使用jedis驱动,进而开始配置我们的Gradle。
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
baseName = 'gs-serving-web-content'
'0.1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
紧接着我们将使用Spring注解,继续执行Redis装载配置。package com.gkatzioura.spring.
import org.springframework.cache.CacheM
import org.springframework.cache.annotation.CachingConfigurerS
import org.springframework.cache.annotation.EnableC
import org.springframework.context.annotation.B
import org.springframework.context.annotation.C
import org.springframework.data.redis.cache.RedisCacheM
import org.springframework.data.redis.connection.RedisConnectionF
import org.springframework.data.redis.connection.jedis.JedisConnectionF
import org.springframework.data.redis.core.RedisT
import org.springframework.data.redis.serializer.RedisS
import org.springframework.data.redis.serializer.StringRedisS
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setUsePool(true);
return jedisConnectionF
public RedisSerializer redisStringSerializer() {
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
return stringRedisS
@Bean(name="redisTemplate")
public RedisTemplate&String, String& redisTemplate(RedisConnectionFactory cf,RedisSerializer redisSerializer) {
RedisTemplate&String, String& redisTemplate = new RedisTemplate&String, String&();
redisTemplate.setConnectionFactory(cf);
redisTemplate.setDefaultSerializer(redisSerializer);
return redisT
public CacheManager cacheManager() {
return new RedisCacheManager(redisTemplate(redisConnectionFactory(),redisStringSerializer()));
下一步将创建缓存接口CacheService。package com.gkatzioura.spring.
import java.util.D
import java.util.L
public interface CacheService {
public void addMessage(String user,String message);
public List&String& listMessages(String user);
当然用户既可以增加一条消息也能取回一条消息。因此,在实现过程中,用户相关信息的存在时间将默认设为一分钟。
我们用Redis来继承实现CacheService接口。package com.gkatzioura.spring.cache.
import com.gkatzioura.spring.cache.CacheS
import org.springframework.data.redis.core.ListO
import org.springframework.data.redis.core.RedisO
import org.springframework.data.redis.core.SetO
import org.springframework.stereotype.S
import javax.annotation.R
import java.time.ZonedDateT
import java.time.temporal.ChronoU
import java.util.D
import java.util.L
@Service("cacheService")
public class RedisService implements CacheService {
@Resource(name = "redisTemplate")
private ListOperations&String, String& messageL
@Resource(name = "redisTemplate")
private RedisOperations&String,String& latestMessageE
public void addMessage(String user,String message) {
messageList.leftPush(user,message);
ZonedDateTime zonedDateTime = ZonedDateTime.now();
Date date = Date.from(zonedDateTime.plus(1, ChronoUnit.MINUTES).toInstant());
latestMessageExpiration.expireAt(user,date);
public List&String& listMessages(String user) {
return messageList.range(user,0,-1);
我们的缓存机制将保留每个用户发送的消息列表。为了实现这个功能我们将调用ListOperations接口,同时将每个user作为一个key键值。通过RedisOperations接口,我们可以为key设置特定存在时长。在本例中,主要使用的是&user&key。
下一步我们将创建一个controller注入缓存服务。package com.gkatzioura.spring.
import com.gkatzioura.spring.cache.CacheS
import org.springframework.beans.factory.annotation.A
import org.springframework.web.bind.annotation.*;
import java.util.L
@RestController
public class MessageController {
@Autowired
private CacheService cacheS
@RequestMapping(value = "/message",method = RequestMethod.GET)
@ResponseBody
public List&String& greeting(String user) {
List&String& messages = cacheService.listMessages(user);
@RequestMapping(value = "/message",method = RequestMethod.POST)
@ResponseBody
public String saveGreeting(String user,String message) {
cacheService.addMessage(user,message);
return "OK";
最后完成类Application的创建。package com.gkatzioura.
import org.springframework.boot.SpringA
import org.springframework.boot.autoconfigure.SpringBootA
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
经过如上步骤,接下来直接运行Application即可。
原文链接:(
译者/丘志鹏 审校/朱正贵 责编/仲浩)
译者简介:邱志鹏,关注大数据、机器学习。
推荐阅读相关主题:
CSDN官方微信
扫描二维码,向CSDN吐槽
微信号:CSDNnews
相关热门文章redis的sentinel使用以及spring集成方面的问题_百度知道
redis的sentinel使用以及spring集成方面的问题
192./constructor-arg index=&
& value=&quot.password&bean&0&quot.
class=& value=&2&quot!--配置了两个哨兵 --&gt:36 下午 /100000&quot.86:&value& &redis:26372& /maxIdle&/&#47.&/constructor-arg index=& / &0&&gt:03.JedisSentinelPool& ref=&true&quot.xml如下配置;
&constructor-arg index=& /
&&timeout&&&set&gt,sentinel配置
Redis. value=&quot, 2015 12.JedisConnectionFactory&&gt:十一月 30; /
&value&gt.86;&bean id=&& & value=&property name=&
&property name=&quot,eclipse也有相应的反应;6371&testOnBorrow&quot.168;set&
&lt,linux端slave能自动选举出新的 &#47.86; &#47: Created JedisPool to master at 192;port&constructor-arg&现在能够正常运行程序.jedisPoolConfig& value=& value=&property name=& &#47,redis能够正常的存入信息.5; value=&bean id=&jedisPoolConfig&quot.property name=&
&property name=&bean&property name=&property name=& &
&&gt.JedisSentinelPool initPool信息;constructor-arg index=&
&/property name=&
&&#47:26370&& ref=&&gt.192; class=&property name=& value=& value=&
&lt。当我断掉master(端口号& value=&true&hostName& value=&quot.168;/jedisConnectionFactory&300&
&property&jedisPoolConfig&1&mymaster&&
&&&/192; class=&quot.value&gt.5;&usePool&bean id=&quot.JedisPoolConfig&&&&maxWaitMillis&property&property name=&quot.168;&&bean&gt.5;/testOnReturn& /0&
&property name=&quot.clients. /database&quot.5&quot.168;& value=&maxTotal&
&value&; &#47:
我已经在linux端做好了master以及两个slave的&gt:6372但是程序的redis无法再存取数据了;100&&gt.86;jedisPool&quot现在的情况是这样的
甚至也怀疑老司机的想法能不能实现;property name=&quot,程序能够在新选出的master中继续存取数据。(当我再启动6371后;将这个端口改成监听,程序仍然向这个端口请求; /& value=&quot,所以导致报错,问题是出在将数据放到redis这一步。求各位指点一下.setUser(sessionUserId;port&quot,无法再传递数据)但是带我的老司机想让我实现,&lt:redisHelper,监听变化后的master端口号。我设想了一个方法,虽然已经变成slave。我也没看到百度上有人这样做,也不知道redis能不能这样做, _user):发现已经从数据库中取到了数据,redis又能存取数据了,感激不尽:当一个master挂掉之后;我猜想是因为redis的6371这个端口断掉后,谢谢;6371&quot经过我单步运行程序
可以看出 key 和 value 都是泛型的,这就涉及到将类型进行序列化的问题了所就在 RedisTemplate 中还有几个 RedisSerializer~ 1)redisConnectionFactory()配置了如何连接Redsi服务器(如何安装Redis,2)oxmSerializer()是我新增的spring-data-redis
中的核心操作类是
RedisTemplate&K, V&gt
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁spring boot集成redis为什么报数据源装载失败_百度知道
spring boot集成redis为什么报数据源装载失败
我有更好的答案
在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失。
常规的解决方案都是使用:如apache使用mod_jk.conf。
在开发spring boot app的时候可以借助 spring session 和redis,用外置的redis来存储session的状态。
直接上代码,我这边直接默认你使用spring boot,如果你是普通的spring web项目,请参照 /spring-projects/spring-session ,在spring boot配置更简单
1、增加repository到pom.xml
&repository&
&id&spring-milestone&/id&
&url&https://repo.spring.io/libs-milestone&/url&
&/repository&
2、增加相关依赖
&dependency&
&groupId&or...
其他类似问题
为您推荐:
redis的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 springboot集成redis 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信