From 37b2e923dc68d765f27ffda16e2507e8b0fd60eb Mon Sep 17 00:00:00 2001 From: "qinxiaodong@pannk.com" Date: Fri, 13 Nov 2020 15:20:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90Redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- integrate-redis/pom.xml | 43 +++++++++++++++++++ .../src/main/java/com/pannk/demo/App.java | 43 +++++++++++++++++++ .../com/pannk/demo/config/RedisConfig.java | 23 ++++++++++ .../src/main/resources/application.yml | 14 ++++++ pom.xml | 1 + 5 files changed, 124 insertions(+) create mode 100644 integrate-redis/pom.xml create mode 100644 integrate-redis/src/main/java/com/pannk/demo/App.java create mode 100644 integrate-redis/src/main/java/com/pannk/demo/config/RedisConfig.java create mode 100644 integrate-redis/src/main/resources/application.yml diff --git a/integrate-redis/pom.xml b/integrate-redis/pom.xml new file mode 100644 index 0000000..cd4c502 --- /dev/null +++ b/integrate-redis/pom.xml @@ -0,0 +1,43 @@ + + + + springboot-demo + com.pannk + 1.0 + + 4.0.0 + + integrate-redis + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-redis + + + io.lettuce + lettuce-core + + + + + redis.clients + jedis + + + org.projectlombok + lombok + 1.18.4 + + + + \ No newline at end of file diff --git a/integrate-redis/src/main/java/com/pannk/demo/App.java b/integrate-redis/src/main/java/com/pannk/demo/App.java new file mode 100644 index 0000000..1668738 --- /dev/null +++ b/integrate-redis/src/main/java/com/pannk/demo/App.java @@ -0,0 +1,43 @@ +package com.pannk.demo; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.types.RedisClientInfo; + +import java.util.List; + +/** + * Created by wolf on 20-11-13. + */ +@Slf4j +@SpringBootApplication +public class App implements CommandLineRunner{ + + @Autowired + private RedisTemplate redisTemplate; + + public static void main(String[] args) { + SpringApplication.run(App.class,args); + } + + + @Override + public void run(String... args) throws Exception { + /*redisTemplate.opsForValue().set("hello","Hello world redis"); + String hello = (String) redisTemplate.opsForValue().get("hello"); + List clientList = redisTemplate.getClientList(); + clientList.stream().forEach(info->{ + log.error("info:{}",info); + }); + log.error("=========hello:{}",hello); +*/ + for (int i = 0; i < 1000; i++) { + new Thread(() -> redisTemplate.opsForValue().set("key"+Thread.currentThread().getName(),"The value is "+Thread.currentThread().getId())).start(); + } + + } +} diff --git a/integrate-redis/src/main/java/com/pannk/demo/config/RedisConfig.java b/integrate-redis/src/main/java/com/pannk/demo/config/RedisConfig.java new file mode 100644 index 0000000..84286f7 --- /dev/null +++ b/integrate-redis/src/main/java/com/pannk/demo/config/RedisConfig.java @@ -0,0 +1,23 @@ +package com.pannk.demo.config; + +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.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * Created by wolf on 20-11-13. + */ +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory){ + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + return redisTemplate; + } +} diff --git a/integrate-redis/src/main/resources/application.yml b/integrate-redis/src/main/resources/application.yml new file mode 100644 index 0000000..3ef22f1 --- /dev/null +++ b/integrate-redis/src/main/resources/application.yml @@ -0,0 +1,14 @@ +spring: + main: + banner-mode: off + redis: + host: localhost + port: 6379 + password: + database: 0 + jedis: + pool: + max-idle: 10 + min-idle: 10 + max-wait: -1ms + max-active: 100 diff --git a/pom.xml b/pom.xml index 7d47ea0..1a54a73 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ integrate-mybatisplus testing integrate-druid + integrate-redis org.springframework.boot -- GitLab