日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Redis在CentOS 6.8中的安装方法,JAVA初级使用Redis连接池

發布時間:2023/11/27 生活经验 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis在CentOS 6.8中的安装方法,JAVA初级使用Redis连接池 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、打開https://redis.io/在Download it下面直接點擊“Redis 5.0.3 is the latest stable version.”下載redis-5.0.3.tar.gz然后傳到centos系統

2、安裝c++編譯器(視網速快慢可能會等待很久)
# yum install gcc-c++
Is this ok [y/N]:y
Is this ok [y/N]:y

3、把redis-5.0.3.tar.gz復制到/usr/local/ 并開始編譯→安裝→啟動redis
# mv /root/Downloads/redis-5.0.3.tar.gz /usr/local/
# cd /usr/local/
# tar -zxvf redis-5.0.3.tar.gz
# cd redis-5.0.3
# make
# make PREFIX=/usr/local/redis/ install
# cp redis.conf /usrl/local/redis
# cd /usr/local/redis/
# vim redis.conf


/daemonize
--把daemonize no 改成daemonize yes (這樣才能讓redis在后臺運行,而不會因為命令行語句的輸入導致中斷redis線程)
/bind 127.0.0.1
--注釋掉這一行#bind 127.0.0.1
/protected-mode yes
--把no改成no
:wq


[root#localhost redis]# ./bin/redis-cli shutdown
[root#localhost redis]# ./bin/redis-server ./redis.conf

4、查看redis是否運行
# ps -ef | grep -i redis

5、開啟redis端口6379
# vim /etc/sysconfig/iptables
復制-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT這一行
然后把端口22改成6379(vim命令:復制當前行yy,粘貼p)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
:wq
# service iptables restart

6、進入redis命令行
[root#localhost redis]# ./bin/redis-cli --raw
127.0.0.1:6379> set keyname keyvalue
127.0.0.1:6379> KEYS *
127.0.0.1:6379> get keyname
127.0.0.1:6379> del keyname


7、設置redis自動啟動 https://blog.csdn.net/qq_37860634/article/details/87363180

________________________

【JAVA連接、使用redis】
1、在POM.xml引入依賴包

? ? ? ? <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.commons</groupId>
? ? ? ? ? ? <artifactId>commons-pool2</artifactId>
? ? ? ? ? ? <version>2.2</version>
? ? ? ? </dependency>

? ? ? ? <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>redis.clients</groupId>
? ? ? ? ? ? <artifactId>jedis</artifactId>
? ? ? ? ? ? <version>2.7.0</version>
? ? ? ? </dependency>

2、測試代碼

public class Test {

? ? public static void main(String[] args) {
? ? ? ? testRedis1();
? ? ? ? testRedis2();
? ? }


? ? public static void testRedis1() {
? ? ? ? Jedis jedis = new Jedis( "192.168.244.138", 6379 );//IP地址記得改成自己的
? ? ? ? jedis.set( "name", "舒工1" );
? ? ? ? String name = jedis.get( "name" );
? ? ? ? System.out.println( name );
? ? ? ? jedis.close();
? ? }


? ? public static void testRedis2() {
? ? ? ? JedisPoolConfig config = new JedisPoolConfig();
? ? ? ? config.setMaxTotal( 30 );
? ? ? ? config.setMaxIdle( 10 );
? ? ? ? JedisPool jedisPool = new JedisPool( config, "192.168.244.138", 6379 );//IP地址記得改成自己的
? ? ? ? Jedis jedis = null;
? ? ? ? try {
? ? ? ? ? ? jedis = jedisPool.getResource();
? ? ? ? ? ? jedis.set( "name", "舒工2" );
? ? ? ? ? ? String name = jedis.get( "name" );
? ? ? ? ? ? System.out.println( name );
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? if (jedis != null) jedis.close();
? ? ? ? ? ? if (jedisPool != null) jedisPool.close();
? ? ? ? }
? ? }
}

總結

以上是生活随笔為你收集整理的Redis在CentOS 6.8中的安装方法,JAVA初级使用Redis连接池的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。