Skip to content

Latest commit

 

History

History
338 lines (320 loc) · 13.1 KB

README.md

File metadata and controls

338 lines (320 loc) · 13.1 KB

devcloud-go/redis

Introduction

Currently, the Redis supports three modes.single-read-write,local-read-single-write and double-write

single-read-write

image

local-read-single-write

image

double-write

image

Quickstart:

  1. use yaml configuartion file
package main

import (
    "context"
    "time"

    "github.com/huaweicloud/devcloud-go/redis"
)

func main()  {
    ctx := context.Background()
    client := redis.NewDevsporeClientWithYaml("./config_with_password.yaml")
    client.Set(ctx, "test_key", "test_val", time.Hour)
    client.Get(ctx, "test_key")
}
  1. use code(recommend)
package main

import (
    "context"
    "time"

    goredis "github.com/go-redis/redis/v8"
    "github.com/huaweicloud/devcloud-go/redis"
    "github.com/huaweicloud/devcloud-go/redis/config"
)
func main() {
    servers := map[string]*config.ServerConfiguration{
        "server1": {
            Hosts:      "127.0.0.0:6379",
            Password:   "XXXX",
            Type:       config.ServerTypeNormal,
            Cloud:      "huawei cloud",
            Region:     "beijing",
            Azs:        "az0",
        },
    }
    configuration := &config.Configuration{
        RedisConfig: &config.RedisConfiguration{
            Servers: servers,
        },
        RouteAlgorithm: "single-read-write",
        Active:         "server1",  
    }

    client := redis.NewDevsporeClient(configuration)
    ctx := context.Background()
    client.Set(ctx, "test_key", "test_val", time.Hour)
    client.Get(ctx, "test_key")
}

Yaml configuration file

props:
  version: v1
  appId: xxx
  monitorId: sdk_test
  cloud: huaweicloud
  region: cn-north-4
  azs: az1 
etcd: # Optional
  address: 127.0.0.1:2379,127.0.0.2:2379,127.0.0.3:2379
  apiVersion: v3
  username: XXXX
  password: XXXX
  httpsEnable: false
redis:
  redisGroupName: xxx-redis-group
  username: xxx # for redis 6.0
  password: yyy
  nearest: dc1
  connectionPool:
    enable: true
  servers:
    dc1:
      hosts: 127.0.0.1:6379
      password: password
      type: normal  # cluster, master-slave, normal
      cloud: huaweicloud  # cloud
      region: cn-north-4  # region id
      azs: az1  # azs
      pool: # Optional
        maxTotal: 100
        maxIdle: 8
        minIdle: 0
        maxWaitMillis: 10000
        timeBetweenEvictionRunsMillis: 1000
    dc2:
      hosts: 127.0.0.1:6380
      password: password
      type: master-slave  # cluster, master-slave, normal
      cloud: huaweicloud  # cloud
      region: cn-north-4  # region id
      azs: az1  # azs
      pool: # Optional
        maxTotal: 100
        maxIdle: 8
        minIdle: 0
        maxWaitMillis: 10000
        timeBetweenEvictionRunsMillis: 1000
routeAlgorithm: single-read-write  # local-read-single-write, single-read-write, double-write
active: dc1

Double-write

Redis also supports double-write modes, including memory double-write and file double-write, depending on asyncRemotePool.persist. true: file double-write; false: memory double-write

redis:
  redisGroupName: xxx-redis-group
  username: xxx # for redis 6.0
  password: yyy
  nearest: dc1
  asyncRemoteWrite:
    retryTimes: 4
  connectionPool:
    enable: true
  asyncRemotePool:
    persist: true
    threadCoreSize: 10
    taskQueueSize: 5
    persistDir: dataDir/
  servers:
    dc1:
      hosts: 127.0.0.1:6379
      password:
      type: normal  # cluster, master-slave, normal
      cloud: huaweicloud  # cloud
      region: cn-north-4  # region id
      azs: az1  # azs
      pool: # Optional
        maxTotal: 100
        maxIdle: 8
        minIdle: 0
        maxWaitMillis: 10000
        timeBetweenEvictionRunsMillis: 1000
    dc2:
      hosts: 127.0.0.1:6380
      password:
      type: normal  # cluster, master-slave, normal
      cloud: huaweicloud  # cloud
      region: cn-north-4  # region id
      azs: az1  # azs
      pool: # Optional
        maxTotal: 100
        maxIdle: 8
        minIdle: 0
        maxWaitMillis: 10000
        timeBetweenEvictionRunsMillis: 1000
routeAlgorithm: double-write  # local-read-single-write, single-read-write, double-write
active: dc2

Fault injection

Redis also supports the creation of services with fault injection. The configuration is similar to that of MySQL.

