Redis服务工程

①坐标

groupId:com.atguigu.crowd

artifactId:distribution-crowd-4-redis-provider

packaging:jar

[注:同样是Maven Module]

②依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

③配置文件

server:port: 3000
spring:application:name: redis-providerredis:host: 192.168.56.150
eureka:client:service-url:defaultZone: http://localhost:1000/eureka/instance:prefer-ip-address: true

④主启动类

@EnableDiscoveryClient
@SpringBootApplication
public class CrowdMainType {public static void main(String[] args) {SpringApplication.run(CrowdMainType.class, args);}}

⑤访问Redis测试

@Autowired
private RedisTemplate<Object, Object> redisTemplate;@Autowired
private StringRedisTemplate stringRedisTemplate;@Test
public void testRedisTemplate() {redisTemplate.opsForValue().set("pig", "red");
}@Test
public void testStringRedisTemplate() {stringRedisTemplate.opsForValue().set("newYear", "oleYear");
}

⑥Redis整合相关

RedisAutoConfiguration

/** Copyright 2012-2018 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.boot.autoconfigure.data.redis;import java.net.UnknownHostException;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;/*** {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis support.** @author Dave Syer* @author Andy Wilkinson* @author Christian Dupuis* @author Christoph Strobl* @author Phillip Webb* @author Eddú Meléndez* @author Stephane Nicoll* @author Marco Aust* @author Mark Paluch*/
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {@Bean@ConditionalOnMissingBean(name = "redisTemplate")public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);return template;}@Bean@ConditionalOnMissingBeanpublic StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}}

RedisProperties

