Redis修正密码及登录验证,及三种操作计划 A8站源码交易平台

摘要:@Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) public class RedisAutoConfiguration { @Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate redisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }

A8站源码交易平台

1.修改密码:redis.windows.conf文件找到#requirepass  foobared.把这排改为requirepass  yourpassword(设置的密码)


1.png


2.启动

redis-server.exe   redis.windows.conf  或加上.,即. edis-server.exe   redis.windows.conf 


2.jpg


3.登录redis数据库


3.jpg


4.注册服务

只需一封闭cmd窗口,redis就会消失。所以要把redis设置成windows下的服务
4.png




卸载服务:redis-server --service-uninstall

敞开服务:redis-server --service-start

中止服务:redis-server --service-stop

5.springboot

pom依靠:


   org.springframework.boot
   spring-boot-starter-data-redis

后台代码:

private static Jedis jedis = new Jedis("192.168.1.66",6379);
jedis.auth("test123");
Jedis域名19.168.1.66,假如装备里边没有装备,需求改为127.0.0.1.假如改就修正redis.conf 装备文件  bind 127.0.0.1  为 bind 101.200.166.125(真是ip地址)

关于操作redis的三种操作计划:
创立工程,引进 Redis 依靠:

5.png
 

创立成功后,还需求手动引进 commos-pool2 的依靠,因而终究完好的 pom.xml 依靠如下:


org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-web


org.apache.commons

commons-pool2



这儿首要便是引进了 Spring Data Redis + 连接池。

装备 Redis 信息
接下来装备 Redis 的信息,信息包括两方面,一方面是 Redis 的根本信息,另一方面则是连接池信息:

spring.redis.database=0

spring.redis.password=123

spring.redis.port=6379

spring.redis.host=192.168.66.128

spring.redis.lettuce.pool.min-idle=5

spring.redis.lettuce.pool.max-idle=10

spring.redis.lettuce.pool.max-active=8

spring.redis.lettuce.pool.max-wait=1ms

spring.redis.lettuce.shutdown-timeout=100ms

主动装备
当开发者在项目中引进了 Spring Data Redis ,而且装备了 Redis 的根本信息,此刻,主动化装备就会收效。A8站源码交易平台

咱们从 Spring Boot 中 Redis 的主动化装备类中就能够看出端倪:

@Configuration

@ConditionalOnClass(RedisOperations.class)

@EnableConfigurationProperties(RedisProperties.class)

@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })

