Skip to content

Commit 8e18480

Browse files
committed
update 23
1 parent badd48d commit 8e18480

File tree

11 files changed

+4078
-0
lines changed

11 files changed

+4078
-0
lines changed

Diff for: 08cache/ha/conf/redis6379.conf

+1,868
Large diffs are not rendered by default.

Diff for: 08cache/ha/conf/redis6380.conf

+1,868
Large diffs are not rendered by default.

Diff for: 08cache/ha/conf/sentinel0.conf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sentinel myid 8d992c54df8f8677b0b345825f61fb733c73d14c
2+
sentinel deny-scripts-reconfig yes
3+
sentinel monitor mymaster 127.0.0.1 6379 2
4+
sentinel down-after-milliseconds mymaster 10000
5+
# Generated by CONFIG REWRITE
6+
protected-mode no
7+
port 26379
8+
user default on nopass ~* +@all
9+
dir "/Users/kimmking/kimmking/JavaCourseCodes/08cache/ha/conf"
10+
sentinel config-epoch mymaster 2
11+
sentinel leader-epoch mymaster 2
12+
sentinel known-replica mymaster 127.0.0.1 6380
13+
sentinel known-sentinel mymaster 127.0.0.1 26380 8d992c54df8f8677b0b345825f61fb733c73d14d
14+
sentinel current-epoch 2

Diff for: 08cache/ha/conf/sentinel1.conf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sentinel myid 8d992c54df8f8677b0b345825f61fb733c73d14d
2+
sentinel deny-scripts-reconfig yes
3+
sentinel monitor mymaster 127.0.0.1 6379 2
4+
sentinel down-after-milliseconds mymaster 10000
5+
# Generated by CONFIG REWRITE
6+
protected-mode no
7+
port 26380
8+
user default on nopass ~* +@all
9+
dir "/Users/kimmking/kimmking/JavaCourseCodes/08cache/ha/conf"
10+
sentinel config-epoch mymaster 2
11+
sentinel leader-epoch mymaster 2
12+
sentinel known-replica mymaster 127.0.0.1 6380
13+
sentinel known-sentinel mymaster 127.0.0.1 26379 8d992c54df8f8677b0b345825f61fb733c73d14c
14+
sentinel current-epoch 2

