日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

创建工程并测试RedisTemplate

發布時間:2024/4/13 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 创建工程并测试RedisTemplate 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。