public class RedisAutoConfiguration {

@Bean

@ConditionalOnMissingBean(name = "redisTemplate")

public RedisTemplate redisTemplate(

RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

RedisTemplate template = new RedisTemplate<>();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

@Bean

@ConditionalOnMissingBean

public StringRedisTemplate stringRedisTemplate(

RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

StringRedisTemplate template = new StringRedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

}

这个主动化装备类很好了解:

首要符号这个是一个装备类,一起该装备在 RedisOperations 存在的情况下才会收效(即项目中引进了 Spring Data Redis)

然后导入在 application.properties 中装备的特点

然后再导入连接池信息(假如存在的话)

最终,供给了两个 Bean ,RedisTemplate 和 StringRedisTemplate ,其间 StringRedisTemplate 是 RedisTemplate 的子类,两个的办法根本共同,不同之处首要体现在操作的数据类型不同,RedisTemplate 中的两个泛型都是 Object ,意味者存储的 key 和 value 都能够是一个目标,而 StringRedisTemplate 的 两个泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都只能是字符串。假如开发者没有供给相关的 Bean ,这两个装备就会收效,不然不会收效。A8站源码交易平台

运用
接下来,能够直接在 Service 中注入 StringRedisTemplate 或许 RedisTemplate 来运用:

@Service

public class HelloService {

@Autowired

RedisTemplate redisTemplate;

public void hello() {

ValueOperations ops = redisTemplate.opsForValue();

ops.set("k1", "v1");

Object k1 = ops.get("k1");

System.out.println(k1);

}

}

Redis 中的数据操作,大体上来说,能够分为两种:

针对 key 的操作,相关的办法就在 RedisTemplate 中

针对详细数据类型的操作,相关的办法需求首要获取对应的数据类型,获取相应数据类型的操作办法是 opsForXXX

调用该办法就能够将数据存储到 Redis 中去了,如下:

A8站源码交易平台

6.png



 

k1 前面的字符是因为运用了 RedisTemplate 导致的,RedisTemplate 对 key 进行序列化之后的成果。

RedisTemplate 中,key 默许的序列化计划是 JdkSerializationRedisSerializer 。

而在 StringRedisTemplate 中,key 默许的序列化计划是 StringRedisSerializer ,因而,假如运用 StringRedisTemplate ,默许情况下 key 前面不会有前缀。

不过开发者也能够自行修正 RedisTemplate 中的序列化计划,如下:

@Service

public class HelloService {

@Autowired

RedisTemplate redisTemplate;

public void hello() {

redisTemplate.setKeySerializer(new StringRedisSerializer());

ValueOperations ops = redisTemplate.opsForValue();

ops.set("k1", "v1");

Object k1 = ops.get("k1");

System.out.println(k1);

}

}

当然也能够直接运用 StringRedisTemplate:

@Service

public class HelloService {

@Autowired

StringRedisTemplate stringRedisTemplate;

public void hello2() {

ValueOperations ops = stringRedisTemplate.opsForValue();

ops.set("k2", "v2");

Object k1 = ops.get("k2");

System.out.println(k1);

}


}


别的需求留意 ,Spring Boot 的主动化装备,只能装备单机的 Redis ,假如是 Redis 集群,则一切的东西都需求自己手动装备。A8站源码交易平台

计划二:Spring Cache
经过 Spring Cache 的方式来操作 Redis,Spring Cache 一致了缓存江湖的门面,这种计划,松哥之前有过一篇专门的文章介绍,小伙伴能够移步这儿:Spring Boot中,Redis缓存还能这么用。

计划三:回归原始时代
第三种计划,便是直接运用 Jedis 或许 其他的客户端东西来操作 Redis ,这种计划在 Spring Boot 中也是支撑的,尽管操作费事,可是支撑。

总结
Spring Boot 中,Redis 的操作,这儿总结了三种计划,实践上前两个运用广泛一些,直接运用 Jedis 仍是比较少。



A8站源码交易平台



  • 全部评论(0)
最新发布的资讯信息
【A8站-免费源码分享|直播源码】直播系统源码开发:关于安卓开发工具和obs直播推流(2020-10-30 09:41)
【计算机/互联网|】【独家修复】用户定制版短视频点赞系统,支持抖音+快手+刷宝+微视等所有主流短视频评论系统源码(2020-10-26 16:34)
【技术宅-为技术而疯|网站架设教程】【独家首发】最新更新已对接短信2020全新抖音快手点赞任务系统霸屏天下小红书头条威客兼职完整搭建架设视频教程(2020-10-25 13:56)
【技术宅-为技术而疯|网站架设教程】最新可用个人发卡网系统源码完整搭建架设视频教程(2020-10-25 10:53)
【技术宅-为技术而疯|网站架设教程】独家更新全新V10抢单系统唯品会京东淘宝自动抢单区块系统源码全开源抢单收单接单返利+搭建架设完整视频教程(2020-10-25 10:52)
【计算机/互联网|】柔丫纸尿裤云仓系统源码部署(2020-10-15 18:13)
【计算机/互联网|】侏罗纪软件模式定制开发(2020-10-14 15:33)
【计算机/互联网|】S2b2C供应商系统 营销闭环(2020-10-10 17:46)
【计算机/互联网|程序设计开发】盛都汇系统奖励机制(2020-10-10 17:20)
【A8站-免费源码分享|网站源码】积分商城系统APP开发(2020-09-23 14:56)
联系我们
Q Q:3101359898 点击直接对话
电话:18580901894
邮箱:admin#a8zhan.com
时间:09:00 - 24:00
手机二维码 访问手机版
返回顶部