Diff for: 08cache/redis/pom.xml

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.0.9.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>io.kimmking.08cache</groupId>
12+
<artifactId>redis</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>redis</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.projectlombok</groupId>
34+
<artifactId>lombok</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>redis.clients</groupId>
39+
<artifactId>jedis</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.alibaba</groupId>
44+
<artifactId>fastjson</artifactId>
45+
<version>1.2.70</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.redisson</groupId>
50+
<artifactId>redisson-spring-boot-starter</artifactId>
51+
<version>3.14.1</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>com.hazelcast</groupId>
56+
<artifactId>hazelcast</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-data-redis</artifactId>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-test</artifactId>
67+
<scope>test</scope>
68+
<exclusions>
69+
<exclusion>
70+
<groupId>org.junit.vintage</groupId>
71+
<artifactId>junit-vintage-engine</artifactId>
72+
</exclusion>
73+
</exclusions>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.springframework.boot</groupId>
81+
<artifactId>spring-boot-maven-plugin</artifactId>
82+
</plugin>
83+
</plugins>
84+
</build>
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.kimmking.cache;
2+
3+
import lombok.SneakyThrows;
4+
import redis.clients.jedis.HostAndPort;
5+
import redis.clients.jedis.JedisCluster;
6+
import redis.clients.jedis.JedisPoolConfig;
7+
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
public final class ClusterJedis {
12+
13+
private static JedisCluster CLUSTER = createJedisCluster();
14+
15+
private static JedisCluster createJedisCluster() {
16+
JedisCluster jedisCluster = null;
17+
// 添加集群的服务节点Set集合
18+
Set<HostAndPort> hostAndPortsSet = new HashSet<HostAndPort>();
19+
// 添加节点
20+
hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6379));
21+
hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6380));
22+
23+
// Jedis连接池配置
24+
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
25+
// 最大空闲连接数, 默认8个
26+
jedisPoolConfig.setMaxIdle(12);
27+
// 最大连接数, 默认8个
28+
jedisPoolConfig.setMaxTotal(16);
29+
//最小空闲连接数, 默认0
30+
jedisPoolConfig.setMinIdle(4);
31+
// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
32+
jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
33+
//对拿到的connection进行validateObject校验
34+
jedisPoolConfig.setTestOnBorrow(true);
35+
jedisCluster = new JedisCluster(hostAndPortsSet, jedisPoolConfig);
36+
return jedisCluster;
37+
}
38+
39+
public static JedisCluster getJedisCluster() {
40+
return CLUSTER;
41+
}
42+
43+
@SneakyThrows
44+
public static void close(){
45+
CLUSTER.close();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.kimmking.cache;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import redis.clients.jedis.Jedis;
7+
import redis.clients.jedis.JedisCluster;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
@SpringBootApplication(scanBasePackages = "io.kimmking.cache")
13+
public class RedisApplication {
14+
15+
public static void main(String[] args) {
16+
17+
// C1.最简单demo
18+
Jedis jedis = new Jedis("localhost", 6379);
19+
System.out.println(jedis.info());
20+
jedis.set("uptime", new Long(System.currentTimeMillis()).toString());
21+
System.out.println(jedis.get("uptime"));
22+
23+
// C2.基于sentinel和连接池的demo
24+
// Jedis sjedis = SentinelJedis.getJedis();
25+
// System.out.println(sjedis.info());
26+
// sjedis.set("uptime", new Long(System.currentTimeMillis()).toString());
27+
// System.out.println(sjedis.get("uptime"));
28+
// SentinelJedis.close();
29+
30+
// C3. 直接连接sentinel进行操作
31+
// Jedis jedis = new Jedis("localhost", 26380); // 连接到sentinel
32+
// List<Map<String, String>> masters = jedis.sentinelMasters();
33+
// System.out.println(JSON.toJSONString(masters));
34+
35+
36+
// 作业:
37+
// 1. 参考C2,实现基于Lettuce和Redission的Sentinel配置
38+
// 2. 实现springboot/spring data redis的sentinel配置
39+
// 3. 使用jedis命令,使用java代码手动切换 redis 主从
40+
// Jedis jedis1 = new Jedis("localhost", 6379);
41+
// jedis1.info...
42+
// jedis1.set xxx...
43+
// Jedis jedis2 = new Jedis("localhost", 6380);
44+
// jedis2.slaveof...
45+
// jedis2.get xxx
46+
// 4. 使用C3的方式,使用java代码手动操作sentinel
47+
48+
49+
// C4. Cluster
50+
// 作业:
51+
// 5.使用命令行配置cluster:
52+
// 1) 以cluster方式启动redis-server
53+
// 2) 添加cluster节点,确认集群节点数目
54+
// 3) 分配槽位,确认分配成功
55+
// 4) 测试简单的get/set是否成功
56+
// 然后运行如下代码
57+
// JedisCluster cluster = ClusterJedis.getJedisCluster();
58+
// for (int i = 0; i < 100; i++) {
59+
// cluster.set("cluster:" + i, "data:" + i);
60+
// }
61+
// System.out.println(cluster.get("cluster:92"));
62+
// ClusterJedis.close();
63+
64+
//SpringApplication.run(RedisApplication.class, args);
65+
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.kimmking.cache;
2+
3+
import redis.clients.jedis.HostAndPort;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.JedisPoolConfig;
6+
import redis.clients.jedis.JedisSentinelPool;
7+
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
public final class SentinelJedis {
12+
13+
//可用连接实例的最大数目,默认为8;
14+
//如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)
15+
private static Integer MAX_TOTAL = 16;
16+
//控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8
17+
private static Integer MAX_IDLE = 12;
18+
//等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。
19+
//如果超过等待时间,则直接抛出JedisConnectionException
20+
private static Integer MAX_WAIT_MILLIS = 10000;
21+
//客户端超时时间配置
22+
private static Integer TIMEOUT = 10000;
23+
//在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作;
24+
//如果为true,则得到的jedis实例均是可用的
25+
private static Boolean TEST_ON_BORROW = true;
26+
//在空闲时检查有效性, 默认false
27+
private static Boolean TEST_WHILE_IDLE = true;
28+
//是否进行有效性检查
29+
private static Boolean TEST_ON_RETURN = true;
30+
31+
private static JedisSentinelPool POOL = createJedisPool();
32+
33+
/**
34+
* 创建连接池
35+
*/
36+
private static JedisSentinelPool createJedisPool() {
37+
JedisPoolConfig config = new JedisPoolConfig();
38+
/*注意:
39+
在高版本的jedis jar包,比如本版本2.9.0,JedisPoolConfig没有setMaxActive和setMaxWait属性了
40+
这是因为高版本中官方废弃了此方法,用以下两个属性替换。
41+
maxActive ==> maxTotal
42+
maxWait==> maxWaitMillis
43+
*/
44+
config.setMaxTotal(MAX_TOTAL);
45+
config.setMaxIdle(MAX_IDLE);
46+
config.setMaxWaitMillis(MAX_WAIT_MILLIS);
47+
config.setTestOnBorrow(TEST_ON_BORROW);
48+
config.setTestWhileIdle(TEST_WHILE_IDLE);
49+
config.setTestOnReturn(TEST_ON_RETURN);
50+
String masterName = "mymaster";
51+
Set<String> sentinels = new HashSet<String>();
52+
sentinels.add(new HostAndPort("127.0.0.1",26379).toString());
53+
sentinels.add(new HostAndPort("127.0.0.1",26380).toString());
54+
JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels, config, TIMEOUT, null);
55+
return pool;
56+
}
57+
58+
public static Jedis getJedis() {
59+
return POOL.getResource();
60+
}
61+
62+
public static void close(){
63+
POOL.close();
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.kimmking.cache.controller;
2+
3+
import io.kimmking.cache.entity.User;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
@RestController
12+
@EnableAutoConfiguration
13+
public class UserController {
14+
15+
16+
@RequestMapping("/user/find")
17+
User find(Integer id) {
18+
return new User(1,"KK", 28);
19+
}
20+
21+
@RequestMapping("/user/list")
22+
List<User> list() {
23+
return Arrays.asList(new User(1,"KK", 28),
24+
new User(2,"CC", 18));
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.kimmking.cache.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@Data
8+
@NoArgsConstructor
9+
@AllArgsConstructor
10+
public class User {
11+
private Integer id;
12+
private String name;
13+
private Integer age;
14+
}

Diff for: 08cache/redis/src/main/resources/application.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
server:
2+
port: 8080
3+
4+
logging:
5+
level:
6+
io:
7+
kimmking:
8+
cache : info

0 commit comments

Comments
 (0)