func DCRedis(etcdAddrs, redisAddrs []string) *redisconfig.Configuration {
    servers := make(map[string]*redisconfig.ServerConfiguration)
    for i, addr := range redisAddrs {
        stri := strconv.Itoa(i + 1)
        servers["ds"+stri] = &redisconfig.ServerConfiguration{
            Hosts:    addr,
            Password: "XXXX",
            Type:     redisconfig.ServerTypeNormal,
            Cloud:    "huawei cloud",
            Region:   "beijing",
            Azs:      "az1",
        }
    }
    configuration := &redisconfig.Configuration{
        RedisConfig: &redisconfig.RedisConfiguration{
            Servers: servers,
        },
        RouteAlgorithm: "single-read-write",
        Active:         "ds1",
        Chaos: &mas.InjectionProperties{
            Active:     true,
            Duration:   50,
            Interval:   100,
            Percentage: 100,
            DelayInjection: &mas.DelayInjection{
                Active:     true,
                Percentage: 100,
                TimeMs:     1000,
                JitterMs:   500,
            },
            ErrorInjection: &mas.ErrorInjection{
                Active:     true,
                Percentage: 30,
            },
        },
    }
    return configuration
}

Alternatively, add the following configuration to the configuration file:

chaos:
  active: true
  duration: 50
  interval: 100
  percentage: 100
  delayInjection:
    active: true
    percentage: 100
    timeMs: 1000
    jitterMs: 500
  errorInjection:
    active: true
    percentage: 20

Testing

package commands_test needs redis 6.2.0+, so if your redis is redis 5.0+, you need to execute

ginkgo -skip="redis6" 

See more usages of ginkgo in https://github.com/onsi/ginkgo

Description of Configuration Parameters

Configuration
Parameter NameParameter TypeValue rangeDescription
propsPropertiesConfigurationFor details,see the description of the data structure of PropertiesConfigurationMas monitoring configuration,which is used together with etcd
etcdEtcdConfigurationFor details,see the description of the data structure of EtcdConfigurationEtcd configuration.If it is configured, it will be pulled from the remote end
redisRedisConfigurationFor details,see the description of the data structure of RedisConfigurationRedisServer configuration
routeAlgorithmstringsingle-read-write,local-read-single-write,double-writeRouting algorithm
activestringThe value can only be dc1 or dc2Activated Redis
chaosInjectionPropertiesFor details,see the description of the data structure of InjectionPropertiesFault Injection Configuration
PropertiesConfiguration
Parameter NameParameter TypeValue rangeDescription
versionstring-Project version number
appIdstring-Project name
monitorIdstring-Monitoring group name
cloudstring-Project deployment cloud group
regionstring-Project deployment region
azsstring-Project deployment AZ
EtcdConfiguration
Parameter NameParameter TypeValue rangeDescription
addressstring-Etcd address
apiVersionstring-Etcd interface Version
usernamestring-Etcd username
passwordstring-Etcd password
httpEnablebool-Specifies whether HTTPS is enabled for Etcd
RedisConfiguration
Parameter NameParameter TypeValue rangeDescription
neareststringThe value can only be dc1 or dc2Indicates the local Redis
asyncRemoteWrite.retryTimesint-Number of retries of asynchronous remote write operations
connectionPool.enablebooltrue/falseIndicates whether to enable the connection pool
asyncRemotePoolAsyncRemotePoolConfigurationFor details,see the description of the data structure of AsyncRemotePoolConfigurationConfigure the asynchronous write thread pool
serversmap[string]ServerConfigurationThe key is dc1/dc2.for details about a single dimension,see the description of the data structure of ServerConfigurationRedisServer connection configuration of dc1 and dc2
AsyncRemotePoolConfiguration
Parameter NameParameter TypeValue rangeDescription
threadCoreSizeint-Basic size of the thread pool
persistbooltrue/falseIndicates whether the command is persistent.No:The command is fast.Yes:The speed is lower than that of non-persistent
taskQueueSizeint-Number of buffer queues
persistDirstringDefault root directory "/"Redis persistent file directory
ServerConfiguration
Parameter NameParameter TypeValue rangeDescription
hostsstring-RedisServer IP address
passwordstring-RedisServer password
typestringcluster,master-slave,normalRedisServer Type
cloudstring-RedisServer cloud
regionstring-Region to which the RedisServer belongs
azsstring-AZ to which RedisServer belongs
poolServerConnectionPoolConfigurationFor details,see the description of the data structure of ServerConnectionPoolConfigurationConnection pool configuration
ServerConnectionPoolConfiguration
Parameter NameParameter TypeValue rangeDescription
maxTotalint-Maximum number of active objects
maxIdleint-Maximum number of objects that can remain in the idle state
minIdleint-Minimum number of objects that can remain in the idle state
maxWaitMillisint-Maximum wait time when no object is returned in the pool
timeBetweenEvictionRunsMillisint-Idle link detection thread,detection interval,in milliseconds.A negative value indicates that the detection thread is not running
InjectionProperties
Parameter NameParameter TypeValue rangeDescription
activebooltrue/falseWhether the fault injection function is enabled
durationint-Fault injection duration,in seconds
intervalint-Fault injection interval,in seconds
percentageint0-100Injection failure probability
delayInjection.activebooltrue/falseDelay injection switch
delayInjection.percentageint0-100Delayed Fault Effective Probability
delayInjection.timeMsint-Indicates the delay base,in milliseconds
delayInjection.jitterMsint-Indicates the jitter amplitude of the delay, in milliseconds
errorInjection.activebooltrue/falseAbnormal injection switch
errorInjection.percentageint0-100Abnormal Fault Effective Probability