Redis Java客户端-通过原生jedis和SpringBoot操作Redis集群

Redis java客户端操作集群

1. 使用Jedis java原生操作redis集群

public class RedisCluster {
    public static void main(String[] args) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);

        Set<HostAndPort> clusterNodes = new HashSet<HostAndPort>();
        clusterNodes.add(new HostAndPort("192.168.0.101", 8001));
        clusterNodes.add(new HostAndPort("192.168.0.101", 8004));
        clusterNodes.add(new HostAndPort("192.168.0.102", 8002));
        clusterNodes.add(new HostAndPort("192.168.0.102", 8005));
        clusterNodes.add(new HostAndPort("192.168.0.103", 8003));
        clusterNodes.add(new HostAndPort("192.168.0.103", 8006));

        JedisCluster jedisClusterPool = new JedisCluster(clusterNodes, 6000, 5000, 10, "allen", jedisPoolConfig);
        System.out.println(jedisClusterPool.set("jedisClusterTest", "hello world jedisClusterTest Tset"));
        System.out.println(jedisClusterPool.get("jedisClusterTest"));
    }
}

执行结构:

OK
hello world jedisClusterTest Tset

Process finished with exit code 0

从集群上查看设置结果:

192.168.0.103:8003> keys *
1) "SentinelTest2"
2) "jedisClusterTest"
192.168.0.103:8003> get jedisClusterTest
"hello world jedisClusterTest Tset"

2. 使用springboot操作redis集群

1. 启动类:

@SpringBootApplication
public class ApplicationStarter {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStarter.class,args);
    }
}

 2. 服务类:

@RestController
public class IndexController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //@RequestMapping("/SentinelTest")
    @RequestMapping("/ClusterRedisTest")
   public void testSentinel() throws InterruptedException {
       int i = 1;
       while(true){
           try {
               stringRedisTemplate.opsForValue().set("SentinelTest2", i++ + "");
               System.out.println("successed set SentinelTest2 to " + i);
               Thread.sleep(1000);
           }catch (Exception e){
               System.out.println(e);
           }
       }
   }
}

Spring 配置redis cluster

server:
  port: 8080

spring:
  redis:
    database: 0
    timeout: 3000
    password: allen
    cluster:
      nodes: 192.168.0.101:8001,192.168.0.101:8004,192.168.0.102:8002,192.168.0.102:8005,192.168.0.103:8003,192.168.0.103:8006
    lettuce:
      pool:
        max‐idle: 50
        min‐idle: 10
        max‐active: 100
        max‐wait: 1000

 从页面上访问,

控制台打印结果:

