使用java连接Redis,Maven管理操作

网友投稿 201 2023-04-02

使用java连接Redis,Maven管理操作

pom配置

xmlns:xsi="http://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">

4.0.0

redis

redis

1.0-SNAPSHOT

5.0.2.RELEASE

1.8

1.8

redis.clients

jedis

2.8.0

junit

junit

4.9

&lhttp://t;dependency>

commons-pool

commons-pool

1.6

org.springframework.data

spring-data-redis

2.0.3.RELEASE

xmlns:xsi="http://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">

4.0.0

redis

redis

1.0-SNAPSHOT

5.0.2.RELEASE

1.8

1.8

redis.clients

jedis

2.8.0

junit

junit

4.9

&lhttp://t;dependency>

commons-pool

commons-pool

1.6

org.springframework.data

spring-data-redis

2.0.3.RELEASE

创建db.properties文件

redis.host=bigdata-hpsk01.huadian.com

redis.port=6379

redis.maxIdle=10

redis.minIdle=10

redis.maxTotal=50

书写工具类

package com.huadian.redisUntil;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public final class JedisPoolUntil {

private static String ADDR = "192.168.59.160";

//Redis的端口号

private static int PORT = 6379;

/* //可用连接实例的最大数目,默认值为8;

//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。

private static int MAX_ACTIVE = 1024;

//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。

private static int MAX_IDLE = 200;

//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;

private static int MAX_WAIT = 10000;

private static int TIMEOUT = 10000;*//*

//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;

private static boolean TEST_ON_BORROW = true;*/

private static int MAXTOTAL=20;

private static int MINIDLE=10;

private static int MAXIDLE=15;

private static JedisPool jedisPool = null;

/**

* 初始化Redis连接池

*/

static {

try {

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxTotal(MAXTOTAL);

config.setMaxIdle(MINIDLE);

config.setMinIdle(MAXIDLE);

jedisPool = new JedisPool(config, ADDR, PORT);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 获取Jedis实例

* @return

*/

public synchronized static Jedis getJedis() {

try {

if (jedisPool != null) {

Jedis resource = jedisPool.getResource();

return resource;

} else {

return null;

}

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* 释放jedis资源

* @param jedis

*/

public static void returnResource(final Jedis jedis) {

if (jedis != null) {

jedisPool.returnResource(jedis);

}

}

}

书写测试类

package com.huadian.jedis;

import com.huadian.redisUntil.JedisPoolUntil;

import org.junit.Test;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public class JedisDemo {

private Jedis jedis = null;

/**

* 单连接

*/

@Test

public void jedisSingleConn(){

String host = "192.168.59.160";

int port = 6379;

Jedis jedis = new Jedis(host, port);

jedis.set("name","张三");

jedis.set("age","18");

String s = jedis.get("name");

String s1 = jedis.get("age");

System.out.println(s);

System.out.println(s1);

}

/**

* 连接池连接

*/

@Test

public void jedisPoolConn(){

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

jedisPoolConfig.setMaxTotal(20);

jedisPoolConfig.setMinIdle(10);

jedisPoolConfig.setMaxIdle(15);

JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.59.160", 6379);

Jedis jedis = jedisPool.getResource();

//取数据

String s = jedis.get("name");

String s1 = jedis.get("age");

System.out.println(s);

System.out.println(s1);

}

/**

* 连接池连接

* 工具类

*/

@Test

public void jedisPoolConn1(){

Jedis jedis1 = JedisPoolUntil.getJedis();

//取数据

String s = jedis1.get("name");

String s1 = jedis1.get("age");

System.out.println(s);

System.out.println(s1);

}

}

补充知识:java使用Redis所需的MAVEN的POM文件

redis不仅可以通过命令行进行操作,同时redis也可以通过javaAPI进行操作,这是操作redis所需的依赖

redis.clients

jedis

2.9.0

junit

junit

4.12

test

org.testng

testng

6.14.3

test

org.apache.maven.plugins

maven-compiler-plugin

3.0

1.8

1.8

UTF-8

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:发票验证api
下一篇:身份证实名api
相关文章

 发表评论

暂时没有评论,来抢沙发吧~