Skip to content

Commit

Permalink
- add support MasterReplica MODE
Browse files Browse the repository at this point in the history
- optimize connection utilization
- fix missing digest method when using the lower version of the lettuce client.
  • Loading branch information
Ahoo-Wang committed Jun 4, 2021
1 parent 305fc31 commit 5e5099f
Show file tree
Hide file tree
Showing 30 changed files with 259 additions and 152 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ between process cache and Redis.
> Kotlin DSL
``` kotlin
val coskyVersion = "1.1.3";
val coskyVersion = "1.1.5";
implementation("me.ahoo.cosky:spring-cloud-starter-cosky-config:${coskyVersion}")
implementation("me.ahoo.cosky:spring-cloud-starter-cosky-discovery:${coskyVersion}")
```
Expand All @@ -52,7 +52,7 @@ between process cache and Redis.
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosky.version>1.1.3</cosky.version>
<cosky.version>1.1.5</cosky.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -96,30 +96,30 @@ logging:
#### Option 1:Download the executable file
> Download [cosky-rest-api-server](https://github.com/Ahoo-Wang/cosky/releases/download/1.1.3/cosky-rest-api-1.1.3.tar)
> Download [cosky-rest-api-server](https://github.com/Ahoo-Wang/cosky/releases/download/1.1.5/cosky-rest-api-1.1.5.tar)
> tar *cosky-rest-api-1.1.3.tar*
> tar *cosky-rest-api-1.1.5.tar*
```shell
cd cosky-rest-api-1.1.3
# Working directory: cosky-rest-api-1.1.3
cd cosky-rest-api-1.1.5
# Working directory: cosky-rest-api-1.1.5
bin/cosky-rest-api --server.port=8080 --cosky.redis.uri=redis://localhost:6379
```

#### Option 2:Run On Docker

```shell
docker pull ahoowang/cosky-rest-api:1.1.3
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.3
docker pull ahoowang/cosky-rest-api:1.1.5
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.5
```

##### MacBook Pro (M1)

> Please use *ahoowang/cosky-rest-api:1.1.3-armv7*
> Please use *ahoowang/cosky-rest-api:1.1.5-armv7*
```shell
docker pull ahoowang/cosky-rest-api:1.1.3-armv7
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.3-armv7
docker pull ahoowang/cosky-rest-api:1.1.5-armv7
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.5-armv7
```

#### Option 3:Run On Kubernetes
Expand Down Expand Up @@ -147,7 +147,7 @@ spec:
value: standalone
- name: COSKY_REDIS_URI
value: redis://redis-uri:6379
image: ahoowang/cosky-rest-api:1.1.3
image: ahoowang/cosky-rest-api:1.1.5
name: cosky-rest-api
ports:
- containerPort: 8080
Expand Down Expand Up @@ -281,12 +281,12 @@ spec:
``` shell
gradle cosky-config:jmh
# or
java -jar cosky-config/build/libs/cosky-config-1.1.3-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
java -jar cosky-config/build/libs/cosky-config-1.1.5-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
# JMH version: 1.29
# VM version: JDK 11.1.31, OpenJDK 64-Bit Server VM, 11.1.31+9-LTS
# VM version: JDK 11.1.51, OpenJDK 64-Bit Server VM, 11.1.51+9-LTS
# VM invoker: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java
# VM options: -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/Users/ahoo/cosky/cosky-config/build/tmp/jmh -Duser.country=CN -Duser.language=zh -Duser.variant
# Blackhole mode: full + dont-inline hint
Expand All @@ -307,12 +307,12 @@ RedisConfigServiceBenchmark.setConfig thrpt 103659.132
``` shell
gradle cosky-discovery:jmh
# or
java -jar cosky-discovery/build/libs/cosky-discovery-1.1.3-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
java -jar cosky-discovery/build/libs/cosky-discovery-1.1.5-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
# JMH version: 1.29
# VM version: JDK 11.1.31, OpenJDK 64-Bit Server VM, 11.1.31+9-LTS
# VM version: JDK 11.1.51, OpenJDK 64-Bit Server VM, 11.1.51+9-LTS
# VM invoker: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java
# VM options: -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/Users/ahoo/cosky/cosky-discovery/build/tmp/jmh -Duser.country=CN -Duser.language=zh -Duser.variant
# Blackhole mode: full + dont-inline hint
Expand Down
32 changes: 16 additions & 16 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
> Kotlin DSL
``` kotlin
val coskyVersion = "1.1.3";
val coskyVersion = "1.1.5";
implementation("me.ahoo.cosky:spring-cloud-starter-cosky-config:${coskyVersion}")
implementation("me.ahoo.cosky:spring-cloud-starter-cosky-discovery:${coskyVersion}")
```
Expand All @@ -46,7 +46,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<cosky.version>1.1.3</cosky.version>
<cosky.version>1.1.5</cosky.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -90,30 +90,30 @@ logging:
#### 方式一:下载可执行文件
> 下载 [rest-api-server](https://github.com/Ahoo-Wang/cosky/releases/download/1.1.3/cosky-rest-api-1.1.3.tar)
> 下载 [rest-api-server](https://github.com/Ahoo-Wang/cosky/releases/download/1.1.5/cosky-rest-api-1.1.5.tar)
> 解压 *cosky-rest-api-1.1.3.tar*
> 解压 *cosky-rest-api-1.1.5.tar*
```shell
cd cosky-rest-api-1.1.3
# 工作目录: cosky-rest-api-1.1.3
cd cosky-rest-api-1.1.5
# 工作目录: cosky-rest-api-1.1.5
bin/cosky-rest-api --server.port=8080 --cosky.redis.uri=redis://localhost:6379
```

#### 方式二:在 Docker 中运行

```shell
docker pull ahoowang/cosky-rest-api:1.1.3
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.3
docker pull ahoowang/cosky-rest-api:1.1.5
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.5
```

##### MacBook Pro (M1)

> 请使用 *ahoowang/cosky-rest-api:1.1.3-armv7*
> 请使用 *ahoowang/cosky-rest-api:1.1.5-armv7*
```shell
docker pull ahoowang/cosky-rest-api:1.1.3-armv7
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.3-armv7
docker pull ahoowang/cosky-rest-api:1.1.5-armv7
docker run --name cosky-rest-api -d -p 8080:8080 --link redis -e COSKY_REDIS_URI=redis://redis:6379 ahoowang/cosky-rest-api:1.1.5-armv7
```

#### 方式三:在 Kubernetes 中运行
Expand Down Expand Up @@ -141,7 +141,7 @@ spec:
value: standalone
- name: COSKY_REDIS_URI
value: redis://redis-uri:6379
image: ahoowang/cosky-rest-api:1.1.3
image: ahoowang/cosky-rest-api:1.1.5
name: cosky-rest-api
ports:
- containerPort: 8080
Expand Down Expand Up @@ -275,12 +275,12 @@ spec:
``` shell
gradle cosky-config:jmh
# or
java -jar cosky-config/build/libs/cosky-config-1.1.3-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
java -jar cosky-config/build/libs/cosky-config-1.1.5-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
# JMH version: 1.29
# VM version: JDK 11.1.31, OpenJDK 64-Bit Server VM, 11.1.31+9-LTS
# VM version: JDK 11.1.51, OpenJDK 64-Bit Server VM, 11.1.51+9-LTS
# VM invoker: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java
# VM options: -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/Users/ahoo/cosky/config/build/tmp/jmh -Duser.country=CN -Duser.language=zh -Duser.variant
# Blackhole mode: full + dont-inline hint
Expand All @@ -301,12 +301,12 @@ RedisConfigServiceBenchmark.setConfig thrpt 103659.132
``` shell
gradle cosky-discovery:jmh
# or
java -jar cosky-discovery/build/libs/cosky-discovery-1.1.3-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
java -jar cosky-discovery/build/libs/cosky-discovery-1.1.5-jmh.jar -bm thrpt -t 25 -wi 1 -rf json -f 1
```

```
# JMH version: 1.29
# VM version: JDK 11.1.31, OpenJDK 64-Bit Server VM, 11.1.31+9-LTS
# VM version: JDK 11.1.51, OpenJDK 64-Bit Server VM, 11.1.51+9-LTS
# VM invoker: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java
# VM options: -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/Users/ahoo/cosky/discovery/build/tmp/jmh -Duser.country=CN -Duser.language=zh -Duser.variant
# Blackhole mode: full + dont-inline hint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private ConfigKeyGenerator() {
/**
* {namespace}:{@link #CONFIG_IDX}
*
* @param namespace
* @param namespace namespace
* @return
*/
public static String getConfigIdxKey(String namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.async.RedisScriptingAsyncCommands;
import me.ahoo.cosky.core.util.RedisScripts;
import me.ahoo.cosky.core.redis.RedisScripts;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import lombok.var;
import me.ahoo.cosky.core.util.RedisScripts;
import me.ahoo.cosky.core.redis.RedisScripts;
import org.junit.jupiter.api.*;

import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private class RedisClusterPubSubListenerAdapter implements RedisClusterPubSubLis
@Override
public void message(RedisClusterNode node, String channel, String message) {
if (log.isDebugEnabled()){
log.debug("Message received from a channel subscription - RedisNode[{}] | channel[{}] | message[{}].", node.getUri(), channel, message);
log.debug("Message received from a channel subscription - {} | channel[{}] | message[{}].", node, channel, message);
}
onMessage(channel, message, null);
}
Expand All @@ -79,7 +79,7 @@ public void message(RedisClusterNode node, String channel, String message) {
@Override
public void message(RedisClusterNode node, String pattern, String channel, String message) {
if (log.isDebugEnabled()){
log.debug("Message received from a pattern subscription - RedisNode[{}] | pattern[{}] | channel[{}] | message[{}].", node.getUri(), pattern, channel, message);
log.debug("Message received from a pattern subscription - {} | pattern[{}] | channel[{}] | message[{}].", node, pattern, channel, message);
}
onMessage(channel, message, pattern);
}
Expand All @@ -94,7 +94,7 @@ public void message(RedisClusterNode node, String pattern, String channel, Strin
@Override
public void subscribed(RedisClusterNode node, String channel, long count) {
if (log.isInfoEnabled()){
log.debug("Subscribed to a channel - RedisNode[{}] | channel[{}] | {}.", node.getUri(), channel, count);
log.debug("Subscribed to a channel - {} | channel[{}] | {}.", node, channel, count);
}
}

Expand All @@ -108,7 +108,7 @@ public void subscribed(RedisClusterNode node, String channel, long count) {
@Override
public void psubscribed(RedisClusterNode node, String pattern, long count) {
if (log.isInfoEnabled()){
log.info("PSubscribed to a pattern - RedisNode[{}] | pattern[{}] | {}.", node.getUri(), pattern, count);
log.info("PSubscribed to a pattern - {} | pattern[{}] | {}.", node, pattern, count);
}
}

Expand All @@ -122,7 +122,7 @@ public void psubscribed(RedisClusterNode node, String pattern, long count) {
@Override
public void unsubscribed(RedisClusterNode node, String channel, long count) {
if (log.isInfoEnabled()){
log.info("Unsubscribed from a channel - RedisNode[{}] | channel[{}] | {}.", node.getUri(), channel, count);
log.info("Unsubscribed from a channel - {} | channel[{}] | {}.", node, channel, count);
}

}
Expand All @@ -137,7 +137,7 @@ public void unsubscribed(RedisClusterNode node, String channel, long count) {
@Override
public void punsubscribed(RedisClusterNode node, String pattern, long count) {
if (log.isInfoEnabled()){
log.info("PUnsubscribed from a pattern - RedisNode[{}] | pattern[{}] | {}.", node.getUri(), pattern, count);
log.info("PUnsubscribed from a pattern - {} | pattern[{}] | {}.", node, pattern, count);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package me.ahoo.cosky.core;
package me.ahoo.cosky.core.redis;

/**
* @author ahoo wang
*/
public class RedisConfig {
private String url;
private RedisMode mode;
private ReadFrom readFrom;

public String getUrl() {
return url;
}
Expand All @@ -22,8 +24,28 @@ public void setMode(RedisMode mode) {
this.mode = mode;
}

public ReadFrom getReadFrom() {
return readFrom;
}

public void setReadFrom(ReadFrom readFrom) {
this.readFrom = readFrom;
}

public enum RedisMode {
STANDALONE,
CLUSTER
}

public enum ReadFrom {
MASTER,
MASTER_PREFERRED,
UPSTREAM,
UPSTREAM_PREFERRED,
REPLICA_PREFERRED,
REPLICA,
NEAREST,
ANY,
ANY_REPLICA
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.ahoo.cosky.core.redis;

import io.lettuce.core.api.StatefulConnection;
import io.lettuce.core.cluster.api.async.RedisClusterAsyncCommands;


/**
* @author ahoo wang
*/
public class RedisConnection implements AutoCloseable {
private StatefulConnection<String, String> connection;
private RedisClusterAsyncCommands<String, String> asyncCommands;

public RedisConnection(StatefulConnection<String, String> connection, RedisClusterAsyncCommands<String, String> asyncCommands) {
this.connection = connection;
this.asyncCommands = asyncCommands;
}

public StatefulConnection<String, String> getConnection() {
return connection;
}

public RedisClusterAsyncCommands<String, String> getAsyncCommands() {
return asyncCommands;
}

@Override
public void close() throws Exception {
connection.close();
}
}
Loading

0 comments on commit 5e5099f

Please sign in to comment.