/** Copyright 2012-2017 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.boot.autoconfigure.data.redis;import java.time.Duration;
import java.util.List;import org.springframework.boot.context.properties.ConfigurationProperties;/*** Configuration properties for Redis.** @author Dave Syer* @author Christoph Strobl* @author Eddú Meléndez* @author Marco Aust* @author Mark Paluch* @author Stephane Nicoll*/
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {/*** Database index used by the connection factory.*/private int database = 0;/*** Connection URL. Overrides host, port, and password. User is ignored. Example:* redis://user:password@example.com:6379*/private String url;/*** Redis server host.*/private String host = "localhost";/*** Login password of the redis server.*/private String password;/*** Redis server port.*/private int port = 6379;/*** Whether to enable SSL support.*/private boolean ssl;/*** Connection timeout.*/private Duration timeout;private Sentinel sentinel;private Cluster cluster;private final Jedis jedis = new Jedis();private final Lettuce lettuce = new Lettuce();public int getDatabase() {return this.database;}public void setDatabase(int database) {this.database = database;}public String getUrl() {return this.url;}public void setUrl(String url) {this.url = url;}public String getHost() {return this.host;}public void setHost(String host) {this.host = host;}public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}public int getPort() {return this.port;}public void setPort(int port) {this.port = port;}public boolean isSsl() {return this.ssl;}public void setSsl(boolean ssl) {this.ssl = ssl;}public void setTimeout(Duration timeout) {this.timeout = timeout;}public Duration getTimeout() {return this.timeout;}public Sentinel getSentinel() {return this.sentinel;}public void setSentinel(Sentinel sentinel) {this.sentinel = sentinel;}public Cluster getCluster() {return this.cluster;}public void setCluster(Cluster cluster) {this.cluster = cluster;}public Jedis getJedis() {return this.jedis;}public Lettuce getLettuce() {return this.lettuce;}/*** Pool properties.*/public static class Pool {/*** Maximum number of "idle" connections in the pool. Use a negative value to* indicate an unlimited number of idle connections.*/private int maxIdle = 8;/*** Target for the minimum number of idle connections to maintain in the pool. This* setting only has an effect if it is positive.*/private int minIdle = 0;/*** Maximum number of connections that can be allocated by the pool at a given* time. Use a negative value for no limit.*/private int maxActive = 8;/*** Maximum amount of time a connection allocation should block before throwing an* exception when the pool is exhausted. Use a negative value to block* indefinitely.*/private Duration maxWait = Duration.ofMillis(-1);public int getMaxIdle() {return this.maxIdle;}public void setMaxIdle(int maxIdle) {this.maxIdle = maxIdle;}public int getMinIdle() {return this.minIdle;}public void setMinIdle(int minIdle) {this.minIdle = minIdle;}public int getMaxActive() {return this.maxActive;}public void setMaxActive(int maxActive) {this.maxActive = maxActive;}public Duration getMaxWait() {return this.maxWait;}public void setMaxWait(Duration maxWait) {this.maxWait = maxWait;}}/*** Cluster properties.*/public static class Cluster {/*** Comma-separated list of "host:port" pairs to bootstrap from. This represents an* "initial" list of cluster nodes and is required to have at least one entry.*/private List<String> nodes;/*** Maximum number of redirects to follow when executing commands across the* cluster.*/private Integer maxRedirects;public List<String> getNodes() {return this.nodes;}public void setNodes(List<String> nodes) {this.nodes = nodes;}public Integer getMaxRedirects() {return this.maxRedirects;}public void setMaxRedirects(Integer maxRedirects) {this.maxRedirects = maxRedirects;}}/*** Redis sentinel properties.*/public static class Sentinel {/*** Name of the Redis server.*/private String master;/*** Comma-separated list of "host:port" pairs.*/private List<String> nodes;public String getMaster() {return this.master;}public void setMaster(String master) {this.master = master;}public List<String> getNodes() {return this.nodes;}public void setNodes(List<String> nodes) {this.nodes = nodes;}}/*** Jedis client properties.*/public static class Jedis {/*** Jedis pool configuration.*/private Pool pool;public Pool getPool() {return this.pool;}public void setPool(Pool pool) {this.pool = pool;}}/*** Lettuce client properties.*/public static class Lettuce {/*** Shutdown timeout.*/private Duration shutdownTimeout = Duration.ofMillis(100);/*** Lettuce pool configuration.*/private Pool pool;public Duration getShutdownTimeout() {return this.shutdownTimeout;}public void setShutdownTimeout(Duration shutdownTimeout) {this.shutdownTimeout = shutdownTimeout;}public Pool getPool() {return this.pool;}public void setPool(Pool pool) {this.pool = pool;}}}
package com.leon.crowd.test;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.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class CrowdTest {@Autowiredprivate RedisTemplate<Object, Object> redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Testpublic void testSaveAndGetValueToRedisByRedisTemplate() {// 获取Redis操作器ValueOperations<Object, Object> operator = redisTemplate.opsForValue();// 设置值// operator.set("keyone", "valueone");// 获取值Object value = operator.get("keyone");System.out.println(value);}@Testpublic void testSaveAndGetValueToRedisByStringRedisTemplate() {// 获取Redis操作器ValueOperations<String, String> operator = stringRedisTemplate.opsForValue();// 设置值// operator.set("keytwo", "valuetwo");// 获取值String value = operator.get("keytwo");System.out.println(value);}}

创建工程并测试RedisTemplate相关推荐

  1. Robot Framework自动化测试教程-通过RIDE创建工程、测试套、测试用例、测试资源、变量文件,引入测试库

    1. 创建测试工程 RIDE工具中有Project概念,实际上Robot Framework中是没有工程的概念,可以理解为最顶层的测试套. 1.1. 新建测试工程 点击 File–>New Pr ...

  2. 第1章 Qt概述和下载安装及创建工程

    目录 1.1 什么是Qt 1.2 Qt的下载及安装 1.3 开发界面及创建工程介绍 1.1 什么是Qt 学习一项技能,首先要了解一下这项技能能做什么,那Qt到底是什么,能用它做什么呢? Qt是什么?简 ...

  3. Swift游戏实战-跑酷熊猫 01 创建工程导入素材

    原文:Swift游戏实战-跑酷熊猫 01 创建工程导入素材 在这节里,我们将建立一个游戏工程,并导入一些必要的素材,例如序列帧动画文件,声音素材文件.动画文件我们使用atlas形式.在打包发布或者模拟 ...

  4. android微信分享之创建工程以及启动微信

    android微信分享之创建工程 1.微信jar包在微信--->资源中心-->资源下载 中进行下载! 2.项目结构: 3.启动微信: private static final String ...

  5. Matlab:创建工程

    Matlab:创建工程 什么是工程? 创建工程 打开工程 设置工程 将文件添加到工程 创建工程的其他方法 从存档工程创建工程 使用 Simulink 创建工程 什么是工程? 工程是一种可扩展的环境,您 ...

  6. 米联客 ZYNQ/SOC精品教程 S01-CH04 VIVADO创建工程之流水灯

    软件版本:VIVADO2017.4 操作系统:WIN10 64bit 硬件平台:适用米联客 ZYNQ系列开发板 米联客(MSXBO)论坛:www.osrc.cn答疑解惑专栏开通,欢迎大家给我提问!! ...

  7. 基于STM32+使用标准库创建工程--手把手纯新手教学

    前言         这个博客的意义就是为了帮助新手快速创建一个基于STM32的工程模板,主要记录从零创建一个全新的STM32F103的项目过程,大部分是自己收集和整理,如有侵权请联系我删除. 本博客 ...

  8. Installshield6.x基础入门(一)创建工程

    Installshield6.x基础入门(一)创建工程 Installshield 6.x是2000年发布的32位软件,支持操作系统最高为Windows2000. InstallShield 12是2 ...

  9. 创建Django项目和模型(创建工程、子应用、设置pycharm环境、使用Django进行数据库开发的步骤)

    1.创建Django项目 文档:Writing your first Django app, part 1 | Django documentation | Django 步骤 创建Django项目 ...

最新文章

  1. http://blog.csdn.net/fanzhonglei
  2. 【Google Play】APK 扩展包 ( 2021年09月02日最新处理方案 | 扩展文件名格式 | 扩展文件下载存放地址 )
  3. 如何学习Linux性能优化?
  4. mysql中,一条select语句是如何执行的?
  5. Oracle小复习(1)
  6. 如何看待快手领投知乎4.34亿美元融资?创始人周源亲自下场回答
  7. Android Debug Bridge 技术实现原理
  8. 网安入门须知:Python基础导读
  9. 【数模】模糊综合评价模型
  10. 迅盘技术(Robson)、Ready Boost和Ready Drive的区别
  11. 【安全牛学习笔记】拒绝服务攻击工具-NMAP、匿名者拒绝服务工具包(匿名者发布的DoS工具)、其他拒绝服务工具-XOIC、HULK、DDOSIM、GoldenEye
  12. 磁阻式随机存储器MRAM基本原理
  13. cygwin安装apt-cyg命令
  14. 【数据可视化】Echarts世界地图需要的数据 - JSON格式世界国家中英文对照表
  15. 同构网络vs异构网络
  16. 笔记本电脑键盘输入错误如何解决 电脑按键错乱的解决方法步骤
  17. a[i][j] 和 a[j][i] 的区别
  18. 女人要怀有一颗珍惜之心
  19. 垂死挣扎的 Flash Player 再爆零日漏洞,影响多个平台
  20. 【LeetCode69】x的平方

热门文章

  1. AF_UNIX和AF_INET
  2. SSL certificate problem: unable to get local issuer certificate 的解决方法
  3. 功能测试工具Selenium IDE
  4. Mysql一些导入导出数据库,添加修改字段命令
  5. Struts标签、Ognl表达式、el表达式、jstl标签库这四者之间的关系和各自作用
  6. jdk1.8新特性(五)——Stream
  7. 【JMS】JMS详解
  8. iptables规则备份和恢复 firewalld服务
  9. docker 服务器engin开放2376端口给pycharm连接
  10. python基础-------python2.7教程学习【廖雪峰版】(二)