Commit 841d6657 by QIANGLU

增加redis使用demo

parent a551176b
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<parent> <parent>
<artifactId>matrix</artifactId> <artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId> <groupId>com.secoo.mall</groupId>
<version>1.1.0.RELEASE</version> <version>1.1.8.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<parent> <parent>
<artifactId>matrix</artifactId> <artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId> <groupId>com.secoo.mall</groupId>
<version>1.0.11.RELEASE</version> <version>1.1.8.RELEASE</version>
</parent> </parent>
<properties> <properties>
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<parent> <parent>
<artifactId>matrix</artifactId> <artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId> <groupId>com.secoo.mall</groupId>
<version>1.0.11.RELEASE</version> <version>1.1.8.RELEASE</version>
</parent> </parent>
<properties> <properties>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nova</groupId>
<artifactId>spring-boot-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId>
<version>1.1.8.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>logger-starter</artifactId>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>redis-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
\ No newline at end of file
package com.matrix.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author qianglu
*/
@SpringBootApplication
public class RedisStartup {
public static void main(String[] args) {
SpringApplication.run(RedisStartup.class, args);
}
}
package com.matrix.redis;
import com.secoo.mall.redis.utils.MatrixRedisClusterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.websocket.SendResult;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @ClassName RedisDemoService
* @Author QIANGLU
* @Date 2019/12/11 3:45 下午
* @Version 1.0
*/
@Component
public class RedisDemoService implements Runnable {
private Logger logger = LoggerFactory.getLogger(RedisDemoService.class);
private ScheduledFuture<?> scheduledFuture;
private volatile AtomicInteger count = new AtomicInteger();
public RedisDemoService() {
scheduledFuture = Executors
.newSingleThreadScheduledExecutor().scheduleAtFixedRate(this, 0, 1, TimeUnit.SECONDS);
}
@Override
public void run() {
try {
//存入Redis
MatrixRedisClusterUtils.opsForHash().put("test_hashs", "hash", count.incrementAndGet()+"_hash");
logger.info("存入Redis Hash 数据 key:{},val:{}", "test_hashs_t1", count.get());
//set
MatrixRedisClusterUtils.opsForSet().add("test_sets", count.get() + "_set");
logger.info("存入Redis Set 数据 key:{},val:{}", "test_sets", count.get());
MatrixRedisClusterUtils.opsForValue().set("test_string", count.get() + "_string");
logger.info("存入Redis Value 数据 key:{},val:{}", "test_string", count.get());
//更多方法请查看 MatrixRedisClusterUtils
//取出
Object obj = MatrixRedisClusterUtils.opsForHash().get("test_hashs", "hash");
logger.info("取出Redis Hash 数据 key:{},val:{}", "test_hashs", obj);
//set
Set<String> sets = MatrixRedisClusterUtils.opsForSet().members("test_set");
logger.info("取出Redis Set 数据 key:{},size:{}", "test_set", sets.size());
String val = MatrixRedisClusterUtils.opsForValue().get("test_string");
logger.info("取出Redis Set 数据 key:{},val:{}", "test_string", val);
System.out.println("===========================================");
} catch (Exception e) {
System.out.println(e);
}
}
}
server:
port: 6080
spring:
application:
name: learn-redis
redis: # 配置可放入Apollo
host: 10.0.253.9 #单机模式下使用此配置
# cluster: 集群模式下打开此注解
# nodes: 192.168.xx.4:7000,192.168.xx.14:7002,192.168.xx.18:7004
# max-redirects: 6
lettuce:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
min-idle: 8 # 连接池中的最小空闲连接
max-wait: 2000 # 连接池最大阻塞等待时间(使用负值表示没有限制)
timeout: 2000 # 连接超时时间(毫秒)
package com.matrix.pure; package com.matrix.redis;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
......
package com.matrix.pure.rocketmq; package com.matrix.redis.rocketmq;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener; import org.apache.rocketmq.spring.core.RocketMQListener;
......
package com.matrix.pure.rocketmq; package com.matrix.redis.rocketmq;
import org.apache.rocketmq.client.producer.SendResult; import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message; import org.apache.rocketmq.common.message.Message;
......
package com.matrix.pure.tcp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
/**
* @ClassName NettyServer
* @Author QIANGLU
* @Date 2019/12/3 11:31 下午
* @Version 1.0
*/
public class NettyClient {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.bind(new InetSocketAddress("localhost", 9999));
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.put("this is test ".getBytes());
byteBuffer.flip();
while (byteBuffer.hasRemaining()){
socketChannel.write(byteBuffer);
}
}
}
package com.matrix.pure.tcp;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
/**
* @ClassName NettyServer
* @Author QIANGLU
* @Date 2019/12/3 11:31 下午
* @Version 1.0
*/
public class NettyServer {
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
ServerSocketChannel socketChannel = ServerSocketChannel.open();
socketChannel.bind(new InetSocketAddress("loclhost", 9999));
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select();
Iterator<SelectionKey> selectorIterator = selector.selectedKeys().iterator();
while (selectorIterator.hasNext()){
SelectionKey selectionKey = selectorIterator.next();
if(selectionKey.isAcceptable()){
SocketChannel channel = socketChannel.accept();
channel.configureBlocking(false);
channel.register(selector,SelectionKey.OP_READ,ByteBuffer.allocate(1024));
}
if(selectionKey.isReadable()){
}
}
//
SocketChannel socketChannel1 = socketChannel.accept();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int reder = socketChannel1.read(buffer);
if (reder > 0) {
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
String body = new String(bytes,"UTF-8");
System.out.println("server 收到:" + body);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment