当前位置: 首页 > news >正文

曲靖高端网站制作数据需求 网站建设

曲靖高端网站制作,数据需求 网站建设,北京网站开发公司飞沐,域名数和网站数目录 一、前言二、基础集成配置#xff08;redis单节点#xff09;2.1、POM2.2、添加配置文件application.yml2.3、编写配置文件2.4、编写启动类2.5、编写测试类测试是否连接成功 一、前言 spring-boot-starter-data-redis有两种实现 lettuce 和 jedis#xff0c;spring bo… 目录 一、前言二、基础集成配置redis单节点2.1、POM2.2、添加配置文件application.yml2.3、编写配置文件2.4、编写启动类2.5、编写测试类测试是否连接成功 一、前言 spring-boot-starter-data-redis有两种实现 lettuce 和 jedisspring boot 2的spring-boot-starter-data-redis中默认使用的是lettuce作为redis客户端也推荐使用lettuce它与jedis的主要区别如下 Jedis Jedis是同步的不支持异步Jedis客户端实例不是线程安全的需要每个线程一个Jedis实例所以一般通过连接池来使用Jedis.优点 提供了比较全面的 Redis 操作特性的 APIAPI 基本与 Redis 的指令一一对应使用简单易理解 缺点 同步阻塞 IO不支持异步线程不安全 Lettuce Lettuce是基于Netty框架的事件驱动的Redis客户端其方法调用是异步的Lettuce的API也是线程安全的所以多个线程可以操作单个Lettuce连接来完成各种操作同时Lettuce也支持连接池.优点 线程安全基于 Netty 框架的事件驱动的通信支持异步和响应式编程适用于分布式缓存支持集群Sentinel管道和编码器等等功能 缺点 API 更抽象学习使用成本高不过我们基本都使用的RedisTemplate来操作Redis它抽象了Jedis或者Lettuce客户端底层实现我们可以不用关心 二、基础集成配置redis单节点 工程结构 2.1、POM parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.12.RELEASE/version/parentdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency!--springboot中的redis依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- lettuce pool 缓存连接池--dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependency!-- 使用jackson作为redis数据序列化 --dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.11.4/version/dependency!-- SpringBoot测试包 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies2.2、添加配置文件application.yml 因为我们用的spring-boot-starter-data-redis包会自动配置redis连接在配置文件中添加对应配置即可 spring:#redis配置信息redis:## Redis数据库索引默认为0database: 0## Redis服务器地址host: 172.16.8.169## Redis服务器连接端口port: 6379## Redis服务器连接密码默认为空password: 123456## 连接超时时间毫秒timeout: 1200lettuce:pool:## 连接池最大连接数使用负值表示没有限制max-active: 8## 连接池最大阻塞等待时间使用负值表示没有限制max-wait: -1## 连接池中的最大空闲连接max-idle: 8## 连接池中的最小空闲连接min-idle: 12.3、编写配置文件 这里需要添加一个RedisTemplate的bean设置这个RedisTemplate序列化方式为jackson import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.*; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration public class RedisConfig{/*** retemplate相关配置*/Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory factory) {RedisTemplateString, Object template new RedisTemplate();// 配置连接工厂template.setConnectionFactory(factory);//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值默认使用JDK的序列化方式Jackson2JsonRedisSerializer jacksonSeial new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();// 指定要序列化的域field,get和set,以及修饰符范围ANY是都有包括private和publicom.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 指定序列化输入的类型类必须是非final修饰的final修饰的类比如String,Integer等会跑出异常om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jacksonSeial.setObjectMapper(om);// 值采用json序列化template.setValueSerializer(jacksonSeial);//使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());// 设置hash key 和value序列化模式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(jacksonSeial);template.afterPropertiesSet();return template;} }2.4、编写启动类 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication public class LettuceApplication {public static void main(String[] args) {SpringApplication.run(LettuceApplication.class);} }2.5、编写测试类测试是否连接成功 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.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest(classes LettuceApplication.class) public class LettuceTest {Autowiredprivate RedisTemplateString,Object redisTemplate;Testpublic void t1(){String key key1;System.out.println(插入数据到redis);redisTemplate.opsForValue().set(key,value1);Object value redisTemplate.opsForValue().get(key);System.out.println(从redis中获取到值为 value);Boolean delete redisTemplate.delete(key);System.out.println(删除redis中值 delete);} }
http://www.zqtcl.cn/news/496450/

相关文章:

  • 如何在网站后台删除栏目阿里巴巴上做网站要多少钱
  • 网站建设意识形态工作河北省两学一做网站
  • 綦江建站哪家正规php做不了大型网站吗
  • 优秀的设计网站青岛网站设计企业
  • 谁有做爰网站号wordpress 4.8 中文
  • 毕业设计做网站用什么广州中智软件开发有限公司
  • 哪个网站不花钱可以做招聘wordpress没有页脚
  • 免费视频网站素材网络系统管理技能大赛
  • 聊天网站建设网站建设毕业设计评价
  • 网站建设 内容缺乏域名备案要多久
  • 产品展示型网站建设全国新冠疫苗接种率
  • 网站建设商如何自建商城和电商平台
  • 深圳做二类学分的网站开发一平方米多少钱
  • 如何做原创小说网站建一个o2o网站
  • 东莞市住房建设网站互动科技 网站建设
  • 淄博网站建设高端网络seo线上培训多少钱
  • s网站优化工地模板图片
  • 手机网站使用微信支付神级网页设计网站
  • 网站建站大约多少钱如何引流被动加好友
  • 哪些网站可以查企业信息大城县有做网站的吗
  • 上海网站建设电影联wordpress 分类title
  • 杭州网站建设招标免费seo排名优化
  • 网站建设服务费是否无形资产百度一下你就知道官网下载安装
  • 网站付款链接怎么做在线设计商标logo
  • 阿里巴巴做网站多少钱特大新闻凌晨刚刚发生
  • 网站如何做se设计师网站pintset
  • 上海网站制作机构wordpress 优酷免广告
  • 关于网站建设的名言网站开发的技术难点
  • 免费云建站廊坊seo外包
  • 个人网站建设方案书用备案的衡水市网站制作