欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java連接Redis全過(guò)程講解

 更新時(shí)間:2022年06月20日 11:26:04   作者:蔣蔣又重名  
這篇文章主要介紹了Java連接Redis全過(guò)程講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java連接Redis

Jedis Client是Redis官網(wǎng)推薦的一個(gè)面向java客戶端,庫(kù)文件實(shí)現(xiàn)了對(duì)redis各類API進(jìn)行封裝調(diào)用.

引入jar包

我創(chuàng)建的是maven項(xiàng)目,所以只用在pom文件中加入

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

如果不是maven項(xiàng)目,你要確定引入相關(guān)依賴


編寫(xiě)測(cè)試類

package cn.jiangdoc;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * 
 * @author jiangdoc
 *
 */
public class JedisUtil {
	public static void main(String[] args) {
	//ip地址,端口號(hào)
		Jedis jedis = cli_single("192.168.1.103", 6379);
		jedis.set("key", "first Java connect!");
		String value = jedis.get("key");
		System.out.println(value);
		
	}
	/**
	 * 單個(gè)連接
	 * 
	 * @param host
	 * @param port
	 * @return
	 */
	public static Jedis cli_single(String host, int port) {
		try {
			return new Jedis(host, port);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 連接池
	 * 
	 * @param host
	 * @param port
	 * @return
	 */
	public static Jedis cli_pool(String host, int port) {
		JedisPoolConfig config = new JedisPoolConfig();
		// 最大連接數(shù)
		config.setMaxTotal(10);
		// 最大連接空閑數(shù)
		config.setMaxIdle(2);
		JedisPool jedisPool = new JedisPool(config, host, port);
		try{
			
			return jedisPool.getResource();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}

注意:如果出現(xiàn)

報(bào)錯(cuò):Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException:

檢查端口是否開(kāi)放

解決方法:

  • 1.關(guān)閉防火墻:service iptables stop
  • 2.開(kāi)放端口:

(1.修改配置文件:vi /etc/sysconfig/iptabls 追加:-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379-j ACCEPT

(2.包存改變:service iptables save

(3.重啟服務(wù):service iptables restart

查看redis的配置文件

報(bào)錯(cuò):DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘–protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

報(bào)錯(cuò)信息很長(zhǎng),但是主要是說(shuō)redis開(kāi)啟了protected mode,這也是Redis3.2加入的新特性,開(kāi)啟保護(hù)模式的redis只允許本機(jī)登錄,同樣設(shè)置在配置文件redis.conf中

這里原來(lái)是yes代表開(kāi)啟了保護(hù)模式,后面可以填密碼也可以填no代表關(guān)閉,我們這里選擇關(guān)閉保護(hù)模式,wq保存退出后再重啟redis-server

下面再運(yùn)行就可以了

Jedis常用方法API

前段時(shí)間給大家介紹了如何在Linux環(huán)境下部署和操作redis,今天將為大家介紹如何在我們的Java代碼中操作redis。接下來(lái) 按部就班:

一、首先把 jedis-2.1.0.jar(jedis基礎(chǔ)包)

導(dǎo)入到 java項(xiàng)目里

二、創(chuàng)建 jedis對(duì)象

三、鍵操作

四、字符串操作

五、整數(shù)和浮點(diǎn)數(shù)操作

六、列表(List)操作

七、集合(Set)操作

八、哈希(Hash)操作

九、有序集合(Zsort)操作

十、排序操作

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java如何創(chuàng)建一個(gè)jdbc程序詳解

    java如何創(chuàng)建一個(gè)jdbc程序詳解

    使用Java程序來(lái)操作數(shù)據(jù)庫(kù),后者更加直接的話就是使用Java程序來(lái)發(fā)送SQL語(yǔ)句的技術(shù)稱之為:JDBC。下面這篇文章主要給大家介紹了關(guān)于利用java如何創(chuàng)建一個(gè)jdbc程序的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-11-11
  • java 中 request.getSession(true、false、null)的區(qū)別

    java 中 request.getSession(true、false、null)的區(qū)別

    這篇文章主要介紹了java 中 request.getSession(true/false/null)的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 淺析Java常用API(Scanner,Random)匿名對(duì)象

    淺析Java常用API(Scanner,Random)匿名對(duì)象

    這篇文章主要介紹了Java常用API(Scanner,Random)匿名對(duì)象,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java實(shí)現(xiàn)圖形化界面的日歷

    Java實(shí)現(xiàn)圖形化界面的日歷

    這篇文章主要介紹了Java實(shí)現(xiàn)圖形化界面的日歷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • SpringBoot應(yīng)用啟動(dòng)內(nèi)置Tomcat的過(guò)程源碼分析

    SpringBoot應(yīng)用啟動(dòng)內(nèi)置Tomcat的過(guò)程源碼分析

    這篇文章主要介紹了SpringBoot應(yīng)用啟動(dòng)內(nèi)置Tomcat的過(guò)程分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • Spring中的AOP原理與使用詳解

    Spring中的AOP原理與使用詳解

    這篇文章主要介紹了Spring中的AOP原理與使用詳解,AOP意為面向切面編程,可以通過(guò)預(yù)編譯方式或運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)在不修改源代碼的情況下給程序動(dòng)態(tài)統(tǒng)一添加功能的一種技術(shù),需要的朋友可以參考下
    2023-12-12
  • 詳解SpringBoot 處理異常的幾種常見(jiàn)姿勢(shì)

    詳解SpringBoot 處理異常的幾種常見(jiàn)姿勢(shì)

    這篇文章主要介紹了詳解SpringBoot 處理異常的幾種常見(jiàn)姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • SpringBoot啟動(dòng)之SpringApplication初始化詳解

    SpringBoot啟動(dòng)之SpringApplication初始化詳解

    這篇文章主要介紹了SpringBoot啟動(dòng)之SpringApplication初始化詳解,首先初始化資源加載器,默認(rèn)為null;斷言判斷主要資源類不能為null,否則報(bào)錯(cuò),需要的朋友可以參考下
    2024-01-01
  • MyBatis實(shí)現(xiàn)遞歸查詢的方法詳解

    MyBatis實(shí)現(xiàn)遞歸查詢的方法詳解

    在項(xiàng)目開(kāi)發(fā)過(guò)程中,往往會(huì)遇到多級(jí)菜單、分類等多層級(jí)結(jié)構(gòu)數(shù)據(jù)的查詢。本文就來(lái)為大家講講MyBatis實(shí)現(xiàn)遞歸查詢的方法,感興趣的可以動(dòng)手嘗試一下
    2022-08-08
  • JVM內(nèi)置函數(shù)Intrinsics介紹

    JVM內(nèi)置函數(shù)Intrinsics介紹

    這篇文章主要介紹了JVM內(nèi)置函數(shù)Intrinsics,我們將學(xué)習(xí)什么是intrinsics(內(nèi)部/內(nèi)置函數(shù)),以及它們?nèi)绾卧贘ava和其他基于JVM的語(yǔ)言中工作,需要的朋友可以參考一下
    2022-02-02

最新評(píng)論