"C:\Program Files\Java\jdk1.8.0_202\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.2\lib\idea_rt.jar=61446:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_202\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_202\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\javaAA_L1\redisTrainning\target\classes;C:\Users\Administrator\.m2\repository\redis\clients\jedis\3.1.0\jedis-3.1.0.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-data-redis\2.3.7.RELEASE\spring-boot-starter-data-redis-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter\2.3.7.RELEASE\spring-boot-starter-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot\2.3.7.RELEASE\spring-boot-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.7.RELEASE\spring-boot-autoconfigure-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.3.7.RELEASE\spring-boot-starter-logging-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\Administrator\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;C:\Users\Administrator\.m2\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\Administrator\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\5.2.12.RELEASE\spring-core-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-jcl\5.2.12.RELEASE\spring-jcl-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\Administrator\.m2\repository\org\springframework\data\spring-data-redis\2.3.6.RELEASE\spring-data-redis-2.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\data\spring-data-keyvalue\2.3.6.RELEASE\spring-data-keyvalue-2.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\data\spring-data-commons\2.3.6.RELEASE\spring-data-commons-2.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-tx\5.2.12.RELEASE\spring-tx-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-oxm\5.2.12.RELEASE\spring-oxm-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\5.2.12.RELEASE\spring-aop-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context-support\5.2.12.RELEASE\spring-context-support-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\io\lettuce\lettuce-core\5.3.5.RELEASE\lettuce-core-5.3.5.RELEASE.jar;C:\Users\Administrator\.m2\repository\io\netty\netty-common\4.1.53.Final\netty-common-4.1.53.Final.jar;C:\Users\Administrator\.m2\repository\io\netty\netty-handler\4.1.53.Final\netty-handler-4.1.53.Final.jar;C:\Users\Administrator\.m2\repository\io\netty\netty-resolver\4.1.53.Final\netty-resolver-4.1.53.Final.jar;C:\Users\Administrator\.m2\repository\io\netty\netty-buffer\4.1.53.Final\netty-buffer-4.1.53.Final.jar;C:\Users\Administrator\.m2\repository\io\netty\netty-codec\4.1.53.Final\netty-codec-4.1.53.Final.jar;C:\Users\Administrator\.m2\repository\io\netty\netty-transport\4.1.53.Final\netty-transport-4.1.53.Final.jar;C:\Users\Administrator\.m2\repository\io\projectreactor\reactor-core\3.3.11.RELEASE\reactor-core-3.3.11.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;C:\Users\Administrator\.m2\repository\org\apache\commons\commons-pool2\2.9.0\commons-pool2-2.9.0.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.3.7.RELEASE\spring-boot-starter-web-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.3.7.RELEASE\spring-boot-starter-json-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.3\jackson-annotations-2.11.3.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.11.3\jackson-core-2.11.3.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.3\jackson-datatype-jdk8-2.11.3.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.3\jackson-datatype-jsr310-2.11.3.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.3\jackson-module-parameter-names-2.11.3.jar;C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.7.RELEASE\spring-boot-starter-tomcat-2.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;C:\Users\Administrator\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-web\5.2.12.RELEASE\spring-web-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\5.2.12.RELEASE\spring-beans-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-webmvc\5.2.12.RELEASE\spring-webmvc-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\5.2.12.RELEASE\spring-context-5.2.12.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\5.2.12.RELEASE\spring-expression-5.2.12.RELEASE.jar" allen.redis.springboot.ApplicationStarter

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.7.RELEASE)

2021-01-21 20:19:50.462  INFO 17356 --- [           main] a.redis.springboot.ApplicationStarter    : Starting ApplicationStarter on 小白联盟-20201123GY with PID 17356 (C:\Users\Administrator\IdeaProjects\javaAA_L1\redisTrainning\target\classes started by Administrator in C:\Users\Administrator\IdeaProjects\javaAA_L1)
2021-01-21 20:19:50.467  INFO 17356 --- [           main] a.redis.springboot.ApplicationStarter    : No active profile set, falling back to default profiles: default
2021-01-21 20:19:51.535  INFO 17356 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-01-21 20:19:51.538  INFO 17356 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-01-21 20:19:51.563  INFO 17356 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 12ms. Found 0 Redis repository interfaces.
2021-01-21 20:19:52.353  INFO 17356 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-01-21 20:19:52.364  INFO 17356 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-21 20:19:52.365  INFO 17356 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-21 20:19:52.475  INFO 17356 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-21 20:19:52.476  INFO 17356 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1923 ms
2021-01-21 20:19:52.929  INFO 17356 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService
2021-01-21 20:19:53.332  INFO 17356 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-21 20:19:53.694  INFO 17356 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-01-21 20:19:53.709  INFO 17356 --- [           main] a.redis.springboot.ApplicationStarter    : Started ApplicationStarter in 3.778 seconds (JVM running for 4.274)
2021-01-21 20:20:32.151  INFO 17356 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-01-21 20:20:32.151  INFO 17356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-01-21 20:20:32.159  INFO 17356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms
successed set SentinelTest2 to 2
successed set SentinelTest2 to 3
successed set SentinelTest2 to 4
successed set SentinelTest2 to 5
successed set SentinelTest2 to 6
successed set SentinelTest2 to 7
successed set SentinelTest2 to 8
successed set SentinelTest2 to 9
...

Redis 客户端访问查看执行结果:

192.168.0.103:8003> get SentinelTest2
"233"
192.168.0.103:8003> get SentinelTest2
"234"
192.168.0.103:8003> get SentinelTest2
"235"
192.168.0.103:8003> get SentinelTest2
"235"
192.168.0.103:8003> get SentinelTest2
"236"
192.168.0.103:8003> 

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页